{"id":7617,"library":"pytest-mypy-plugins","title":"pytest-mypy-plugins","description":"pytest-mypy-plugins is a pytest plugin designed for writing tests for mypy plugins, stubs, and type definitions. It allows developers to verify the correctness of their type annotations and mypy plugin behavior directly within their pytest test suite. The library is actively maintained, with releases as needed, and the current version is 4.0.1.","status":"active","version":"4.0.1","language":"en","source_language":"en","source_url":"https://github.com/TypedDjango/pytest-mypy-plugins","tags":["pytest","mypy","testing","type checking","plugins","static analysis"],"install":[{"cmd":"pip install pytest-mypy-plugins","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"This is a pytest plugin and integrates with pytest's test discovery and execution. Requires pytest>=7.0.0.","package":"pytest","optional":false},{"reason":"The plugin tests mypy types, stubs, and plugins. It integrates directly with the mypy type checker. Supports mypy@1.8.0 and later versions.","package":"mypy"}],"imports":[{"note":"pytest plugins are typically auto-discovered upon installation. For advanced configuration, one might access the plugin via `config.pluginmanager.getplugin('mypy')` in `conftest.py`.","symbol":"pytest-mypy-plugins","correct":"No explicit import usually needed, automatically discovered by pytest."}],"quickstart":{"code":"# Save as my_module.py\ndef add(a: int, b: int) -> int:\n    return a + b\n\n# Save as test_types.yml in a directory like 'typesafety/'\n- case: correct_addition\n  main: |\n    from my_module import add\n    result = add(1, 2)\n    reveal_type(result) # N: Revealed type is 'builtins.int'\n\n- case: incorrect_addition_type\n  main: |\n    from my_module import add\n    add(1, '2') # E: Argument 2 to \"add\" has incompatible type \"str\"; expected \"int\" [arg-type]\n\n# To run tests, simply execute pytest in your terminal from the project root:\n# pytest","lang":"python","description":"Create a Python file (`my_module.py`) with code to be type-checked, and a YAML file (`test_types.yml`) defining test cases. Each `case` block in the YAML file contains `main` (Python code) and uses `# N:` for expected `reveal_type` output or `# E:` for expected mypy error messages. The plugin automatically discovers and runs these tests when `pytest` is executed."},"warnings":[{"fix":"The `--mypy-only-local-stub` flag was removed. To restore the previous behavior of checking errors in site-packages, use the new `--mypy-no-silence-site-packages` flag.","message":"Errors are now ignored in site-packages by default starting from version 4.0.0.","severity":"breaking","affected_versions":"4.0.0 and later"},{"fix":"Ensure your test environment uses Python 3.9 or newer. Python 3.13 support was officially added in 3.3.0.","message":"Python 3.8 support was dropped in versions 3.2.0 and 3.3.0.","severity":"breaking","affected_versions":"3.2.0, 3.3.0 and later"},{"fix":"Upgrade your Python environment to Python 3.8 or newer. The minimum Python requirement for the latest versions is `>=3.10`.","message":"Python 3.7 support was dropped starting from version 3.0.0.","severity":"breaking","affected_versions":"3.0.0 and later"},{"fix":"Either invoke `pytest` with `--mypy-same-process` to run `mypy` in the same process, or use absolute paths for `PYTHONPATH` and `MYPYPATH` to ensure correct referencing.","message":"Relative paths in `PYTHONPATH` or `MYPYPATH` environment variables may not work as expected when `mypy` runs in a subprocess (the default mode for the plugin).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure all relevant functions have type annotations. For wider checks, configure mypy to use `--check-untyped-defs`. If `Any` is the issue, investigate missing library stubs or remove `--ignore-missing-imports` to expose hidden import errors.","cause":"Mypy, by default, might not type-check functions without any type annotations, or it might infer `Any` types due to missing stubs or the `--ignore-missing-imports` flag, silently passing errors.","error":"No errors reported for obviously wrong code"},{"fix":"Review your `mypy` configuration (e.g., `mypy.ini` or `pyproject.toml`) to ensure error reporting is not suppressed. Remove `ignore_errors = True` or similar options that prevent mypy from failing. Consider removing or narrowing the scope of `--ignore-missing-imports`.","cause":"This can happen if `mypy`'s configuration (e.g., in `mypy.ini` or `pyproject.toml`) contains settings like `ignore_errors = True` or `warn_unreachable = False`, which suppress error reporting. Or, if `--ignore-missing-imports` is broadly used.","error":"Mypy errors are not causing pytest to fail as expected, even with `--mypy` flag."},{"fix":"To resolve this, either configure `pytest-mypy-plugins` to run `mypy` in the same process using the `--mypy-same-process` pytest CLI option, or ensure that any paths specified in `MYPYPATH` or `PYTHONPATH` are absolute paths (e.g., using `$(pwd)/my_plugin` in a shell script).","cause":"When `mypy` runs as a subprocess (the default behavior for `pytest-mypy-plugins`), its working directory might differ from where `pytest` was invoked. This causes relative paths in environment variables to resolve incorrectly.","error":"Cannot import module X when using relative paths in MYPYPATH/PYTHONPATH within pytest-mypy-plugins tests."}]}