MCPcopy
hub / github.com/python/mypy / _belongs_to_runtime

Function _belongs_to_runtime

mypy/stubtest.py:376–407  ·  view source on GitHub ↗

Heuristics to determine whether a name originates from another module.

(r: types.ModuleType, attr: str)

Source from the content-addressed store, hash-verified

374 to_check = {m for m, o in stub.names.items() if not o.module_hidden}
375
376 def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
377 """Heuristics to determine whether a name originates from another module."""
378 obj = getattr(r, attr)
379 if isinstance(obj, types.ModuleType):
380 return False
381
382 symbol_table = _module_symbol_table(r)
383 if symbol_table is not None:
384 try:
385 symbol = symbol_table.lookup(attr)
386 except KeyError:
387 pass
388 else:
389 if symbol.is_imported():
390 # symtable says we got this from another module
391 return False
392 # But we can't just return True here, because symtable doesn't know about symbols
393 # that come from `from module import *`
394 if symbol.is_assigned():
395 # symtable knows we assigned this symbol in the module
396 return True
397
398 # The __module__ attribute is unreliable for anything except functions and classes,
399 # but it's our best guess at this point
400 try:
401 obj_mod = obj.__module__
402 except Exception:
403 pass
404 else:
405 if isinstance(obj_mod, str):
406 return bool(obj_mod == r.__name__)
407 return True
408
409 runtime_public_contents = (
410 runtime_all_as_set

Callers 1

verify_mypyfileFunction · 0.85

Calls 5

getattrFunction · 0.85
isinstanceFunction · 0.85
_module_symbol_tableFunction · 0.85
boolClass · 0.85
lookupMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…