openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/test_utils/test_proxy.py
35lines · modecode
| 1 | import operator |
| 2 | from typing import Any |
| 3 | from typing_extensions import override |
| 4 | |
| 5 | from openai._utils import LazyProxy |
| 6 | from openai._extras._common import MissingDependencyError |
| 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) == [] |
| 23 | assert type(proxy).__name__ == "RecursiveLazyProxy" |
| 24 | assert type(operator.attrgetter("name.foo.bar.baz")(proxy)).__name__ == "RecursiveLazyProxy" |
| 25 | |
| 26 | |
| 27 | def test_isinstance_does_not_error() -> None: |
| 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) |
| 36 | |