Skip to content

Commit 8a8083d

Browse files
committed
Typing questiones for py.typed + mypy or pyright + ci + docs
Fixes #230
1 parent c89db10 commit 8a8083d

File tree

11 files changed

+141
-3
lines changed

11 files changed

+141
-3
lines changed

copier.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
# community features
3131
!include copier/questions/features_community.yml
3232

33+
---
34+
# typing features
35+
!include copier/questions/features_typing.yml
36+
3337
---
3438
# global flags
3539
!include copier/global_flags.yml

copier/global_flags.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ HasWorkflows:
1212
or AddOnlineDocumentation
1313
or AddSonarCloud
1414
or AddZenodo
15-
or AddLinting }}"
15+
or AddLinting
16+
or AddGithubActionTypeCheck }}"
1617
when: false
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
# Questions for typing features
3+
4+
# the main menu
5+
SelectTypingFeatures:
6+
when: "{{ template_profile != 'minimum' }}"
7+
type: yaml
8+
default: |-
9+
{% if template_profile == 'recommended' %}
10+
[AddTyping_flag, AddTypingInDocs_flag, AddGitHubActionTypeCheck_flag]
11+
{%- else -%}
12+
[]
13+
{%- endif %}
14+
help: Select typing features
15+
multiselect: true
16+
choices:
17+
Support Typing (add py.typed file):
18+
value: AddTyping_flag
19+
GitHub Action to type check:
20+
value: AddGitHubActionTypeCheck_flag
21+
Typing in API documentation:
22+
value: AddTypingInDocs_flag
23+
24+
# TODO add runtime type checking (using pydantic or typeguard)
25+
26+
SelectTypeChecker:
27+
when: "{{ template_profile != 'minimum' and 'AddTyping_flag' in SelectTypingFeatures }}"
28+
type: str
29+
default: |-
30+
{%- if template_profile == 'recommended' -%}
31+
pyright
32+
{%- endif %}
33+
help: Select a type checker
34+
choices:
35+
Mypy:
36+
value: mypy
37+
Pyright:
38+
value: pyright
39+
# TODO add pyrefly https://pyrefly.org/
40+
# TODO add ty https://github.com/astral-sh/ty
41+
42+
AddPyTyped:
43+
type: bool
44+
default: "{{ 'AddTyping_flag' in SelectTypingFeatures }}"
45+
when: false
46+
47+
AddGithubActionTypeCheck:
48+
type: bool
49+
default: "{{ 'AddGitHubActionTypeCheck_flag' in SelectTypingFeatures and SelectTypeChecker != '' }}"
50+
when: false
51+
52+
AddTypingInDocs:
53+
type: bool
54+
default: "{{ 'AddTypingInDocs_flag' in SelectTypingFeatures }}"
55+
when: false
56+
57+
# TODO ask how strict to typecheck

template/README.md.jinja

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
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 SelectTypeChecker == 'pyright' -%}
33+
| Type checker | [![Checked with pyright](https://microsoft.github.io/pyright/img/pyright_badge.svg)](https://microsoft.github.io/pyright/) |
34+
{%- endif -%}
35+
{% if SelectTypeChecker == 'mypy' -%}
36+
| Type checker | [![Checked with mypy](https://mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/) |
37+
{%- endif -%}
3238

3339
## How to use {{ package_name }}
3440

template/pyproject.toml.jinja

Lines changed: 18 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 SelectTypeChecker -%}
62+
"{{ SelectTypeChecker }}",
63+
{%- endif %}
64+
{%- if 'AddTypingInDocs_flag' in SelectTypingFeatures %}
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_flag' in SelectTypingFeatures %}
75+
"sphinx-autodoc-typehints",
76+
{%- endif %}
6877
]
6978
{%- endif %}
7079
publishing = [
@@ -147,6 +156,15 @@ 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 SelectTypeChecker == 'pyright' -%}
160+
[tool.pyright]
161+
include = ["src"]
162+
{%- endif %}
163+
{% if SelectTypeChecker == 'mypy' -%}
164+
[tool.mypy]
165+
files = ["src"]
166+
{%- endif %}
167+
150168
[tool.bumpversion]
151169
current_version = "{{ version }}"
152170

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 AddPyTyped %}py.typed{% endif %}

Whitespace-only changes.

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

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

90+
{%- if SelectTypeChecker -%}
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+
108+
{% endif -%}
109+
{% endif -%}
110+
90111
## Generating the API docs
91112

92113
```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.

template/{% if AddPreCommit %}.githooks{% endif %}/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ echo "Starting ruff analysis..."
99
ruff check --fix
1010
ruff format
1111

12+
# TODO add type checker
13+
1214
# use return code to abort commit if necessary
1315
if [ $? != "0" ]; then
1416
echo "Commit aborted. Fix linter issues found by ruff before committing."

0 commit comments

Comments
 (0)