Commit caf4c4e
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 <aron@aroncarroll.com>
* 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 <aron@aroncarroll.com>1 parent af6ae70 commit caf4c4e
File tree
3 files changed
+78
-15
lines changed- src/replicate
- lib
- tests
3 files changed
+78
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
94 | 96 | | |
95 | | - | |
96 | | - | |
97 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
98 | 102 | | |
99 | 103 | | |
100 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
436 | 437 | | |
437 | 438 | | |
438 | 439 | | |
439 | | - | |
440 | 440 | | |
441 | 441 | | |
442 | 442 | | |
443 | | - | |
444 | | - | |
| 443 | + | |
| 444 | + | |
445 | 445 | | |
446 | 446 | | |
447 | 447 | | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
448 | 452 | | |
449 | 453 | | |
450 | 454 | | |
| |||
666 | 670 | | |
667 | 671 | | |
668 | 672 | | |
669 | | - | |
670 | 673 | | |
671 | 674 | | |
672 | 675 | | |
673 | 676 | | |
674 | | - | |
675 | | - | |
| 677 | + | |
| 678 | + | |
676 | 679 | | |
677 | 680 | | |
678 | 681 | | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
679 | 686 | | |
680 | 687 | | |
681 | 688 | | |
| |||
804 | 811 | | |
805 | 812 | | |
806 | 813 | | |
807 | | - | |
| 814 | + | |
808 | 815 | | |
809 | 816 | | |
810 | 817 | | |
| |||
814 | 821 | | |
815 | 822 | | |
816 | 823 | | |
817 | | - | |
| 824 | + | |
818 | 825 | | |
819 | 826 | | |
820 | 827 | | |
| |||
824 | 831 | | |
825 | 832 | | |
826 | 833 | | |
827 | | - | |
| 834 | + | |
828 | 835 | | |
829 | 836 | | |
830 | 837 | | |
| |||
834 | 841 | | |
835 | 842 | | |
836 | 843 | | |
837 | | - | |
| 844 | + | |
838 | 845 | | |
839 | 846 | | |
840 | 847 | | |
| |||
843 | 850 | | |
844 | 851 | | |
845 | 852 | | |
846 | | - | |
| 853 | + | |
847 | 854 | | |
848 | 855 | | |
849 | 856 | | |
| |||
868 | 875 | | |
869 | 876 | | |
870 | 877 | | |
871 | | - | |
| 878 | + | |
872 | 879 | | |
873 | 880 | | |
874 | 881 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
0 commit comments