Skip to content

fix: ignore errors in isinstance() calls on LazyProxy subclasses #2343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/openai/_utils/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def __dir__(self) -> Iterable[str]:
@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__
Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing_extensions import override

from openai._utils import LazyProxy
from openai._extras._common import MissingDependencyError


class RecursiveLazyProxy(LazyProxy[Any]):
Expand All @@ -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)