Skip to content

Commit e47f7de

Browse files
authored
Make type_checker selectable (#108)
2 parents 301c471 + 4c579a9 commit e47f7de

File tree

6 files changed

+40
-15
lines changed

6 files changed

+40
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ It integrates the following tools:
2424
- [pip](https://pip.pypa.io) to manage installation
2525
- [pytest](https://docs.pytest.org) for code testing and coverage
2626
- [pre-commit](https://pre-commit.com) to run linting and formatting such as [ruff](https://docs.astral.sh/ruff)
27-
- [pyright](https://microsoft.github.io/pyright) for static type checking
27+
- [pyright](https://microsoft.github.io/pyright) or [mypy](https://www.mypy-lang.org) for static type checking
2828
- [sphinx](https://www.sphinx-doc.org) for tutorials, how-to guides, explanations and reference documentation
2929
- [tox](https://tox.wiki) to run the above tasks locally and in CI
3030
- [GitHub Actions](https://docs.github.com/en/actions) to provide CI and deployment to PyPI and GitHub Pages

catalog-info.yaml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ spec:
4646
ui:field: RepoUrlPicker
4747
ui:options:
4848
requestUserCredentials:
49-
secretsKey: USER_OAUTH_TOKEN
50-
additionalScopes:
51-
github:
52-
- workflow
49+
secretsKey: USER_OAUTH_TOKEN
50+
additionalScopes:
51+
github:
52+
- workflow
5353
allowedHosts:
5454
- github.com
5555
description:
@@ -75,7 +75,17 @@ spec:
7575
enum:
7676
- README
7777
- sphinx
78-
78+
typeChecker:
79+
title: Type Checker
80+
description: |
81+
What type checker would you like to use?
82+
Pyright is recommended for new projects, but it is stricter,
83+
so you might need to select mypy for existing projects.
84+
type: string
85+
default: pyright
86+
enum:
87+
- pyright
88+
- mypy
7989

8090
# This template is meant to be used on top of an existing template.
8191
# By adding the following and fetching from an absolute URL you can
@@ -86,9 +96,9 @@ spec:
8696
action: fetch:copier
8797
input:
8898
url: .
89-
values:
99+
values:
90100
destination: ${{ parameters.repoUrl | parseRepoUrl }}
91-
101+
# These are answers for the copier template
92102
package_name: ${{ parameters.repoUrl | parseRepoUrl | pick('repo') | replace('-', '_') }}
93103
description: ${{ parameters.description }}
94104
git_platform: ${{ parameters.repoUrl | parseRepoUrl | pick('host') }}
@@ -100,12 +110,13 @@ spec:
100110
component_owner: ${{ parameters.owner }}
101111
docker: ${{ parameters.docker }}
102112
docs_type: ${{ parameters.docs }}
113+
type_checker: ${{ parameters.typeChecker }}
103114

104115
- id: publish
105116
name: Publish
106117
action: publish:github
107118
input:
108-
allowedHosts: ['github.com']
119+
allowedHosts: ["github.com"]
109120
description: This is ${{ parameters.name }}
110121
repoUrl: ${{ parameters.repoUrl }}
111122
repoVisibility: public

copier.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ docs_type:
7676
- README
7777
- sphinx
7878

79+
type_checker:
80+
type: str
81+
help: |
82+
What type checker would you like to use?
83+
Pyright is recommended for new projects, but it is stricter,
84+
so you might need to select mypy for existing projects.
85+
choices:
86+
- pyright
87+
- mypy
88+
7989
# Internal variables
8090
repo_url:
8191
type: str

docs/how-to/static-analysis.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Run static analysis using pyright
1+
# Run static analysis using pyright or mypy
22

3-
Static type analysis is done with [pyright](https://microsoft.github.io/pyright/). It checks type definition in source files without running them, and highlights potential issues where types do not match. You can run it with:
3+
Static type analysis is done with [pyright](https://microsoft.github.io/pyright) or [mypy](https://www.mypy-lang.org) dependent on the settings in `pyproject.toml`. It checks type definition in source files without running them, and highlights potential issues where types do not match. You can run it with:
44

55
```
66
$ tox -e type-checking

example-answers.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ git_platform: github.com
99
github_org: DiamondLightSource
1010
package_name: python_copier_template_example
1111
repo_name: python-copier-template-example
12+
type_checker: pyright

template/pyproject.toml.jinja

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ name = "{{ author_name }}"
5151

5252
[tool.setuptools_scm]
5353
write_to = "src/{{ package_name }}/_version.py"
54-
54+
{% if type_checker=="pyright" %}
5555
[tool.pyright]
5656
reportMissingImports = false # Ignore missing stubs in imported modules
57-
57+
{% endif %}{% if type_checker=="mypy" %}
58+
[tool.mypy]
59+
ignore_missing_imports = true # Ignore missing stubs in imported modules
60+
{% endif %}
5861
[tool.pytest.ini_options]
5962
# Run pytest with all our checkers, and don't spam us with massive tracebacks on error
6063
addopts = """
@@ -86,12 +89,12 @@ passenv = *
8689
allowlist_externals =
8790
pytest
8891
pre-commit
89-
pyright
92+
{{ type_checker }}
9093
{% if sphinx %} sphinx-build
9194
sphinx-autobuild
9295
{% endif %}commands =
9396
pre-commit: pre-commit run --all-files {posargs}
94-
type-checking: pyright src tests {posargs}
97+
type-checking: {{ type_checker }} src tests {posargs}
9598
tests: pytest --cov={{ package_name }} --cov-report term --cov-report xml:cov.xml {posargs}
9699
{% if sphinx %} docs: sphinx-{posargs:build -EW --keep-going} -T docs build/html
97100
{% endif %}"""

0 commit comments

Comments
 (0)