Skip to content

Commit c864725

Browse files
crdantclaude
andcommitted
Fix linting issues and remove obsolete async tests
- Remove f-string without placeholders in examples - Fix line length issues by breaking long lines - Remove obsolete async tests that referenced deleted instance_token methods - Auto-format code with black - All tests passing (26/26) - All linting checks passing (flake8, mypy, black, isort) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b7f7034 commit c864725

File tree

4 files changed

+21
-99
lines changed

4 files changed

+21
-99
lines changed

examples/basic_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def main():
8686
if token_after_instance:
8787
print(f" Service token: {token_after_instance}")
8888
if token_after_customer != token_after_instance:
89-
print(f" ⚠️ Token was replaced by instance-specific token")
89+
print(" ⚠️ Token was replaced by instance-specific token")
9090

9191
# Set instance status
9292
instance.set_status(args.status)

examples/metrics_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async def main():
8686
if token_after_instance:
8787
print(f" Service token: {token_after_instance}")
8888
if token_after_customer != token_after_instance:
89-
print(f" ⚠️ Token was replaced by instance-specific token")
89+
print(" ⚠️ Token was replaced by instance-specific token")
9090

9191
# Set instance status
9292
await instance.set_status(args.status)

replicated/resources.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ def set_version(self, version: str) -> None:
133133
def _ensure_instance(self) -> None:
134134
"""Ensure the instance ID is generated and cached."""
135135
if self.instance_id:
136-
# If we have an instance ID but a service token was provided, replace dynamic token
136+
# If we have an instance ID but a service token was provided,
137+
# replace dynamic token
137138
if self._service_account_token:
138-
self._client.state_manager.set_dynamic_token(self._service_account_token)
139+
self._client.state_manager.set_dynamic_token(
140+
self._service_account_token
141+
)
139142
return
140143

141144
# Check if instance ID is cached
@@ -144,7 +147,9 @@ def _ensure_instance(self) -> None:
144147
self.instance_id = cached_instance_id
145148
# If we have a service token provided, replace dynamic token
146149
if self._service_account_token:
147-
self._client.state_manager.set_dynamic_token(self._service_account_token)
150+
self._client.state_manager.set_dynamic_token(
151+
self._service_account_token
152+
)
148153
return
149154

150155
# Create new instance
@@ -279,9 +284,12 @@ async def set_version(self, version: str) -> None:
279284
async def _ensure_instance(self) -> None:
280285
"""Ensure the instance ID is generated and cached."""
281286
if self.instance_id:
282-
# If we have an instance ID but a service token was provided, replace dynamic token
287+
# If we have an instance ID but a service token was provided,
288+
# replace dynamic token
283289
if self._service_account_token:
284-
self._client.state_manager.set_dynamic_token(self._service_account_token)
290+
self._client.state_manager.set_dynamic_token(
291+
self._service_account_token
292+
)
285293
return
286294

287295
# Check if instance ID is cached
@@ -290,7 +298,9 @@ async def _ensure_instance(self) -> None:
290298
self.instance_id = cached_instance_id
291299
# If we have a service token provided, replace dynamic token
292300
if self._service_account_token:
293-
self._client.state_manager.set_dynamic_token(self._service_account_token)
301+
self._client.state_manager.set_dynamic_token(
302+
self._service_account_token
303+
)
294304
return
295305

296306
# Create new instance

tests/test_client.py

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def test_get_or_create_instance_with_service_token(self, mock_httpx):
203203

204204
@patch("replicated.http_client.httpx.Client")
205205
def test_ensure_instance_replaces_dynamic_token_from_api(self, mock_httpx):
206-
"""Test that _ensure_instance replaces dynamic_token with service_token from API."""
206+
"""Test that _ensure_instance replaces dynamic_token with
207+
service_token from API."""
207208
from replicated.resources import Instance
208209

209210
with tempfile.TemporaryDirectory() as tmpdir:
@@ -436,33 +437,12 @@ async def test_instance_uses_machine_id_in_headers(self):
436437
assert "X-Replicated-ClusterID" in headers
437438
assert headers["X-Replicated-ClusterID"] == client._machine_id
438439

439-
@pytest.mark.asyncio
440-
async def test_instance_token_storage_and_retrieval(self):
441-
"""Test that instance tokens can be stored and retrieved in async client."""
442-
with tempfile.TemporaryDirectory() as tmpdir:
443-
client = AsyncReplicatedClient(
444-
publishable_key="pk_test_123",
445-
app_slug="my-app",
446-
state_directory=tmpdir,
447-
)
448-
449-
# Store an instance token
450-
client.state_manager.set_instance_token(
451-
"instance_123", "instance_token_abc"
452-
)
453-
454-
# Retrieve it
455-
token = client.state_manager.get_instance_token("instance_123")
456-
assert token == "instance_token_abc"
457-
458440
@pytest.mark.asyncio
459441
async def test_instance_with_service_account_token(self):
460442
"""Test that async instances can be created with a service account token."""
461443
from replicated.resources import AsyncInstance
462444

463-
client = AsyncReplicatedClient(
464-
publishable_key="pk_test_123", app_slug="my-app"
465-
)
445+
client = AsyncReplicatedClient(publishable_key="pk_test_123", app_slug="my-app")
466446
instance = AsyncInstance(
467447
client,
468448
"customer_123",
@@ -503,71 +483,3 @@ async def test_get_or_create_instance_with_service_token(self):
503483
)
504484

505485
assert instance._service_account_token == "instance_token_abc"
506-
507-
@pytest.mark.asyncio
508-
async def test_ensure_instance_stores_service_token_from_api(self):
509-
"""Test that async _ensure_instance stores service_token from API response."""
510-
from unittest.mock import AsyncMock
511-
512-
from replicated.resources import AsyncInstance
513-
514-
with tempfile.TemporaryDirectory() as tmpdir:
515-
with patch("replicated.http_client.httpx.AsyncClient") as mock_httpx:
516-
mock_response = Mock()
517-
mock_response.is_success = True
518-
mock_response.json.return_value = {
519-
"instance_id": "instance_789",
520-
"service_token": "api_returned_token_xyz",
521-
}
522-
523-
mock_client = Mock()
524-
mock_client.request = AsyncMock(return_value=mock_response)
525-
mock_httpx.return_value = mock_client
526-
527-
client = AsyncReplicatedClient(
528-
publishable_key="pk_test_123",
529-
app_slug="my-app",
530-
state_directory=tmpdir,
531-
)
532-
instance = AsyncInstance(client, "customer_123")
533-
534-
# Trigger instance creation
535-
await instance._ensure_instance()
536-
537-
# Verify token was stored
538-
stored_token = client.state_manager.get_instance_token("instance_789")
539-
assert stored_token == "api_returned_token_xyz"
540-
541-
@pytest.mark.asyncio
542-
async def test_auth_headers_prefer_instance_token(self):
543-
"""Test that async _get_auth_headers prefers instance token over customer token."""
544-
with tempfile.TemporaryDirectory() as tmpdir:
545-
with patch("replicated.http_client.httpx.AsyncClient") as mock_httpx:
546-
mock_client = Mock()
547-
mock_httpx.return_value = mock_client
548-
549-
client = AsyncReplicatedClient(
550-
publishable_key="pk_test_123",
551-
app_slug="my-app",
552-
state_directory=tmpdir,
553-
)
554-
555-
# Set customer-level token
556-
client.state_manager.set_dynamic_token("customer_token_abc")
557-
558-
# Set instance-level token
559-
client.state_manager.set_instance_token(
560-
"instance_123", "instance_token_xyz"
561-
)
562-
563-
# Without instance_id, should use customer token
564-
headers = client._get_auth_headers()
565-
assert headers["Authorization"] == "customer_token_abc"
566-
567-
# With instance_id, should prefer instance token
568-
headers = client._get_auth_headers(instance_id="instance_123")
569-
assert headers["Authorization"] == "instance_token_xyz"
570-
571-
# With non-existent instance_id, should fall back to customer token
572-
headers = client._get_auth_headers(instance_id="instance_999")
573-
assert headers["Authorization"] == "customer_token_abc"

0 commit comments

Comments
 (0)