A Pytest Plugin for Mypy static type checking integration
—
Comprehensive command-line configuration for controlling mypy behavior within pytest test runs. These options provide fine-grained control over type checking execution, error reporting, and failure handling.
The primary flag to activate mypy checking during pytest execution.
def pytest_addoption(parser: pytest.Parser) -> None:
"""
Add mypy command line options to pytest parser.
Registers the following options:
--mypy: Enable mypy type checking
--mypy-ignore-missing-imports: Suppress import resolution errors
--mypy-config-file: Specify custom mypy configuration file
--mypy-report-style: Choose error output format
--mypy-no-status-check: Ignore mypy exit status
--mypy-xfail: Mark mypy errors as expected failures
"""Control how mypy handles missing or unresolvable imports during type checking.
--mypy-ignore-missing-imports
--ignore-missing-imports optionUsage example:
pytest --mypy --mypy-ignore-missing-imports src/Specify a custom mypy configuration file instead of using the default mypy.ini.
--mypy-config-file PATH
Usage example:
pytest --mypy --mypy-config-file ./custom-mypy.ini src/Control the format and verbosity of mypy error output within pytest reports.
--mypy-report-style STYLE
Available styles:
mypy: Preserve original mypy output format with full pathsno-path (default): Strip path prefixes from error messages for cleaner outputUsage example:
pytest --mypy --mypy-report-style=mypy src/Control whether pytest validates mypy's exit status in addition to per-file errors.
--mypy-no-status-check
Usage example:
pytest --mypy --mypy-no-status-check src/Mark mypy errors as expected failures instead of test failures.
--mypy-xfail
Usage example:
pytest --mypy --mypy-xfail src/# Enable mypy checking on Python files
pytest --mypy src/
# Check only mypy tests using markers
pytest --mypy -m mypy src/# Comprehensive mypy checking with custom settings
pytest --mypy \
--mypy-ignore-missing-imports \
--mypy-config-file ./strict-mypy.ini \
--mypy-report-style=no-path \
src/# Allow mypy errors without failing tests
pytest --mypy --mypy-xfail src/
# Ignore status check for partial typing adoption
pytest --mypy --mypy-no-status-check src/Install with Tessl CLI
npx tessl i tessl/pypi-pytest-mypy