question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add implicit -> None type annotation to __init__ methods

See original GitHub issue

Should work fine with mypy even if not marked via -> None: (because it’s implicit in Python terms)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
eindirancommented, Feb 11, 2018

That’s not entirely correct. PEP 484 specifies that __init__ should be annotated with a return type of None 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 of None instead of Any, as mentioned in the final comment.

0reactions
auscompgeekcommented, Dec 26, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found