Commit 08d67adf
Changed files (2)
src
openai
_utils
tests
test_utils
src/openai/_utils/_proxy.py
@@ -46,7 +46,10 @@ class LazyProxy(Generic[T], ABC):
@property # type: ignore
@override
def __class__(self) -> type: # pyright: ignore
- proxied = self.__get_proxied__()
+ try:
+ proxied = self.__get_proxied__()
+ except Exception:
+ return type(self)
if issubclass(type(proxied), LazyProxy):
return type(proxied)
return proxied.__class__
tests/test_utils/test_proxy.py
@@ -3,6 +3,7 @@ from typing import Any
from typing_extensions import override
from openai._utils import LazyProxy
+from openai._extras._common import MissingDependencyError
class RecursiveLazyProxy(LazyProxy[Any]):
@@ -21,3 +22,14 @@ def test_recursive_proxy() -> None:
assert dir(proxy) == []
assert type(proxy).__name__ == "RecursiveLazyProxy"
assert type(operator.attrgetter("name.foo.bar.baz")(proxy)).__name__ == "RecursiveLazyProxy"
+
+
+def test_is_instance_with_missing_dependency_error() -> None:
+ class MissingDepsProxy(LazyProxy[Any]):
+ @override
+ def __load__(self) -> Any:
+ raise MissingDependencyError("Mocking missing dependency")
+
+ proxy = MissingDepsProxy()
+ assert not isinstance(proxy, dict)
+ assert isinstance(proxy, LazyProxy)