-
Notifications
You must be signed in to change notification settings - Fork 0
fix: implement lazy client creation in replicate.use() #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e05e719
fix: implement lazy client creation in replicate.use()
zeke 9d4e23f
style: fix linter issues
zeke 0c94641
fix: resolve async detection and test issues
zeke 6c1c60f
test: simplify lazy client test
zeke 0f95a92
lint
zeke ff47d56
fix: add type ignore for final linter warning
zeke 0ab8c4f
fix: add arg-type ignore for type checker warnings
zeke ab20bbf
refactor: simplify lazy client creation to use Type[Client] only
zeke 0017b48
Update tests/test_simple_lazy.py
zeke 117dbdc
test: improve lazy client test to follow project conventions
zeke 69162f3
lint
zeke 4f608fd
lint
zeke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """Test lazy client creation in replicate.use().""" | ||
|
|
||
| import os | ||
| import sys | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
|
|
||
| def test_use_does_not_raise_without_token(): | ||
| """Test that replicate.use() works even when no API token is available.""" | ||
| sys.path.insert(0, "src") | ||
|
|
||
| with patch.dict(os.environ, {}, clear=True): | ||
| with patch.dict(sys.modules, {"cog": None}): | ||
| import replicate | ||
|
|
||
| # Should not raise an exception | ||
| model = replicate.use("test/model") # type: ignore[misc] | ||
| assert model is not None | ||
|
|
||
|
|
||
| def test_cog_current_scope(): | ||
| """Test that cog.current_scope().context is read on each client creation.""" | ||
| sys.path.insert(0, "src") | ||
|
|
||
| mock_context = MagicMock() | ||
| mock_context.items.return_value = [("REPLICATE_API_TOKEN", "test-token-1")] | ||
|
|
||
| mock_scope = MagicMock() | ||
| mock_scope.context = mock_context | ||
|
|
||
| mock_cog = MagicMock() | ||
| mock_cog.current_scope.return_value = mock_scope | ||
|
|
||
| with patch.dict(os.environ, {}, clear=True): | ||
| with patch.dict(sys.modules, {"cog": mock_cog}): | ||
| import replicate | ||
|
|
||
| model = replicate.use("test/model") # type: ignore[misc] | ||
|
|
||
| # Access the client property - this should trigger client creation and cog.current_scope call | ||
| _ = model._client | ||
|
|
||
| assert mock_cog.current_scope.call_count == 1 | ||
|
|
||
| # Change the token and access client again - should trigger another call | ||
| mock_context.items.return_value = [("REPLICATE_API_TOKEN", "test-token-2")] | ||
|
|
||
| # Create a new model to trigger another client creation | ||
| model2 = replicate.use("test/model2") # type: ignore[misc] | ||
| _ = model2._client | ||
|
|
||
| assert mock_cog.current_scope.call_count == 2 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor tweak, we want to assert that the same model refreshes the client between calls. If we're looking at the client we can look at the
bearer_tokento verify it has actually picked up the token correctly.