Commit 5d7e55b
committed
Add return-type annotation on __init__ methods
Although sometimes found unintuitive, the reutrn type of a class's
__init__ method is best annotated as None, as in other functions
that return implicitly or by operand-less return statements.
Note that this is only a minor improvement, effectively just a
style fix, because mypy treats __init__ specially and, *when at
least one parameter is annotated*, its return type is implicitly
None rather than implicitly Any like other functions. All the
__init__ methods modified here did already have one or more
annotated parameters.
However, having __init__ methods without the return type makes it
easier to introduce a bug where an __init__ method with no
parameters besides self -- which itself should almost always be
unannotated and is properly inferred -- is written and the return
annotation needed to get mypy to regard it as statically typed,
so it checks it at all, is omitted. (It is also inelegant when
one considers the meaning of __init__ and the distinction between
it and __new__.)
This commit does not add any return type annotations to functions
in the test suite, since the test suite doesn't currently use
static typing.
Further reading:
- https://peps.python.org/pep-0484/#the-meaning-of-annotations
- python/mypy#604
- #1755 (comment)1 parent e984bfe commit 5d7e55b
7 files changed
+7
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
846 | 846 | | |
847 | 847 | | |
848 | 848 | | |
849 | | - | |
| 849 | + | |
850 | 850 | | |
851 | 851 | | |
852 | 852 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
167 | | - | |
| 167 | + | |
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
917 | 917 | | |
918 | 918 | | |
919 | 919 | | |
920 | | - | |
| 920 | + | |
921 | 921 | | |
922 | 922 | | |
923 | 923 | | |
| |||
0 commit comments