Skip to content

Commit 608f274

Browse files
authored
stubtest: catch more getattr errors (#12219)
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 <>
1 parent 58514a9 commit 608f274

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

mypy/stubtest.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ def verify_mypyfile(
214214

215215
def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
216216
obj = getattr(r, attr)
217-
obj_mod = getattr(obj, "__module__", None)
217+
try:
218+
obj_mod = getattr(obj, "__module__", None)
219+
except Exception:
220+
return False
218221
if obj_mod is not None:
219222
return obj_mod == r.__name__
220223
return not isinstance(obj, types.ModuleType)
@@ -241,11 +244,13 @@ def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
241244
# Don't recursively check exported modules, since that leads to infinite recursion
242245
continue
243246
assert stub_entry is not None
244-
yield from verify(
245-
stub_entry,
246-
getattr(runtime, entry, MISSING),
247-
object_path + [entry],
248-
)
247+
try:
248+
runtime_entry = getattr(runtime, entry, MISSING)
249+
except Exception:
250+
# Catch all exceptions in case the runtime raises an unexpected exception
251+
# from __getattr__ or similar.
252+
continue
253+
yield from verify(stub_entry, runtime_entry, object_path + [entry])
249254

250255

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

0 commit comments

Comments
 (0)