Skip to content

Commit 5aaa764

Browse files
Support pyright strict mode in template
- Add an option to the template to enable strict mode if pyright is selected as the type checker. - Default to standard type checking, the user must actively select it - Set config to strict if the user selects both pyright and strict mode (we do not support mypy strict mode) - Modify tests, remove "manual" enabling of strict mode now the template can do it for us
1 parent 1357917 commit 5aaa764

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

copier.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ type_checker:
122122
- pyright
123123
- mypy
124124

125+
strict_typing:
126+
type: bool
127+
when: >-
128+
{{ type_checker == 'pyright' }}
129+
default: false
130+
help: |
131+
Would you like to run pyright in strict mode?
132+
It is excellent for building consistent, maintainable projects but difficult to adopt on top
133+
of legacy code and projects with many external dependencies.
134+
The recommended approach is to start with strict mode and disable it if it
135+
becomes too costly to maintain.
136+
125137
pypi:
126138
type: bool
127139
help: Would you like the wheel and source distribution to be automatically uploaded to PyPI when a release is made?

example-answers.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ github_org: DiamondLightSource
1212
package_name: python_copier_template_example
1313
repo_name: python-copier-template-example
1414
type_checker: pyright
15+
strict_typing: false
1516
pypi: true

template/pyproject.toml.jinja

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ name = "{{ author_name }}"
5151
[tool.setuptools_scm]
5252
version_file = "src/{{ package_name }}/_version.py"
5353
{% if type_checker=="pyright" %}
54-
[tool.pyright]
54+
[tool.pyright]{% if strict_typing %}
55+
typeCheckingMode = "strict"
56+
{% else %}
5557
typeCheckingMode = "standard"
56-
reportMissingImports = false # Ignore missing stubs in imported modules
58+
{% endif %}reportMissingImports = false # Ignore missing stubs in imported modules
5759
{% endif %}{% if type_checker=="mypy" %}
5860
[tool.mypy]
5961
ignore_missing_imports = true # Ignore missing stubs in imported modules

tests/test_example.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ def test_template_defaults(tmp_path: Path):
4848
copy_project(tmp_path)
4949
run = make_venv(tmp_path)
5050
container_doc = tmp_path / "docs" / "how-to" / "run-container.md"
51+
pyproject_toml = tmp_path / "pyproject.toml"
5152
assert container_doc.exists()
5253
catalog_info = tmp_path / "catalog-info.yaml"
5354
assert catalog_info.exists()
55+
assert 'typeCheckingMode = "standard"' in pyproject_toml.read_text()
5456
run("./venv/bin/tox -p")
5557
if not run_pipe("git tag --points-at HEAD"):
5658
# Only run linkcheck if not on a tag, as the CI might not have pushed
@@ -213,21 +215,26 @@ def __init__(self):
213215
run("ruff check")
214216

215217

216-
def test_works_in_pyright_strict_mode(tmp_path: Path):
217-
copy_project(tmp_path)
218+
def test_pyright_works_in_strict_typing_mode(tmp_path: Path):
219+
copy_project(tmp_path, type_checker="pyright", strict_typing=True)
218220
pyproject_toml = tmp_path / "pyproject.toml"
219221

220-
# Enable strict mode
221-
run_pipe(
222-
'sed -i \'s|typeCheckingMode = "standard"|typeCheckingMode = "strict"|\''
223-
f" {pyproject_toml}"
224-
)
222+
# Check strict mode is configured
223+
assert 'typeCheckingMode = "strict"' in pyproject_toml.read_text()
225224

226225
# Ensure pyright is still happy
227226
run = make_venv(tmp_path)
228227
run(f"./venv/bin/pyright {tmp_path}")
229228

230229

230+
def test_ignores_mypy_strict_mode(tmp_path: Path):
231+
copy_project(tmp_path, type_checker="mypy", strict_typing=True)
232+
pyproject_toml = tmp_path / "pyproject.toml"
233+
234+
# Check strict mode is not configured
235+
assert "typeCheckingMode =" not in pyproject_toml.read_text()
236+
237+
231238
def test_works_with_pydocstyle(tmp_path: Path):
232239
copy_project(tmp_path)
233240
pyproject_toml = tmp_path / "pyproject.toml"

0 commit comments

Comments
 (0)