Skip to content

Commit 0c94641

Browse files
committed
fix: resolve async detection and test issues
- Fix async detection to not call client factory prematurely - Add use_async parameter to explicitly indicate async mode - Update test to avoid creating client during verification - Fix test mocking to use correct module path
1 parent 9d4e23f commit 0c94641

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/replicate/_module_client.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,14 @@ def _run(*args, **kwargs):
8888
return _load_client().run(*args, **kwargs)
8989

9090
def _use(ref, *, hint=None, streaming=False, use_async=False, **kwargs):
91+
from .lib._predictions_use import use
92+
9193
if use_async:
9294
# For async, we need to use AsyncReplicate instead
9395
from ._client import AsyncReplicate
94-
from .lib._predictions_use import use
95-
96-
return use(lambda: AsyncReplicate(), ref, hint=hint, streaming=streaming, **kwargs)
97-
from .lib._predictions_use import use
98-
99-
return use(_load_client, ref, hint=hint, streaming=streaming, **kwargs)
96+
return use(lambda: AsyncReplicate(), ref, hint=hint, streaming=streaming, use_async=True, **kwargs)
97+
98+
return use(_load_client, ref, hint=hint, streaming=streaming, use_async=False, **kwargs)
10099

101100
run = _run
102101
use = _use

src/replicate/lib/_predictions_use.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ def use(
858858
*,
859859
hint: Optional[Callable[Input, Output]] = None, # pylint: disable=unused-argument # noqa: ARG001 # required for type inference
860860
streaming: bool = False,
861+
use_async: bool = False, # Internal parameter to indicate async mode
861862
) -> Union[
862863
Function[Input, Output],
863864
AsyncFunction[Input, Output],
@@ -878,8 +879,8 @@ def use(
878879
except AttributeError:
879880
pass
880881

881-
# Determine if this is async by checking the type
882-
is_async = isinstance(client, AsyncClient) or (callable(client) and isinstance(client(), AsyncClient))
882+
# Determine if this is async
883+
is_async = isinstance(client, AsyncClient) or use_async
883884

884885
if is_async:
885886
# TODO: Fix type inference for AsyncFunction return type

tests/test_simple_lazy.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ def test_use_does_not_create_client_immediately():
2424
assert isinstance(model, Function)
2525
print("✓ replicate.use() works without immediate client creation")
2626

27-
# Verify the client is stored as a callable (factory function)
28-
assert callable(model._client)
29-
print("✓ Client is stored as factory function")
27+
# Verify the client property is a property that will create client on demand
28+
# We can't call it without a token, but we can check it's the right type
29+
assert hasattr(model, '_client_or_factory')
30+
print("✓ Client factory is stored for lazy creation")
3031

3132
except Exception as e:
3233
print(f"✗ Test failed: {e}")
@@ -54,7 +55,7 @@ def track_client_creation(**kwargs):
5455

5556
with patch.dict(os.environ, {}, clear=True):
5657
with patch.dict(sys.modules, {"cog": mock_cog}):
57-
with patch("replicate._module_client._ModuleClient", side_effect=track_client_creation):
58+
with patch("replicate._client._ModuleClient", side_effect=track_client_creation):
5859
import replicate
5960

6061
# Create model function - should not create client yet

0 commit comments

Comments
 (0)