Skip to content

Commit

Permalink
stubtest: fallback to getattr_static on AttributeError (#13285)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Jul 29, 2022
1 parent 6648199 commit 5e340c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ class SubClass(runtime): # type: ignore
stub_to_verify = next((t.names[entry].node for t in stub.mro if entry in t.names), MISSING)
assert stub_to_verify is not None
try:
runtime_attr = getattr(runtime, mangled_entry, MISSING)
try:
runtime_attr = getattr(runtime, mangled_entry)
except AttributeError:
runtime_attr = inspect.getattr_static(runtime, mangled_entry, MISSING)
except Exception:
# Catch all exceptions in case the runtime raises an unexpected exception
# from __getattr__ or similar.
Expand Down
20 changes: 20 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,26 @@ def read_write_attr(self, val): self._val = val
""",
error=None,
)
yield Case(
stub="""
class FineAndDandy:
@property
def attr(self) -> int: ...
""",
runtime="""
class _EvilDescriptor:
def __get__(self, instance, ownerclass=None):
if instance is None:
raise AttributeError('no')
return 42
def __set__(self, instance, value):
raise AttributeError('no')
class FineAndDandy:
attr = _EvilDescriptor()
""",
error=None,
)

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

0 comments on commit 5e340c1

Please sign in to comment.