Skip to content
Open
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
4 changes: 4 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,10 @@ def _named_type(name: str) -> mypy.types.Instance:
return mypy.types.TupleType(items, fallback)

fallback = mypy.types.Instance(type_info, [anytype() for _ in type_info.type_vars])
if type(runtime) != runtime.__class__:
# Since `__class__` is redefined for an instance, we can't trust
# its `isinstance` checks, it can be dynamic. See #20919
return fallback

value: bool | int | str
if isinstance(runtime, enum.Enum) and isinstance(runtime.name, str):
Expand Down
43 changes: 43 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,49 @@ def f(): return 3
error=None,
)

@collect_cases
def test_proxy_object(self) -> Iterator[Case]:
yield Case(
stub="""
class LazyObject:
def __init__(self, func: object) -> None: ...
def __bool__(self) -> bool: ...
""",
runtime="""
class LazyObject:
def __init__(self, func):
self.__dict__["_wrapped"] = None
self.__dict__["_setupfunc"] = func
def _setup(self):
self.__dict__["_wrapped"] = self._setupfunc()
@property
def __class__(self):
if self._wrapped is None:
self._setup()
return type(self._wrapped)
def __bool__(self):
if self._wrapped is None:
self._setup()
return bool(self._wrapped)
""",
error="test_module.LazyObject.__class__",
)
yield Case(
stub="""
def default_value() -> bool: ...

DEFAULT_VALUE: bool
""",
runtime="""
def default_value():
return True

DEFAULT_VALUE = LazyObject(default_value)
bool(DEFAULT_VALUE) # evaluate the lazy object
""",
error="test_module.DEFAULT_VALUE",
)

@collect_cases
def test_all_at_runtime_not_stub(self) -> Iterator[Case]:
yield Case(
Expand Down