Use analysed (or just parsed) AST to generate type stub for single file. If directory for target doesn't exist it will created. Existing stub will be overwritten.
(
mod: StubSource,
target: str,
*,
parse_only: bool = False,
inspect: bool = False,
include_private: bool = False,
export_less: bool = False,
include_docstrings: bool = False,
doc_dir: str = "",
all_modules: list[str],
)
| 1789 | |
| 1790 | |
| 1791 | def generate_stub_for_py_module( |
| 1792 | mod: StubSource, |
| 1793 | target: str, |
| 1794 | *, |
| 1795 | parse_only: bool = False, |
| 1796 | inspect: bool = False, |
| 1797 | include_private: bool = False, |
| 1798 | export_less: bool = False, |
| 1799 | include_docstrings: bool = False, |
| 1800 | doc_dir: str = "", |
| 1801 | all_modules: list[str], |
| 1802 | ) -> None: |
| 1803 | """Use analysed (or just parsed) AST to generate type stub for single file. |
| 1804 | |
| 1805 | If directory for target doesn't exist it will created. Existing stub |
| 1806 | will be overwritten. |
| 1807 | """ |
| 1808 | if inspect: |
| 1809 | ngen = InspectionStubGenerator( |
| 1810 | module_name=mod.module, |
| 1811 | known_modules=all_modules, |
| 1812 | _all_=mod.runtime_all, |
| 1813 | doc_dir=doc_dir, |
| 1814 | include_private=include_private, |
| 1815 | export_less=export_less, |
| 1816 | include_docstrings=include_docstrings, |
| 1817 | ) |
| 1818 | ngen.generate_module() |
| 1819 | output = ngen.output() |
| 1820 | |
| 1821 | else: |
| 1822 | gen = ASTStubGenerator( |
| 1823 | mod.runtime_all, |
| 1824 | include_private=include_private, |
| 1825 | analyzed=not parse_only, |
| 1826 | export_less=export_less, |
| 1827 | include_docstrings=include_docstrings, |
| 1828 | ) |
| 1829 | assert mod.ast is not None, "This function must be used only with analyzed modules" |
| 1830 | mod.ast.accept(gen) |
| 1831 | output = gen.output() |
| 1832 | |
| 1833 | # Write output to file. |
| 1834 | subdir = os.path.dirname(target) |
| 1835 | if subdir and not os.path.isdir(subdir): |
| 1836 | os.makedirs(subdir) |
| 1837 | with open(target, "w", encoding="utf-8") as file: |
| 1838 | file.write(output) |
| 1839 | |
| 1840 | |
| 1841 | def generate_stubs(options: Options) -> None: |
no test coverage detected
searching dependent graphs…