MCPcopy Index your code
hub / github.com/python/mypy / generate_stubs

Function generate_stubs

mypy/stubgen.py:1841–1896  ·  view source on GitHub ↗

Main entry point for the program.

(options: Options)

Source from the content-addressed store, hash-verified

1839
1840
1841def generate_stubs(options: Options) -> None:
1842 """Main entry point for the program."""
1843 mypy_opts = mypy_options(options)
1844 py_modules, pyc_modules, c_modules = collect_build_targets(options, mypy_opts)
1845 all_modules = py_modules + pyc_modules + c_modules
1846 all_module_names = sorted(m.module for m in all_modules)
1847 # Use parsed sources to generate stubs for Python modules.
1848 generate_asts_for_modules(py_modules, options.parse_only, mypy_opts, options.verbose)
1849 files = []
1850 for mod in py_modules + pyc_modules:
1851 assert mod.path is not None, "Not found module was not skipped"
1852 target = mod.module.replace(".", "/")
1853 if os.path.basename(mod.path) in ["__init__.py", "__init__.pyc"]:
1854 target += "/__init__.pyi"
1855 else:
1856 target += ".pyi"
1857 target = os.path.join(options.output_dir, target)
1858 files.append(target)
1859 with generate_guarded(mod.module, target, options.ignore_errors, options.verbose):
1860 generate_stub_for_py_module(
1861 mod,
1862 target,
1863 parse_only=options.parse_only,
1864 inspect=options.inspect or mod in pyc_modules,
1865 include_private=options.include_private,
1866 export_less=options.export_less,
1867 include_docstrings=options.include_docstrings,
1868 doc_dir=options.doc_dir,
1869 all_modules=all_module_names,
1870 )
1871
1872 # Separately analyse C modules using different logic.
1873 for mod in c_modules:
1874 if any(py_mod.module.startswith(mod.module + ".") for py_mod in all_modules):
1875 target = mod.module.replace(".", "/") + "/__init__.pyi"
1876 else:
1877 target = mod.module.replace(".", "/") + ".pyi"
1878 target = os.path.join(options.output_dir, target)
1879 files.append(target)
1880 with generate_guarded(mod.module, target, options.ignore_errors, options.verbose):
1881 generate_stub_for_c_module(
1882 mod.module,
1883 target,
1884 known_modules=all_module_names,
1885 doc_dir=options.doc_dir,
1886 include_private=options.include_private,
1887 export_less=options.export_less,
1888 include_docstrings=options.include_docstrings,
1889 )
1890 num_modules = len(all_modules)
1891 if not options.quiet and num_modules > 0:
1892 print("Processed %d modules" % num_modules)
1893 if len(files) == 1:
1894 print(f"Generated {files[0]}")
1895 else:
1896 print(f"Generated files under {common_dir_prefix(files)}" + os.sep)
1897
1898

Callers 2

run_case_innerMethod · 0.90
mainFunction · 0.85

Calls 15

generate_guardedFunction · 0.90
common_dir_prefixFunction · 0.90
mypy_optionsFunction · 0.85
collect_build_targetsFunction · 0.85
sortedFunction · 0.85
anyFunction · 0.85
lenFunction · 0.85
printFunction · 0.85
replaceMethod · 0.80

Tested by 1

run_case_innerMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…