2222import os
2323import subprocess
2424import sys
25+ import venv
2526from datetime import datetime , timezone
2627from pathlib import Path
28+ from textwrap import dedent
2729from typing import Callable
2830
2931import pytest
@@ -196,7 +198,7 @@ def test_docs_build(documentation: str, generated: Callable[..., Path], use_hatc
196198 )
197199 run_command ("sphinx-apidoc -o docs/api src/alien_clones" , project )
198200 # prepend pythonpath so we don't have to actually install here...
199- run_command (f"PYTHONPATH={ str ( project / 'src' ) } sphinx-build -W -b html docs docs/_build" , project )
201+ run_command (f"PYTHONPATH={ project / 'src' !s } sphinx-build -W -b html docs docs/_build" , project )
200202
201203 run_command ("pre-commit run --all-files -v check-readthedocs" , project )
202204
@@ -229,7 +231,7 @@ def test_non_hatch_deps(
229231 project = generated (
230232 use_hatch_envs = False ,
231233 use_lint = True ,
232- use_types = True ,
234+ use_mypy = True ,
233235 use_test = True ,
234236 use_git = False ,
235237 documentation = documentation ,
@@ -254,3 +256,46 @@ def test_non_hatch_deps(
254256 if documentation != "no" :
255257 assert "docs" in optional_deps
256258 assert any (dep .startswith (documentation ) for dep in optional_deps ["docs" ])
259+
260+ @pytest .mark .parametrize ("use_hatch_envs" , [True , False ])
261+ @pytest .mark .parametrize ("valid" , [True , False ])
262+ def test_mypy (generated : Callable [..., Path ], use_hatch_envs : bool , valid : bool , tmp_path : Path ):
263+ """Mypy type checking works out of the box."""
264+ if valid :
265+ module = dedent ("""
266+ def f1(value: int, other: int = 2) -> int:
267+ return value * other
268+
269+ def f2(value: int) -> float:
270+ return f1(value, 5) / 2
271+ """ )
272+ else :
273+ module = dedent ("""
274+ def f1(value: int, other: int = 2) -> int:
275+ return value * other
276+
277+ def f2(value: str) -> str:
278+ return f1(value, 5) / 2
279+ """ )
280+
281+ root = generated (use_hatch_envs = use_hatch_envs , use_mypy = True )
282+ pkg_path = root / "src" / "alien_clones"
283+ module_path = pkg_path / "typechecking.py"
284+ with module_path .open ("w" ) as f :
285+ f .write (module )
286+
287+ if use_hatch_envs :
288+ command = "hatch run types:check"
289+ else :
290+ venv_path = tmp_path / ".venv"
291+ mypy_path = venv_path / "bin" / "mypy"
292+ python_path = venv_path / "bin" / "python"
293+ venv .EnvBuilder (with_pip = True ).create (venv_path )
294+ run_command (f"{ python_path !s} -m pip install -e '.[types]'" , root )
295+ command = f"{ mypy_path !s} { pkg_path !s} "
296+
297+ if valid :
298+ run_command (command , root )
299+ else :
300+ with pytest .raises (subprocess .CalledProcessError ):
301+ run_command (command , root )
0 commit comments