{"id":5430,"library":"pytest-mypy","title":"Pytest Mypy Plugin","description":"pytest-mypy is a pytest plugin that integrates the Mypy static type checker into your pytest test runs. It allows you to run Mypy checks on your Python source files as part of your existing test suite, similar to how pytest-flake8 works for Flake8. The current version is 1.0.1, and it is actively maintained, with releases as needed.","status":"active","version":"1.0.1","language":"en","source_language":"en","source_url":"https://github.com/realpython/pytest-mypy","tags":["pytest","mypy","type checking","static analysis","testing","plugin"],"install":[{"cmd":"pip install pytest-mypy","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for pytest plugin functionality.","package":"pytest"},{"reason":"The static type checker that this plugin integrates.","package":"mypy"}],"imports":[{"note":"The plugin is activated via the '--mypy' CLI flag or configured through conftest.py.","symbol":"pytest-mypy","correct":"This is a pytest plugin and is automatically discovered upon installation; no explicit import statement is typically needed in your test files."}],"quickstart":{"code":"# my_project/my_module.py\ndef add_numbers(a: int, b: int) -> int:\n    return a + b\n\ndef incorrect_call():\n    # Mypy should flag this type mismatch\n    return add_numbers('hello', 5)\n\n# my_project/test_example.py (empty or with other tests)\n# This file just needs to exist for pytest to discover the project.\n\n# To run mypy with pytest (from my_project directory):\n# pip install pytest pytest-mypy mypy\n# pytest --mypy my_module.py\n\n# Example conftest.py to add extra mypy arguments (e.g., --strict):\n# # my_project/conftest.py\n# def pytest_configure(config):\n#     plugin = config.pluginmanager.getplugin('mypy')\n#     if plugin:\n#         # Example: make mypy run in strict mode\n#         plugin.mypy_argv.append('--strict')\n#         # Example: check untyped definitions\n#         plugin.mypy_argv.append('--disallow-untyped-defs')","lang":"python","description":"After installing `pytest-mypy`, enable it by passing the `--mypy` flag to pytest, specifying the files or directories you want to type check. For more granular control or to enforce stricter Mypy settings, you can add options to `plugin.mypy_argv` within a `pytest_configure` hook in your `conftest.py` file."},"warnings":[{"fix":"Always aim for strict Mypy settings in CI/CD pipelines (e.g., using `--strict`). Review your `mypy.ini` or `pyproject.toml` configuration to ensure no critical errors are being ignored. Consider using `plugin.mypy_argv.append('--strict')` in `conftest.py` if configuring via pytest.","message":"Mypy configuration flags like `--ignore-missing-imports` or not using strict checking (`--strict`, `--disallow-untyped-defs`) can cause Mypy to silently miss type errors. Users might mistakenly believe their code is fully type-checked when issues are being suppressed.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To resolve pathing issues, use absolute paths for `MYPYPATH`/`PYTHONPATH` or instruct `pytest-mypy` to run Mypy in the same process using the `--mypy-same-process` flag.","message":"When Mypy is run as a subprocess (the default behavior of `pytest-mypy`), relative paths specified in environment variables like `MYPYPATH` or `PYTHONPATH` may not resolve correctly because pytest tests can run in temporary working directories.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To ensure comprehensive type checking, explicitly add type annotations to your functions. Alternatively, configure Mypy with flags like `--disallow-untyped-defs` (to report errors for untyped function definitions) or use `--strict` mode, which enables a comprehensive set of strict checks.","message":"By default, Mypy does not fully check functions without type annotations, and `Any` types can propagate through your code, masking potential type errors. This can lead to false negatives in your type checking results.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}