openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/test_utils/test_proxy.py
35lines · modeblame
2e7e897aStainless Bot2 years ago | 1 | import operator |
| 2 | from typing import Any | |
| 3 | from typing_extensions import override | |
| 4 | | |
| 5 | from openai._utils import LazyProxy | |
08d67adfBruno Alla1 years ago | 6 | from openai._extras._common import MissingDependencyError |
2e7e897aStainless Bot2 years ago | 7 | |
| 8 | | |
| 9 | class RecursiveLazyProxy(LazyProxy[Any]): | |
| 10 | @override | |
| 11 | def __load__(self) -> Any: | |
| 12 | return self | |
| 13 | | |
| 14 | def __call__(self, *_args: Any, **_kwds: Any) -> Any: | |
| 15 | raise RuntimeError("This should never be called!") | |
| 16 | | |
| 17 | | |
| 18 | def test_recursive_proxy() -> None: | |
| 19 | proxy = RecursiveLazyProxy() | |
| 20 | assert repr(proxy) == "RecursiveLazyProxy" | |
| 21 | assert str(proxy) == "RecursiveLazyProxy" | |
| 22 | assert dir(proxy) == [] | |
e967f5a5Stainless Bot2 years ago | 23 | assert type(proxy).__name__ == "RecursiveLazyProxy" |
2e7e897aStainless Bot2 years ago | 24 | assert type(operator.attrgetter("name.foo.bar.baz")(proxy)).__name__ == "RecursiveLazyProxy" |
08d67adfBruno Alla1 years ago | 25 | |
| 26 | | |
e2417755stainless-app[bot]1 years ago | 27 | def test_isinstance_does_not_error() -> None: |
08d67adfBruno Alla1 years ago | 28 | class MissingDepsProxy(LazyProxy[Any]): |
| 29 | @override | |
| 30 | def __load__(self) -> Any: | |
| 31 | raise MissingDependencyError("Mocking missing dependency") | |
| 32 | | |
| 33 | proxy = MissingDepsProxy() | |
| 34 | assert not isinstance(proxy, dict) | |
| 35 | assert isinstance(proxy, LazyProxy) |