Skip to content

Commit 512e5d7

Browse files
claude[bot]zeke
andcommitted
fix: resolve lint issues and test logic for cog integration
- Fix import sorting violations in _client.py and cog.py - Remove unused imports (cast, Iterator) from cog.py - Fix test logic to distinguish between cog unavailable vs cog returning None - When cog explicitly provides a value (even None), use it without env fallback - Only fallback to environment when cog is unavailable or fails 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Zeke Sikelianos <zeke@users.noreply.github.com>
1 parent 8347763 commit 512e5d7

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/replicate/_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
Union,
1717
overload,
1818
)
19-
from typing_extensions import Self, Unpack, ParamSpec, override
2019

2120
import httpx
21+
from typing_extensions import ParamSpec, Self, Unpack, override
2222

2323
from replicate.lib._files import FileEncodingStrategy
24-
from replicate.lib._predictions_run import Model, Version, ModelVersionIdentifier
24+
from replicate.lib._predictions_run import Model, ModelVersionIdentifier, Version
2525
from replicate.lib.cog import get_api_token_from_environment
2626
from replicate.types.prediction_create_params import PredictionCreateParamsWithoutVersion
2727

src/replicate/lib/cog.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,62 @@
22

33
import logging
44
import os
5-
from typing import Any, Iterator, Optional, cast
5+
from typing import Any, Optional
66

77
logger = logging.getLogger("replicate")
88

99

10-
def get_api_token_from_current_scope() -> Optional[str]:
11-
"""Get API token from cog's current_scope if available, otherwise return None.
10+
def get_api_token_from_current_scope() -> tuple[bool, Optional[str]]:
11+
"""Get API token from cog's current_scope if available.
1212
1313
This function attempts to retrieve the API token from cog's current_scope context.
14-
It gracefully handles all errors and returns None if cog is not available or if
15-
any part of the retrieval process fails.
14+
It gracefully handles all errors and returns a tuple indicating success and the value.
1615
1716
Returns:
18-
Optional[str]: The API token from cog's current_scope, or None if not available.
17+
tuple[bool, Optional[str]]: (success, token) where success indicates if cog
18+
was available and provided a value, and token is the actual value (or None).
1919
"""
2020
try:
2121
import cog # type: ignore[import-untyped, import-not-found]
2222

2323
# Get the current scope - this might return None or raise an exception
2424
scope = getattr(cog, "current_scope", lambda: None)()
2525
if scope is None:
26-
return None
26+
return (False, None)
2727

2828
# Get the context from the scope
2929
context = getattr(scope, "context", None)
3030
if context is None:
31-
return None
31+
return (False, None)
3232

3333
# Get the items method and call it
3434
items_method = getattr(context, "items", None)
3535
if not callable(items_method):
36-
return None
36+
return (False, None)
3737

3838
# Iterate through context items looking for the API token
39-
items = cast(Iterator[tuple[Any, Any]], items_method())
39+
items = items_method()
4040
for key, value in items:
4141
if str(key).upper() == "REPLICATE_API_TOKEN":
42-
return str(value) if value is not None else value
42+
# Found the key - return success=True with the actual value (even if None)
43+
return (True, str(value) if value is not None else value)
44+
45+
# API token key not found in context
46+
return (False, None)
4347

4448
except Exception as e: # Catch all exceptions to ensure robust fallback
4549
logger.debug("Failed to retrieve API token from cog.current_scope(): %s", e)
4650

47-
return None
51+
return (False, None)
4852

4953

5054
def get_api_token_from_environment() -> Optional[str]:
5155
"""Get API token from cog current scope if available, otherwise from environment."""
5256
# Try to get token from cog's current_scope first
53-
token = get_api_token_from_current_scope()
54-
if token is not None:
57+
success, token = get_api_token_from_current_scope()
58+
if success:
59+
# Cog was available and provided a value (even if None) - use it
5560
return token
5661

57-
# Fall back to environment variable
62+
# Cog was not available or failed - fall back to environment variable
5863
return os.environ.get("REPLICATE_API_TOKEN")

0 commit comments

Comments
 (0)