Skip to content

Commit 07e120e

Browse files
authored
Merge pull request #698 from NLeSC/typing
Typing
2 parents e5601c5 + 4390b9e commit 07e120e

File tree

11 files changed

+103
-5
lines changed

11 files changed

+103
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Added
66

7+
- Typing support. [#698](https://github.com/NLeSC/python-template/pull/698)
8+
79
### Changed
810

911
### Removed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ in line with what is recommended there. If not, please [contribute to the guide]
3333
1. (**important**) wait until some kind of consensus is reached about your idea being a good idea;
3434
1. if needed, fork the repository to your own Github profile and create your own feature branch off of the latest main commit. While working on your feature branch, make sure to stay up to date with the main branch by pulling in changes, possibly from the 'upstream' repository (follow the instructions [here](https://help.github.com/articles/configuring-a-remote-for-a-fork/) and [here](https://help.github.com/articles/syncing-a-fork/));
3535
1. install dependencies (see the [development documentation](README.dev.md#create-a-virtual-environment));
36-
1. make sure the existing tests still work by running ``pytest``. If project tests fail use ``pytest --keep-baked-projects`` to keep generated project files in `/tmp/pytest-*` and investigate;
36+
1. make sure the existing tests still work by running ``pytest``. If project tests fail use ``pytest --keep-copied-projects`` to keep generated project files in `/tmp/pytest-*` and investigate;
3737
1. add your own tests (if necessary);
3838
1. update or expand the documentation;
3939
1. update the `CHANGELOG.md` file with your change;

copier/questions/features_code_quality.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SelectCodeQualityFeatures:
77
type: yaml
88
default: |-
99
{% if template_profile == 'recommended' %}
10-
[AddLocalTests_flag, SelectGitHubActions_flag, AddLinting_flag, AddSonarCloud_flag, AddEditorConfig_flag]
10+
[AddLocalTests_flag, SelectGitHubActions_flag, AddLinting_flag, AddSonarCloud_flag, AddEditorConfig_flag, AddTyping_flag]
1111
{%- else -%}
1212
[]
1313
{%- endif %}
@@ -32,7 +32,8 @@ SelectCodeQualityFeatures:
3232
Editorconfig:
3333
value: AddEditorConfig_flag
3434
# validator: "{% if something != 'AnotherThing' %}BlaBla{% endif %}"
35-
35+
Typing (select type checker later):
36+
value: AddTyping_flag
3637

3738
# Sub-menus
3839
SelectGitHubActions:
@@ -54,6 +55,27 @@ SelectGitHubActions:
5455
value: AddLinkCheck_flag
5556
# validator: "{% if something != 'AnotherThing' %}BlaBla{% endif %}"
5657

58+
AddTyping:
59+
type: bool
60+
default: "{{ 'AddTyping_flag' in SelectCodeQualityFeatures }}"
61+
when: false
62+
63+
SelectTypeChecker:
64+
when: "{{ template_profile != 'minimum' and AddTyping }}"
65+
type: str
66+
default: pyright
67+
help: Select a type checker
68+
choices:
69+
Mypy:
70+
value: mypy
71+
Pyright:
72+
value: pyright
73+
# TODO add pyrefly https://pyrefly.org/
74+
# TODO add ty https://github.com/astral-sh/ty
75+
76+
# TODO add runtime type checking (using pydantic or typeguard)
77+
78+
# TODO ask how strict to typecheck
5779

5880
# computed features
5981
AddLocalTests:

copier/questions/features_documentation.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ AddGitHubActionDocumentation:
4444
type: bool
4545
default: "{{ 'AddGitHubActionDocumentation_flag' in SelectDocumentationFeatures }}"
4646
when: false
47+
AddTypingInDocs:
48+
type: bool
49+
default: "{{ AddTyping and AddLocalDocumentation }}"
50+
when: false

template/README.md.jinja

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
{% if AddLinkCheck -%}
3030
| Link checker | [![link-check]({{repository_url}}/actions/workflows/link-check.yml/badge.svg)]({{repository_url}}/actions/workflows/link-check.yml) |
3131
{%- endif -%}
32+
{% if AddTyping -%}
33+
{% if SelectTypeChecker == 'pyright' -%}
34+
| Type checker | [![Checked with pyright](https://microsoft.github.io/pyright/img/pyright_badge.svg)](https://microsoft.github.io/pyright/) |
35+
{%- endif -%}
36+
{% if SelectTypeChecker == 'mypy' -%}
37+
| Type checker | [![Checked with mypy](https://mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/) |
38+
{%- endif -%}
39+
{%- endif %}
3240

3341
## How to use {{ package_name }}
3442

template/pyproject.toml.jinja

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,22 @@ dev = [
5858
{%- endif %}
5959
"tox",
6060
"myst_parser",
61+
{% if AddTyping and SelectTypeChecker -%}
62+
"{{ SelectTypeChecker }}",
63+
{%- endif %}
64+
{%- if AddTypingInDocs %}
65+
"sphinx-autodoc-typehints",
66+
{%- endif %}
6167
]
6268
{%- if AddLocalDocumentation %}
6369
docs = [
6470
"sphinx",
6571
"sphinx_rtd_theme",
6672
"sphinx-autoapi",
6773
"myst_parser",
74+
{%- if AddTypingInDocs %}
75+
"sphinx-autodoc-typehints",
76+
{%- endif %}
6877
]
6978
{%- endif %}
7079
publishing = [
@@ -147,6 +156,17 @@ known-first-party = ["{{ package_name }}"]
147156
force-single-line = true
148157
no-lines-before = ["future","standard-library","third-party","first-party","local-folder"]
149158

159+
{% if AddTyping -%}
160+
{% if SelectTypeChecker == 'pyright' -%}
161+
[tool.pyright]
162+
include = ["src"]
163+
{%- endif %}
164+
{% if SelectTypeChecker == 'mypy' -%}
165+
[tool.mypy]
166+
files = ["src"]
167+
{%- endif %}
168+
{%- endif %}
169+
150170
[tool.bumpversion]
151171
current_version = "{{ version }}"
152172

template/src/{{package_name}}/my_module.py.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def hello(name: str) -> str:
88
Function docstring using Google docstring style.
99

1010
Args:
11-
name (str): Name to say hello to
11+
name{% if not AddTypingInDocs %} (str){% endif %}: Name to say hello to
1212

1313
Returns:
14-
str: Hello message
14+
{% if not AddTypingInDocs %}str: {% endif %}Hello message
1515

1616
Raises:
1717
ValueError: If `name` is equal to `nobody`

template/src/{{package_name}}/{% if AddTyping %}py.typed{% endif %}

Whitespace-only changes.

template/{% if AddDevDoc %}README.dev.md{% endif %}.jinja

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ git config --local core.hooksPath .githooks
8787
```
8888
{%- endif -%}
8989

90+
{% if AddTyping %}
91+
## Type checking
92+
93+
{% if SelectTypeChecker == 'pyright' -%}
94+
We use [pyright](https://microsoft.github.io/pyright/) for type checking.
95+
96+
```shell
97+
pyright
98+
```
99+
100+
{% endif -%}
101+
{% if SelectTypeChecker == 'mypy' -%}
102+
We use [mypy](http://mypy-lang.org/) for type checking.
103+
104+
```shell
105+
mypy
106+
```
107+
{% endif -%}
108+
{% endif -%}
109+
90110
## Generating the API docs
91111

92112
```shell

template/{% if AddLocalDocumentation %}docs{% endif %}/conf.py.jinja

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ extensions = [
4646
"sphinx.ext.viewcode",
4747
"autoapi.extension",
4848
"myst_parser",
49+
{%- if AddTypingInDocs %}
50+
"sphinx_autodoc_typehints",
51+
{%- endif %}
4952
]
5053

5154
# Add any paths that contain templates here, relative to this directory.

0 commit comments

Comments
 (0)