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

Function test_module

mypy/stubtest.py:230–283  ·  view source on GitHub ↗

Tests a given module's stub against introspecting it at runtime. Requires the stub to have been built already, accomplished by a call to ``build_stubs``. :param module_name: The module to test

(module_name: str)

Source from the content-addressed store, hash-verified

228
229
230def test_module(module_name: str) -> Iterator[Error]:
231 """Tests a given module's stub against introspecting it at runtime.
232
233 Requires the stub to have been built already, accomplished by a call to ``build_stubs``.
234
235 :param module_name: The module to test
236
237 """
238 stub = get_stub(module_name)
239 if stub is None:
240 if not is_probably_private(module_name.split(".")[-1]):
241 runtime_desc = repr(sys.modules[module_name]) if module_name in sys.modules else "N/A"
242 yield Error(
243 [module_name], "failed to find stubs", MISSING, None, runtime_desc=runtime_desc
244 )
245 return
246
247 try:
248 runtime = silent_import_module(module_name)
249 except KeyboardInterrupt:
250 raise
251 except BaseException as e:
252 note = ""
253 if isinstance(e, ModuleNotFoundError):
254 note = " Maybe install the runtime package or alter PYTHONPATH?"
255 yield Error(
256 [module_name], f"failed to import.{note} {type(e).__name__}: {e}", stub, MISSING
257 )
258 return
259
260 with warnings.catch_warnings():
261 warnings.simplefilter("ignore")
262 try:
263 yield from verify(stub, runtime, [module_name])
264 except Exception as e:
265 bottom_frame = list(traceback.walk_tb(e.__traceback__))[-1][0]
266 bottom_module = bottom_frame.f_globals.get("__name__", "")
267 # Pass on any errors originating from stubtest or mypy
268 # These can occur expectedly, e.g. StubtestFailure
269 if bottom_module == "__main__" or bottom_module.split(".")[0] == "mypy":
270 raise
271 yield Error(
272 [module_name],
273 f"encountered unexpected error, {type(e).__name__}: {e}",
274 stub,
275 runtime,
276 stub_desc="N/A",
277 runtime_desc=(
278 "This is most likely the fault of something very dynamic in your library. "
279 "It's also possible this is a bug in stubtest.\nIf in doubt, please "
280 "open an issue at https://github.com/python/mypy\n\n"
281 + traceback.format_exc().strip()
282 ),
283 )
284
285
286@singledispatch

Callers 1

test_stubsFunction · 0.85

Calls 12

get_stubFunction · 0.85
is_probably_privateFunction · 0.85
reprFunction · 0.85
ErrorClass · 0.85
silent_import_moduleFunction · 0.85
isinstanceFunction · 0.85
typeClass · 0.85
verifyFunction · 0.85
listClass · 0.85
splitMethod · 0.80
stripMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…