Skip to content

Commit 4c056a9

Browse files
committed
✨ Template | pre-commit: Add mypy type checking
Generated packages can now opt into mypy via a new `type_check` Copier question (none / loose / strict, default loose). The hook is a local pre-commit entry with `language: system`, so mypy runs inside the hatch `pre-commit` environment and sees every installed project dependency — no `additional_dependencies` list to maintain. `mypy` is added to the `pre-commit` optional-deps group when type checking is enabled. Two `[tool.mypy]` presets: - `loose` turns on `warn_unused_ignores`, `warn_redundant_casts`, and `warn_unreachable` - `strict` enables mypy's built-in `strict = true`. Both scope checking to `src/` and pin `python_version = "3.10"` to match `requires-python`. CI coverage is automatic — the existing `hatch run pre-commit:run --all-files` job picks up the new hook.
1 parent 8fa76b9 commit 4c056a9

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

copier.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ doc_deploy:
2222
{%- endif %}
2323
default: nowhere
2424

25+
type_check:
26+
type: str
27+
help: 'How strict should type checking be?'
28+
choices:
29+
- none
30+
- loose
31+
- strict
32+
default: loose
33+
2534
_subdirectory: template
2635
_message_after_copy: |
2736
The `{{package_name}}` package has been created successfully!

template/.pre-commit-config.yaml.jinja

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,14 @@ repos:
99
- id: ruff-format
1010
name: Format with Ruff
1111
types_or: [ python, pyi ]
12+
{% if type_check != 'none' %}
13+
- repo: local
14+
hooks:
15+
- id: mypy
16+
name: Type-check with mypy
17+
entry: mypy
18+
language: system
19+
types: [ python ]
20+
require_serial: true
21+
files: ^src/
22+
{% endif -%}

template/pyproject.toml.jinja

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ tests = [
3636
]
3737
pre-commit = [
3838
"pre-commit"
39+
{%- if type_check != 'none' -%}
40+
,
41+
"mypy"
42+
{%- endif %}
3943
]
4044

4145
[dependency-groups]
@@ -86,3 +90,18 @@ lint.ignore = [
8690
]
8791
[tool.ruff.lint.per-file-ignores]
8892
"tests/**/*.py" = ["INP001", "S101"]
93+
{%- if type_check == 'loose' %}
94+
95+
[tool.mypy]
96+
python_version = "3.10"
97+
files = ["src"]
98+
warn_unused_ignores = true
99+
warn_redundant_casts = true
100+
warn_unreachable = true
101+
{%- elif type_check == 'strict' %}
102+
103+
[tool.mypy]
104+
python_version = "3.10"
105+
files = ["src"]
106+
strict = true
107+
{%- endif %}

0 commit comments

Comments
 (0)