Generator of dynamically typed draft stubs for arbitrary modules.
The logic of this script can be split in three steps: * parsing options and finding sources:
- use runtime imports be default (to find also C modules)
- or use mypy's mechanisms, if importing is prohibited
During first and third steps some problematic files can be skipped, but any blocking error during second step will cause the whole program to stop.
Basic usage:
$ stubgen foo.py bar.py some_directory => Generate out/foo.pyi, out/bar.pyi, and stubs for some_directory (recursively).
$ stubgen -m urllib.parse => Generate out/urllib/parse.pyi.
$ stubgen -p urllib => Generate stubs for whole urlib package (recursively).
For Python 2 mode, use --py2:
$ stubgen --py2 -m textwrap
For C modules, you can get more precise function signatures by parsing .rst (Sphinx) documentation for extra information. For this, use the --doc-dir option:
$ stubgen --doc-dir <DIR>/Python-3.4.2/Doc/library -m curses
Note: The generated stubs should be verified manually.
support stubs for C modules in Python 2 mode
detect 'if PY2 / is_py2' etc. and either preserve those or only include Python 2 or 3 case
maybe use .rst docs also for Python modules
maybe export more imported names if there is no __all__ (this affects ssl.SSLError, for example) - a quick and dirty heuristic would be to turn this on if a module has something like
'from x import y as _y'
we don't seem to always detect properties ('closed' in 'io', for example)
Class | AliasPrinter |
Visitor used to collect type aliases _and_ type variable definitions. |
Class | AnnotationPrinter |
Visitor used to print existing annotations in a file. |
Class | DefinitionFinder |
Find names of things defined at the top level of a module. |
Class | ImportTracker |
Record necessary imports during stub generation. |
Class | Options |
Represents stubgen options. |
Class | ReferenceFinder |
Find all name references (both local and global). |
Class | SelfTraverser |
Undocumented |
Class | StubGenerator |
Generate stub text from a mypy AST. |
Class | StubSource |
A single source for stub: can be a Python or C module. |
Function | collect_build_targets |
Collect files for which we need to generate stubs. |
Function | collect_docs_signatures |
Gather all function and class signatures in the docs. |
Function | find_defined_names |
Undocumented |
Function | find_method_names |
Undocumented |
Function | find_module_paths_using_imports |
Find path and runtime value of __all__ (if possible) for modules and packages. |
Function | find_module_paths_using_search |
Find sources for modules and packages requested. |
Function | find_referenced_names |
Undocumented |
Function | find_self_initializers |
Find attribute initializers in a method. |
Function | generate_asts_for_modules |
Use mypy to parse (and optionally analyze) source files. |
Function | generate_stub_from_ast |
Use analysed (or just parsed) AST to generate type stub for single file. |
Function | generate_stubs |
Main entry point for the program. |
Function | get_qualified_name |
Undocumented |
Function | is_blacklisted_path |
Undocumented |
Function | is_non_library_module |
Does module look like a test module or a script? |
Function | main |
Undocumented |
Function | mypy_options |
Generate mypy options using the flag passed by user. |
Function | normalize_path_separators |
Undocumented |
Function | parse_options |
Undocumented |
Function | parse_source_file |
Parse a source file. |
Function | remove_blacklisted_modules |
Undocumented |
Function | translate_module_name |
Undocumented |
Constant | BLACKLIST |
Undocumented |
Constant | CLASS |
Undocumented |
Constant | DESCRIPTION |
Undocumented |
Constant | EMPTY |
Undocumented |
Constant | EMPTY_CLASS |
Undocumented |
Constant | ERROR_MARKER |
Undocumented |
Constant | EXTRA_EXPORTED |
Undocumented |
Constant | FUNC |
Undocumented |
Constant | HEADER |
Undocumented |
Constant | IGNORED_DUNDERS |
Undocumented |
Constant | METHODS_WITH_RETURN_VALUE |
Undocumented |
Constant | NOT_IN_ALL |
Undocumented |
Constant | VAR |
Undocumented |
Constant | VENDOR_PACKAGES |
Undocumented |
Collect files for which we need to generate stubs.
Return list of Python modules and C modules.
Parameters | |
options:Options | Undocumented |
mypy_opts:MypyOptions | Undocumented |
Returns | |
Tuple[ | Undocumented |
Gather all function and class signatures in the docs.
Return a tuple (function signatures, class signatures). Currently only used for C modules.
Parameters | |
doc_dir:str | Undocumented |
Returns | |
Tuple[ | Undocumented |
Find path and runtime value of __all__ (if possible) for modules and packages.
This function uses runtime Python imports to get the information.
Parameters | |
modules:List[ | Undocumented |
packages:List[ | Undocumented |
interpreter:str | Undocumented |
pyversion:Tuple[ | Undocumented |
verbose:bool | Undocumented |
quiet:bool | Undocumented |
Returns | |
Tuple[ | Undocumented |
Find sources for modules and packages requested.
This function just looks for source files at the file system level. This is used if user passes --no-import, and will not find C modules. Exit if some of the modules or packages can't be found.
Parameters | |
modules:List[ | Undocumented |
packages:List[ | Undocumented |
search_path:List[ | Undocumented |
pyversion:Tuple[ | Undocumented |
Returns | |
List[ | Undocumented |
Find attribute initializers in a method.
Return a list of pairs (attribute name, r.h.s. expression).
Parameters | |
fdef:FuncBase | Undocumented |
Returns | |
List[ | Undocumented |
Parameters | |
py_modules:List[ | Undocumented |
parse_only:bool | Undocumented |
mypy_options:MypyOptions | Undocumented |
verbose:bool | Undocumented |
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.
Parameters | |
mod:StubSource | Undocumented |
target:str | Undocumented |
parse_only:bool | Undocumented |
pyversion:Tuple[ | Undocumented |
include_private:bool | Undocumented |
export_less:bool | Undocumented |
Parameters | |
module:str | Undocumented |
Returns | |
bool | Undocumented |
Parameters | |
stubgen_options:Options | Undocumented |
Returns | |
MypyOptions | Undocumented |
Parse a source file.
On success, store AST in the corresponding attribute of the stub source. If there are syntax errors, print them and exit.
Parameters | |
mod:StubSource | Undocumented |
mypy_options:MypyOptions | Undocumented |
Undocumented
Parameters | |
module:str | Undocumented |
relative:int | Undocumented |
Returns | |
Tuple[ | Undocumented |
Undocumented
Value |
|
Undocumented
Value |
|
Undocumented
Value |
|
Undocumented
Value |
|