You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: implement lazy client creation in replicate.use() (#57)
* fix: implement lazy client creation in replicate.use()
Fixes issue where replicate.use() would fail if no API token was available at call time,
even when token becomes available later (e.g., from cog.current_scope).
Changes:
- Modified Function/AsyncFunction classes to accept client factories
- Added _client property that creates client on demand
- Updated module client to pass factory functions instead of instances
- Token is now retrieved from current scope when model is called
This maintains full backward compatibility while enabling use in Cog pipelines
where tokens are provided through the execution context.
* style: fix linter issues
- Remove unused *args parameter in test function
- Fix formatting issues from linter
* 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
* test: simplify lazy client test
Replace complex mocking test with simpler verification that:
- use() works without token initially
- Lazy client factory is properly configured
- Client can be created when needed
This avoids complex mocking while still verifying the core functionality.
* lint
* fix: add type ignore for final linter warning
* fix: add arg-type ignore for type checker warnings
* refactor: simplify lazy client creation to use Type[Client] only
Address PR feedback by removing Union types and using a single consistent approach:
- Change Function/AsyncFunction constructors to accept Type[Client] only
- Remove Union[Client, Type[Client]] in favor of just Type[Client]
- Simplify _client property logic by removing isinstance checks
- Update all use() overloads to accept class types only
- Use issubclass() for async client detection instead of complex logic
- Update tests to check for _client_class attribute
This maintains the same lazy client creation behavior while being
much simpler and more consistent.
* Update tests/test_simple_lazy.py
Co-authored-by: Aron Carroll <[email protected]>
* test: improve lazy client test to follow project conventions
- Remove verbose comments and print statements
- Focus on observable behavior rather than internal implementation
- Use proper mocking that matches actual cog integration
- Test that cog.current_scope() is called on client creation
- Address code review feedback from PR discussion
* lint
* lint
---------
Co-authored-by: Aron Carroll <[email protected]>
0 commit comments