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

Function verify_mypyfile

mypy/stubtest.py:349–459  ·  view source on GitHub ↗
(
    stub: nodes.MypyFile, runtime: MaybeMissing[types.ModuleType], object_path: list[str]
)

Source from the content-addressed store, hash-verified

347
348@verify.register(nodes.MypyFile)
349def verify_mypyfile(
350 stub: nodes.MypyFile, runtime: MaybeMissing[types.ModuleType], object_path: list[str]
351) -> Iterator[Error]:
352 if isinstance(runtime, Missing):
353 yield Error(object_path, "is not present at runtime", stub, runtime)
354 return
355 if not isinstance(runtime, types.ModuleType):
356 # Can possibly happen:
357 yield Error(object_path, "is not a module", stub, runtime) # type: ignore[unreachable]
358 return
359
360 runtime_all_as_set: set[str] | None
361
362 if hasattr(runtime, "__all__"):
363 runtime_all_as_set = set(runtime.__all__)
364 if "__all__" in stub.names:
365 # Only verify the contents of the stub's __all__
366 # if the stub actually defines __all__
367 yield from _verify_exported_names(object_path, stub, runtime_all_as_set)
368 else:
369 yield Error(object_path + ["__all__"], "is not present in stub", MISSING, runtime)
370 else:
371 runtime_all_as_set = None
372
373 # Check things in the stub
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__)

Callers

nothing calls this directly

Calls 14

isinstanceFunction · 0.85
ErrorClass · 0.85
hasattrFunction · 0.85
setClass · 0.85
_verify_exported_namesFunction · 0.85
is_probably_privateFunction · 0.85
_belongs_to_runtimeFunction · 0.85
sortedFunction · 0.85
_is_decoratableFunction · 0.85
getattrFunction · 0.85
verifyFunction · 0.85
rsplitMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…