Add implicit -> None type annotation to __init__ methods
See original GitHub issueShould work fine with mypy even if not marked via -> None:
(because it’s implicit in Python terms)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:10 (9 by maintainers)
Top Results From Across the Web
python - Why should we use -> in def __init__(self, n) -> None
The main reason is to allow static type checking. By default, mypy will ignore unannotated functions and methods.
Read more >typing — Support for type hints — Python 3.11.1 documentation
New in version 3.5. ... The Python runtime does not enforce function and variable type annotations. They can be used by third party...
Read more >Automatically know that the function returns None if there is no ...
So you'd have to annotate no-argument functions anyway with an explicit -> None . But this would be inconsistent, as sometimes you have...
Read more >Kinds of types - mypy 0.991 documentation
When initializing a variable as None , None is usually an empty place-holder value, and the actual value has a different type. This...
Read more >Using Python's Type Annotations - DEV Community
This is accomplished by adding a given type declaration after initializing/declaring a variable or method. Why & How to Use Type Annotations. A ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s not entirely correct. PEP 484 specifies that
__init__
should be annotated with a return type ofNone
if annotated, but that “Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention”.As you can see in this mypy issue, Guido himself complains that “… it’d suck if we’d have to develop the habit of adding explicit ‘-> None’ to all init methods just to make mypy happy”. But the issue is still an open bug with mypy, so for the time being, making mypy happy requires that you explicitly add the ‘-> None’. Hopefully in the near future,
__init__
will be assumed to have a default return type ofNone
instead ofAny
, as mentioned in the final comment.Per the above, this is probably not worth implementing. If someone wants a type-checked no-argument
__init__
, then they should probably type hint the function signature.If someone didn’t want their no-argument
__init__
type-checked, then that should still be possible whilst still running mypy on their code.