✅ all_doctests#

This module is used to run all the doctests for all the modules in a given directory.

# Create a package whose docstring holds two tests, the second one wrong
mkdir my_package
cat > my_package/geometry.py <<'EOF'
def area(width: float, height: float) -> float:
    ''' Area of a rectangle.

    >>> area(2, 3)
    6
    >>> area(2.5, 2)
    5
    '''
    return width * height
EOF

# Run every doctest of the package
stouputils all_doctests my_package
Terminal output of the example, one doctest failing

launch#

launch_tests(
root_dir: str,
strict: bool = True,
pattern: str = '*',
) → int[source]#

Main function to launch tests for all modules in the given directory.

Parameters:
  • root_dir – Root directory to search for modules

  • strict – Modify the FORCE_RAISE_EXCEPTION configuration to True

  • pattern – Pattern to filter module names (fnmatch style, e.g., ‘typ’, ‘io’, etc.)

Returns:

The number of failed tests

>>> launch_tests("unknown_dir")
Traceback (most recent call last):
    ...
ValueError: No modules found in 'unknown_dir'
> if launch_tests("/path/to/source") > 0:
    sys.exit(1)
[PROGRESS HH:MM:SS] Importing module 'module1'  took 0.001s
[PROGRESS HH:MM:SS] Importing module 'module2'  took 0.002s
[PROGRESS HH:MM:SS] Importing module 'module3'  took 0.003s
[PROGRESS HH:MM:SS] Importing module 'module4'  took 0.004s
[INFO HH:MM:SS] Testing 4 modules...
[PROGRESS HH:MM:SS] Testing module 'module1'    took 0.005s
[PROGRESS HH:MM:SS] Testing module 'module2'    took 0.006s
[PROGRESS HH:MM:SS] Testing module 'module3'    took 0.007s
[PROGRESS HH:MM:SS] Testing module 'module4'    took 0.008s
find_modules(root_dir: str, dir_to_check: str) → list[str][source]#

Dotted names of the modules under root_dir, the shallowest first and then in alphabetical order.

Parameters:

dir_to_check – Folder the names are relative to, the one put on sys.path.

Raises:

ValueError – If root_dir holds no module.

filter_modules(
module_paths: list[str],
pattern: str,
root_dir: str,
) → list[str][source]#

The module names matching an fnmatch pattern, "*" keeping them all.

>>> filter_modules(["pkg.io", "pkg.io.csv", "pkg.typing"], "*io*", "pkg")
['pkg.io', 'pkg.io.csv']
Raises:

ValueError – If no module matches, naming the candidates.

import_modules(
module_paths: list[str],
) → tuple[list[ModuleType], list[str]][source]#

Import each module, timing it, and warn about the ones that fail.

Returns:

The imported modules, and for each one the spaces that align its timing line with the longest name.

report_results(
modules: list[ModuleType],
results: list[TestResults],
) → int[source]#

Print the modules whose tests failed and the totals across all of them.

Returns:

The number of failed tests.

reexports#

Consistency check for packages that re-export their submodules explicitly.

Explicit re-exports are what makes PEP 810 lazy imports possible, since a star import resolves every deferred name at once. The cost is that a new public function is easy to forget in the parent package, so this module compares each submodule against the package that re-exports it.

module_public_names(module: ModuleType) → list[str][source]#

List the public names a module defines itself, ignoring anything it merely imported.

Only objects carrying a __module__ are considered, so plain constants are out of scope.

Parameters:

module – Module to inspect

Returns:

Names defined by that module, sorted

>>> from stouputils.all_doctests import reexports
>>> module_public_names(reexports)
['find_missing_reexports', 'module_public_names']
find_missing_reexports(
package: ModuleType,
) → dict[str, list[str]][source]#

Find names a submodule defines that its parent package does not expose at all.

A submodule the parent exposes nothing from counts as deliberately internal and is skipped, which is how modules such as stouputils.config stay out of the flat namespace. A name the parent already binds to a sibling’s definition is a plain naming clash, not a missing re-export, so it is not reported here either. Submodules that cannot be imported are skipped, since optional dependencies may be absent.

Parameters:

package – Root package to walk

Returns:

Missing names, keyed by the submodule that defines them

>>> import stouputils
>>> find_missing_reexports(stouputils)
{}

utils#

This module is used to run all the doctests for all the modules in a given directory.

stouputils all_doctests examples
test_module_with_progress(
module: ModuleType,
separator: str,
) → TestResults[source]#

Test a module with testmod and measure the time taken with progress printing.

Parameters:
  • module – Module to test

  • separator – Separator string for alignment in output

Returns:

The results of the tests