Skip to content

Commit 52cbbdf

Browse files
authored
fix: ignore errors in isinstance() calls on LazyProxy subclasses (#2343)
Fix #2056
1 parent 834813c commit 52cbbdf

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/openai/_utils/_proxy.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ def __dir__(self) -> Iterable[str]:
4646
@property # type: ignore
4747
@override
4848
def __class__(self) -> type: # pyright: ignore
49-
proxied = self.__get_proxied__()
49+
try:
50+
proxied = self.__get_proxied__()
51+
except Exception:
52+
return type(self)
5053
if issubclass(type(proxied), LazyProxy):
5154
return type(proxied)
5255
return proxied.__class__

tests/test_utils/test_proxy.py

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing_extensions import override
44

55
from openai._utils import LazyProxy
6+
from openai._extras._common import MissingDependencyError
67

78

89
class RecursiveLazyProxy(LazyProxy[Any]):
@@ -21,3 +22,14 @@ def test_recursive_proxy() -> None:
2122
assert dir(proxy) == []
2223
assert type(proxy).__name__ == "RecursiveLazyProxy"
2324
assert type(operator.attrgetter("name.foo.bar.baz")(proxy)).__name__ == "RecursiveLazyProxy"
25+
26+
27+
def test_is_instance_with_missing_dependency_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)

0 commit comments

Comments
 (0)