Skip to content

docspec-python

load_python_modules

def load_python_modules(
    modules: t.Optional[t.Sequence[str]] = None,
    packages: t.Optional[t.Sequence[str]] = None,
    search_path: t.Optional[t.Sequence[t.Union[str, Path]]] = None,
    options: t.Optional[ParserOptions] = None,
    raise_: bool = True,
    encoding: t.Optional[str] = None,
    *,
    files: t.Optional[t.Sequence[t.Tuple[str,
                                         str]]] = None) -> t.Iterable[Module]

[view_source]

Utility function for loading multiple Modules from a list of Python module and package names. It combines find_module(), iter_package_files() and parse_python_module() in a convenient way.

Arguments

  • modules: A list of module names to load and parse.
  • packages: A list of package names to load and parse.
  • search_path: The Python module search path. Falls back to sys.path if omitted.
  • options: Options for the Python module parser.
  • files: A list of (module_name, filename) tuples to parse.

Returns

Iterable of Module.

parse_python_module

def parse_python_module(fp: t.Union[str, Path, t.TextIO],
                        filename: t.Union[str, Path, None] = None,
                        module_name: t.Optional[str] = None,
                        options: t.Optional[ParserOptions] = None,
                        encoding: t.Optional[str] = None) -> Module

[view_source]

Parses Python code of a file or file-like object and returns a Module object with the contents of the file The options are forwarded to the {@link pydoc:docspec_python.parser.Parser} constructor.

find_module

def find_module(
        module_name: str,
        search_path: t.Optional[t.Sequence[t.Union[str, Path]]] = None) -> str

[view_source]

Finds the filename of a module that can be parsed with parse_python_module(). If search_path is not set,

the default sys.path is used to search for the module. If module_name is a Python package, it will return the path to the package's __init__.py file. If the module does not exist, an ImportError is raised. This is also true for PEP 420 namespace packages that do not provide an __init__.py file.

Raises:

  • ImportError: If the module cannot be found.

iter_package_files

def iter_package_files(
    package_name: str,
    search_path: t.Optional[t.Sequence[t.Union[str, Path]]] = None
) -> t.Iterable[t.Tuple[str, str]]

[view_source]

Returns an iterator for the Python source files in the specified package. The items returned by the iterator are tuples of the module name and filename. Supports a PEP 420 namespace package if at least one matching directory with at least one Python source file in it is found.

discover

def discover(directory: t.Union[str, Path]) -> t.Iterable[DiscoveryResult]

[view_source]

Discovers Python modules and packages in the specified directory. The returned generated

returns tuples where the first element of the tuple is the type (either 'module' or 'package'), the second is the name and the third is the path. In case of a package, the path points to the directory.

Raises:

  • OSError: Propagated from os.listdir().

format_arglist

def format_arglist(args: t.Sequence[Argument],
                   render_type_hints: bool = True) -> str

[view_source]

Formats a Python argument list.