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

Function build_stubs

mypy/stubtest.py:2166–2224  ·  view source on GitHub ↗

Uses mypy to construct stub objects for the given modules. This sets global state that ``get_stub`` can access. Returns all modules we might want to check. If ``find_submodules`` is False, this is equal to ``modules``. :param modules: List of modules to build stubs for. :param

(modules: list[str], options: Options, find_submodules: bool = False)

Source from the content-addressed store, hash-verified

2164
2165
2166def build_stubs(modules: list[str], options: Options, find_submodules: bool = False) -> list[str]:
2167 """Uses mypy to construct stub objects for the given modules.
2168
2169 This sets global state that ``get_stub`` can access.
2170
2171 Returns all modules we might want to check. If ``find_submodules`` is False, this is equal
2172 to ``modules``.
2173
2174 :param modules: List of modules to build stubs for.
2175 :param options: Mypy options for finding and building stubs.
2176 :param find_submodules: Whether to attempt to find submodules of the given modules as well.
2177
2178 """
2179 data_dir = mypy.build.default_data_dir()
2180 search_path = mypy.modulefinder.compute_search_paths([], options, data_dir)
2181 find_module_cache = mypy.modulefinder.FindModuleCache(
2182 search_path, fscache=None, options=options
2183 )
2184
2185 all_modules = []
2186 sources = []
2187 for module in modules:
2188 all_modules.append(module)
2189 if not find_submodules:
2190 module_path = find_module_cache.find_module(module)
2191 if not isinstance(module_path, str):
2192 # test_module will yield an error later when it can't find stubs
2193 continue
2194 sources.append(mypy.modulefinder.BuildSource(module_path, module, None))
2195 else:
2196 found_sources = find_module_cache.find_modules_recursive(module)
2197 sources.extend(found_sources)
2198 # find submodules via mypy
2199 all_modules.extend(s.module for s in found_sources if s.module not in all_modules)
2200 # find submodules via pkgutil
2201 try:
2202 runtime = silent_import_module(module)
2203 all_modules.extend(
2204 m.name
2205 for m in pkgutil.walk_packages(runtime.__path__, runtime.__name__ + ".")
2206 if m.name not in all_modules
2207 )
2208 except KeyboardInterrupt:
2209 raise
2210 except BaseException:
2211 pass
2212
2213 if sources:
2214 try:
2215 res = mypy.build.build(sources=sources, options=options)
2216 except mypy.errors.CompileError as e:
2217 raise StubtestFailure(f"failed mypy compile:\n{e}") from e
2218 if res.errors:
2219 raise StubtestFailure("mypy build errors:\n" + "\n".join(res.errors))
2220
2221 global _all_stubs
2222 _all_stubs = res.files
2223

Callers 1

test_stubsFunction · 0.85

Calls 9

find_moduleMethod · 0.95
isinstanceFunction · 0.85
silent_import_moduleFunction · 0.85
StubtestFailureClass · 0.85
appendMethod · 0.80
extendMethod · 0.80
buildMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…