|
| 1 | +"""Tests for mypy type checking. |
| 2 | +
|
| 3 | +This module validates that type checking via mypy passes without errors. |
| 4 | +Mypy is run directly (not through hatch or pre-commit) to ensure consistent |
| 5 | +error reporting and prevent errors from being swallowed by tool configuration. |
| 6 | +""" |
| 7 | + |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +try: |
| 13 | + import tomllib |
| 14 | +except ImportError: |
| 15 | + import tomli as tomllib # type: ignore |
| 16 | + |
| 17 | + |
| 18 | +def test_mypy_not_in_hatch_config() -> None: |
| 19 | + """Verify that mypy is not configured in any hatch environment. |
| 20 | +
|
| 21 | + Mypy should be run directly (not through hatch) because hatch's |
| 22 | + automatic dependency installation and environment isolation can mask |
| 23 | + type errors. When dependencies are installed automatically, mypy |
| 24 | + behaves differently and errors get swallowed. |
| 25 | +
|
| 26 | + This test ensures mypy doesn't accidentally get added back to hatch |
| 27 | + environments where it would be misconfigured. |
| 28 | + """ |
| 29 | + project_root = Path(__file__).parent.parent |
| 30 | + pyproject_path = project_root / "pyproject.toml" |
| 31 | + |
| 32 | + with open(pyproject_path, "rb") as f: |
| 33 | + config = tomllib.load(f) |
| 34 | + |
| 35 | + # Check all hatch environment configurations |
| 36 | + hatch_config = config.get("tool", {}).get("hatch", {}) |
| 37 | + envs = hatch_config.get("envs", {}) |
| 38 | + |
| 39 | + mypy_found_in = [] |
| 40 | + |
| 41 | + for env_name, env_config in envs.items(): |
| 42 | + if not isinstance(env_config, dict): |
| 43 | + continue |
| 44 | + |
| 45 | + # Check dependencies |
| 46 | + deps = env_config.get("dependencies", []) |
| 47 | + if isinstance(deps, list): |
| 48 | + for dep in deps: |
| 49 | + if isinstance(dep, str) and "mypy" in dep.lower(): |
| 50 | + mypy_found_in.append(f"envs.{env_name}.dependencies") |
| 51 | + |
| 52 | + # Check scripts |
| 53 | + scripts = env_config.get("scripts", {}) |
| 54 | + if isinstance(scripts, dict): |
| 55 | + for script_name, script_content in scripts.items(): |
| 56 | + if isinstance(script_content, str) and "mypy" in script_content: |
| 57 | + mypy_found_in.append(f"envs.{env_name}.scripts.{script_name}") |
| 58 | + elif isinstance(script_content, list): |
| 59 | + for i, cmd in enumerate(script_content): |
| 60 | + if isinstance(cmd, str) and "mypy" in cmd: |
| 61 | + mypy_found_in.append(f"envs.{env_name}.scripts.{script_name}[{i}]") |
| 62 | + |
| 63 | + if mypy_found_in: |
| 64 | + error_msg = ( |
| 65 | + "MyPy should not be configured in any hatch environment section. " |
| 66 | + "It should be run directly to ensure consistent error reporting.\n\n" |
| 67 | + "Found mypy in the following sections:\n" |
| 68 | + + "\n".join(f" - [tool.hatch.{section}]" for section in mypy_found_in) |
| 69 | + + "\n\nRun mypy directly instead:\n" |
| 70 | + " python -m mypy" |
| 71 | + ) |
| 72 | + raise AssertionError(error_msg) |
0 commit comments