Skip to content

Commit eb9426b

Browse files
authored
Merge pull request #8 from pybamm-team/mypy-for-template
feat: enable mypy for the template
2 parents d68c0df + d93c6ec commit eb9426b

File tree

10 files changed

+57
-10
lines changed

10 files changed

+57
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,6 @@ cython_debug/
129129

130130
# hatch-vcs
131131
src/pybamm_cookiecutter/_version.py
132+
133+
# ruff
134+
.ruff_cache/

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ repos:
1515
- id: trailing-whitespace
1616
- id: end-of-file-fixer
1717
- id: check-yaml
18+
files: "^(src/pybamm_cookiecutter|tests$)"
1819
- id: check-added-large-files
1920
- id: mixed-line-ending
2021

cookiecutter.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"backend": [
1818
"hatch"
1919
],
20+
"mypy": false,
2021
"__year": "{% now 'utc', '%Y' %}",
2122
"__project_slug": "{{ cookiecutter.project_name | lower | replace('-', '_') | replace('.', '_') }}",
2223
"__type": "{{ 'pure' }}",
@@ -38,6 +39,7 @@
3839
"backend": {
3940
"__prompt__": "Choose a build backend",
4041
"hatch": "Hatchling (recommended for pure Python projects)"
41-
}
42+
},
43+
"mypy": "Use mypy and static types"
4244
}
4345
}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ classifiers = [
2727
"Programming Language :: Python :: 3.10",
2828
"Programming Language :: Python :: 3.11", # PyBaMM supports >=3.8,<3.12
2929
"Topic :: Scientific/Engineering",
30+
"Typing :: Typed",
3031
]
3132
dynamic = ["version"]
3233
dependencies = ["pybamm"]

{{cookiecutter.project_name}}/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,8 @@ Thumbs.db
122122

123123
# hatch-vcs
124124
*/_version.py
125+
126+
# mypy
127+
.mypy_cache
128+
.dmypy.json
129+
dmypy.json

{{cookiecutter.project_name}}/.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,13 @@ repos:
5555
language: pygrep
5656
entry: PyBind|Numpy|Scipy|Cmake|CCache|Github|Pybamm
5757
exclude: .pre-commit-config.yaml
58+
{# keep this line here for newline #}
59+
{%- if cookiecutter.mypy %}
60+
# mypy
61+
62+
- repo: https://github.com/pre-commit/mirrors-mypy
63+
rev: v1.4.1
64+
hooks:
65+
- id: mypy
66+
files: "^(src/{{ cookiecutter.__project_slug }}|tests$)"
67+
{%- endif %}

{{cookiecutter.project_name}}/docs/conf.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
# https://pydata-sphinx-theme.readthedocs.io/en/stable/user_guide/index.html
1515

1616

17-
import os
18-
import sys
1917
import importlib.metadata
20-
18+
import sys
19+
from pathlib import Path
2120

2221
# ---- Project information ------------------------------------------------------------
2322

@@ -29,10 +28,10 @@
2928
# ---- Path configuration -------------------------------------------------------------
3029

3130
# Path for repository root
32-
sys.path.insert(0, os.path.abspath("../"))
31+
sys.path.insert(0, Path("../").resolve())
3332

3433
# Path for local Sphinx extensions
35-
sys.path.append(os.path.abspath("./sphinxext/"))
34+
sys.path.append(Path("./sphinxext/").resolve())
3635

3736
# ---- General configuration ----------------------------------------------------------
3837

@@ -55,7 +54,7 @@
5554
]
5655

5756
html_theme = "pydata_sphinx_theme"
58-
html_title = "%s v%s Manual" % (project, version)
57+
html_title = f"{project} v{version} Manual"
5958
html_last_updated_fmt = "%Y-%m-%d"
6059
html_static_path = ["_static"]
6160
html_file_suffix = ".html"

{{cookiecutter.project_name}}/pyproject.toml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ classifiers = [
4040
"Programming Language :: Python :: 3.10",
4141
"Programming Language :: Python :: 3.11", # PyBaMM supports >=3.8,<3.12
4242
"Topic :: Scientific/Engineering",
43+
{%- if cookiecutter.mypy %}
44+
"Typing :: Typed",
45+
{%- endif %}
4346
]
4447
{%- if cookiecutter.backend == "hatch" %}
4548
dynamic = ["version"]
@@ -73,7 +76,7 @@ Homepage = "{{ cookiecutter.url }}"
7376
"Bug Tracker" = "{{ cookiecutter.url }}/issues"
7477
Discussions = "{{ cookiecutter.url }}/discussions"
7578
Changelog = "{{ cookiecutter.url }}/releases"
76-
79+
{# keep this line here for newline #}
7780
{%- if cookiecutter.backend == "hatch" %}
7881
[tool.hatch]
7982
version.source = "vcs"
@@ -82,6 +85,21 @@ envs.default.dependencies = [
8285
"pybamm",
8386
]
8487
{%- endif %}
88+
{# keep this line here for newline #}
89+
{%- if cookiecutter.mypy %}
90+
[tool.mypy]
91+
python_version = "3.8"
92+
strict = true
93+
warn_return_any = false
94+
show_error_codes = true
95+
enable_error_code = [
96+
"ignore-without-code",
97+
"truthy-bool",
98+
"redundant-expr",
99+
]
100+
disallow_untyped_defs = false
101+
disallow_untyped_calls = false
102+
{%- endif %}
85103

86104
[tool.coverage]
87105
run.source = ["{{ cookiecutter.__project_slug }}"]
@@ -120,5 +138,6 @@ unfixable = [
120138
"T20", # Removes print statements
121139
"F841", # Removes unused variables
122140
]
141+
line-length = 100
123142
exclude = []
124143
flake8-unused-arguments.ignore-variadic-names = true

{{cookiecutter.project_name}}/src/{{cookiecutter.__project_slug}}/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
44
{{ cookiecutter.project_name }}: {{ cookiecutter.project_short_description }}
55
"""
6-
from ._version import version as __version__
7-
6+
{%- if cookiecutter.mypy %}
7+
from __future__ import annotations
8+
{%- endif %}
89

10+
from ._version import version as __version__
11+
{# keep this line here for newline #}
12+
{%- if cookiecutter.mypy %}
13+
__all__: tuple[str] = ("__version__",)
14+
{%- else %}
915
__all__ = ("__version__",)
16+
{%- endif %}

{{cookiecutter.project_name}}/src/{{cookiecutter.__project_slug}}/{% if cookiecutter.mypy %}py.typed{% endif %}

Whitespace-only changes.

0 commit comments

Comments
 (0)