Commit 08d67adf

Bruno Alla <browniebroke@users.noreply.github.com>
2025-05-08 01:50:47
fix: ignore errors in isinstance() calls on LazyProxy subclasses (#2343)
Fix #2056
1 parent 917dade
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)