22
22
import os
23
23
import subprocess
24
24
import sys
25
+ import venv
25
26
from datetime import datetime , timezone
26
27
from pathlib import Path
28
+ from textwrap import dedent
27
29
from typing import Callable
28
30
29
31
import pytest
@@ -196,7 +198,7 @@ def test_docs_build(documentation: str, generated: Callable[..., Path], use_hatc
196
198
)
197
199
run_command ("sphinx-apidoc -o docs/api src/alien_clones" , project )
198
200
# 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 )
200
202
201
203
run_command ("pre-commit run --all-files -v check-readthedocs" , project )
202
204
@@ -229,7 +231,7 @@ def test_non_hatch_deps(
229
231
project = generated (
230
232
use_hatch_envs = False ,
231
233
use_lint = True ,
232
- use_types = True ,
234
+ use_mypy = True ,
233
235
use_test = True ,
234
236
use_git = False ,
235
237
documentation = documentation ,
@@ -254,3 +256,46 @@ def test_non_hatch_deps(
254
256
if documentation != "no" :
255
257
assert "docs" in optional_deps
256
258
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