Skip to content

Commit

Permalink
Stubtest: clean up the _belongs_to_runtime function (#14361)
Browse files Browse the repository at this point in the history
There's a small semantic change in this PR (instead of returning `False`
if trying to access the `__module__` attribute raises an exception, we
now just move on to the next heuristic). But the main purpose of this PR
is to make the code more readable, as this function was getting quite
hard to understand.

Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
  • Loading branch information
AlexWaygood and hauntsaninja authored Dec 29, 2022
1 parent 9dc624b commit 5e817cd
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 @@ -346,16 +346,20 @@ def verify_mypyfile(
imported_symbols = _get_imported_symbol_names(runtime)

def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
"""Heuristics to determine whether a name originates from another module."""
obj = getattr(r, attr)
if isinstance(obj, types.ModuleType):
return False
if callable(obj):
# It's highly likely to be a class or a function if it's callable,
# so the __module__ attribute will give a good indication of which module it comes from
try:
obj_mod = getattr(obj, "__module__", None)
obj_mod = obj.__module__
except Exception:
return False
if obj_mod is not None:
return bool(obj_mod == r.__name__)
pass
else:
if isinstance(obj_mod, str):
return bool(obj_mod == r.__name__)
if imported_symbols is not None:
return attr not in imported_symbols
return True
Expand All @@ -367,8 +371,9 @@ def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
m
for m in dir(runtime)
if not is_probably_private(m)
# Ensure that the object's module is `runtime`, since in the absence of __all__ we
# don't have a good way to detect re-exports at runtime.
# Filter out objects that originate from other modules (best effort). Note that in the
# absence of __all__, we don't have a way to detect explicit / intentional re-exports
# at runtime
and _belongs_to_runtime(runtime, m)
}
)
Expand Down

0 comments on commit 5e817cd

Please sign in to comment.