Skip to content

Commit

Permalink
stubtest: catch more getattr errors (#12219)
Browse files Browse the repository at this point in the history
Fixes for python/typeshed#7307
Amusingly, the evil runtime module that is causing havoc is... mypy,
which sqlalchemy has some extension for.

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja committed Feb 20, 2022
1 parent 58514a9 commit 608f274
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ def verify_mypyfile(

def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
obj = getattr(r, attr)
obj_mod = getattr(obj, "__module__", None)
try:
obj_mod = getattr(obj, "__module__", None)
except Exception:
return False
if obj_mod is not None:
return obj_mod == r.__name__
return not isinstance(obj, types.ModuleType)
Expand All @@ -241,11 +244,13 @@ def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
# Don't recursively check exported modules, since that leads to infinite recursion
continue
assert stub_entry is not None
yield from verify(
stub_entry,
getattr(runtime, entry, MISSING),
object_path + [entry],
)
try:
runtime_entry = getattr(runtime, entry, MISSING)
except Exception:
# Catch all exceptions in case the runtime raises an unexpected exception
# from __getattr__ or similar.
continue
yield from verify(stub_entry, runtime_entry, object_path + [entry])


if sys.version_info >= (3, 7):
Expand Down

0 comments on commit 608f274

Please sign in to comment.