Skip to content

tests ¤

Test helpers and pytest fixtures.

Classes:

Functions:

TmpPackage dataclass ¤

TmpPackage(tmpdir: Path, name: str, path: Path)

A temporary package.

Attributes:

  • name (str) –

    The package name, as to dynamically import it.

  • path (Path) –

    The package path.

  • tmpdir (Path) –

    The temporary directory containing the package.

name instance-attribute ¤

name: str

The package name, as to dynamically import it.

path instance-attribute ¤

path: Path

The package path.

tmpdir instance-attribute ¤

tmpdir: Path

The temporary directory containing the package.

htree ¤

htree(*objects: Object) -> Object

Link objects together, horizontally.

Parameters:

  • *objects (Object, default: () ) –

    A sequence of objects. All objects starting at the second become members of the first.

Raises:

Returns:

  • Object

    The first given object, with all the other objects as members of it.

module_vtree ¤

module_vtree(
    path: str,
    *,
    leaf_package: bool = True,
    return_leaf: bool = False
) -> Module

Link objects together, vertically.

Parameters:

  • path (str) –

    The complete module path, like "a.b.c.d".

  • leaf_package (bool, default: True ) –

    Whether the deepest module should also be a package.

  • return_leaf (bool, default: False ) –

    Whether to return the leaf instead of the root.

Raises:

Returns:

  • Module

    The top or leaf module.

temporary_inspected_module ¤

temporary_inspected_module(
    code: str,
    *,
    module_name: str = "module",
    import_paths: list[Path] | None = None,
    extensions: Extensions | None = None,
    parent: Module | None = None,
    docstring_parser: Parser | None = None,
    docstring_options: dict[str, Any] | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None
) -> Iterator[Module]

Create and inspect a temporary module with the given code.

Parameters:

  • code (str) –

    The code of the module.

  • module_name (str, default: 'module' ) –

    The name of the temporary module.

  • import_paths (list[Path] | None, default: None ) –

    Paths to import the module from.

  • extensions (Extensions | None, default: None ) –

    The extensions to use when visiting the AST.

  • parent (Module | None, default: None ) –

    The optional parent of this module.

  • docstring_parser (Parser | None, default: None ) –

    The docstring parser to use. By default, no parsing is done.

  • docstring_options (dict[str, Any] | None, default: None ) –

    Additional docstring parsing options.

  • lines_collection (LinesCollection | None, default: None ) –

    A collection of source code lines.

  • modules_collection (ModulesCollection | None, default: None ) –

    A collection of modules.

Yields:

  • Module

    The inspected module.

temporary_pyfile ¤

temporary_pyfile(
    code: str, *, module_name: str = "module"
) -> Iterator[tuple[str, Path]]

Create a Python file containing the given code in a temporary directory.

Parameters:

  • code (str) –

    The code to write to the temporary file.

  • module_name (str, default: 'module' ) –

    The name of the temporary module.

Yields:

  • module_name ( str ) –

    The module name, as to dynamically import it.

  • module_path ( Path ) –

    The module path.

temporary_pypackage ¤

temporary_pypackage(
    package: str,
    modules: (
        Sequence[str] | Mapping[str, str] | None
    ) = None,
    *,
    init: bool = True,
    inits: bool = True
) -> Iterator[TmpPackage]

Create a package containing the given modules in a temporary directory.

Parameters:

  • package (str) –

    The package name. Example: "a" gives a package named a, while "a/b" gives a namespace package named a with a package inside named b. If init is false, then b is also a namespace package.

  • modules (Sequence[str] | Mapping[str, str] | None, default: None ) –

    Additional modules to create in the package. If a list, simply touch the files: ["b.py", "c/d.py", "e/f"]. If a dict, keys are the file names and values their contents: {"b.py": "b = 1", "c/d.py": "print('hey from c')"}.

  • init (bool, default: True ) –

    Whether to create an __init__ module in the top package.

  • inits (bool, default: True ) –

    Whether to create __init__ modules in subpackages.

Yields:

temporary_visited_module ¤

temporary_visited_module(
    code: str,
    *,
    module_name: str = "module",
    extensions: Extensions | None = None,
    parent: Module | None = None,
    docstring_parser: Parser | None = None,
    docstring_options: dict[str, Any] | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None
) -> Iterator[Module]

Create and visit a temporary module with the given code.

Parameters:

  • code (str) –

    The code of the module.

  • module_name (str, default: 'module' ) –

    The name of the temporary module.

  • extensions (Extensions | None, default: None ) –

    The extensions to use when visiting the AST.

  • parent (Module | None, default: None ) –

    The optional parent of this module.

  • docstring_parser (Parser | None, default: None ) –

    The docstring parser to use. By default, no parsing is done.

  • docstring_options (dict[str, Any] | None, default: None ) –

    Additional docstring parsing options.

  • lines_collection (LinesCollection | None, default: None ) –

    A collection of source code lines.

  • modules_collection (ModulesCollection | None, default: None ) –

    A collection of modules.

Yields:

  • Module

    The visited module.

temporary_visited_package ¤

temporary_visited_package(
    package: str,
    modules: (
        Sequence[str] | Mapping[str, str] | None
    ) = None,
    *,
    init: bool = True,
    extensions: Extensions | None = None,
    docstring_parser: Parser | None = None,
    docstring_options: dict[str, Any] | None = None,
    lines_collection: LinesCollection | None = None,
    modules_collection: ModulesCollection | None = None,
    allow_inspection: bool = False,
    store_source: bool = True
) -> Iterator[Module]

Create and visit a temporary package.

Parameters:

  • package (str) –

    The package name. Example: "a" gives a package named a, while "a/b" gives a namespace package named a with a package inside named b. If init is false, then b is also a namespace package.

  • modules (Sequence[str] | Mapping[str, str] | None, default: None ) –

    Additional modules to create in the package. If a list, simply touch the files: ["b.py", "c/d.py", "e/f"]. If a dict, keys are the file names and values their contents: {"b.py": "b = 1", "c/d.py": "print('hey from c')"}.

  • init (bool, default: True ) –

    Whether to create an __init__ module in the leaf package.

  • extensions (Extensions | None, default: None ) –

    The extensions to use.

  • docstring_parser (Parser | None, default: None ) –

    The docstring parser to use. By default, no parsing is done.

  • docstring_options (dict[str, Any] | None, default: None ) –

    Additional docstring parsing options.

  • lines_collection (LinesCollection | None, default: None ) –

    A collection of source code lines.

  • modules_collection (ModulesCollection | None, default: None ) –

    A collection of modules.

  • allow_inspection (bool, default: False ) –

    Whether to allow inspecting modules when visiting them is not possible.

  • store_source (bool, default: True ) –

    Whether to store code source in the lines collection.

Yields:

vtree ¤

vtree(
    *objects: Object, return_leaf: bool = False
) -> Object

Link objects together, vertically.

Parameters:

  • *objects (Object, default: () ) –

    A sequence of objects. The first one is at the top of the tree.

  • return_leaf (bool, default: False ) –

    Whether to return the leaf instead of the root.

Raises:

Returns:

  • Object

    The top or leaf object.