A tool that extracts doctests from .pyi stub files and runs them with pytest --doctest-modules.
This tool is designed for:
- Stub file testing: Validate examples in
.pyitype stub files - Non-Python codebases: Test doctests for Cython/PyO3/Rust extensions where the implementation isn't in Python
- Third-party stubs: Verify examples in type stubs for external libraries
Note: For regular Python code, always write doctests directly in your
.pyimplementation files, even if you have separate.pyistub files.
- Discovers
.pyifiles in your project - Extracts doctests from docstrings
- Generates temporary test files with proper source mapping
- Executes tests with
pytest --doctest-modules - Reports failures with accurate line numbers pointing to source files
- Cleans up temporary files automatically
uv add git+https://github.com/OutSquareCapital/stubtester.git# Run on all .pyi files in a directory
uv run stubtester path/to/your/package
# Run on a single file
uv run stubtester path/to/file.pyi
# Keep temporary files for debugging
uv run stubtester path/to/file.pyi --keepfrom pathlib import Path
import stubtester
# Test a directory (discovers all .pyi and .md files)
result = stubtester.run(Path("my_package"))
# Test a single file
result = stubtester.run(Path("my_package/module.pyi"))
# Keep temporary files for debugging
result = stubtester.run(Path("my_package"), keep=True)
# Handle results
match result:
case stubtester.Ok(message):
print(f"✓ {message}")
case stubtester.Err(error):
print(f"✗ {error}")Your stub file might look like this:
# math_helpers.pyi
def add(a: int, b: int) -> int:
"""Add two numbers.
>>> add(2, 3)
5
>>> add(-1, 1)
0
"""
When run with stubtester, doctests are extracted and executed.
--keep: Keep temporary test files for debugging (stored indoctests_temp/)--help: Display help information
Contributions welcome! Please ensure all tests pass:
uv run pytest tests/
uv run stubtester tests/examples/success/ # This project verifies itself!