You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After introduction of #604 you are not longer obligated to specify that __init__ returns None type. However, if you dealing with inheritance and the child class has no parameters, thus it is not annotated if you don't specify the __init__ return type, you unknowingly disable mypy inspection in that class. To make those inspections work you have to explicitly pass --check-untyped-defs argument or simply add return type annotation to __init__.
Now, I'm not saying that it is a bug but this behavior is not so obvious. Especially that after introduction of #604 most people probably removed those explicit annotations from their code or stopped writing them after that point.
Because of that, you can unknowingly make mypy validation pass even thought it shouldn't:
example_1.py:4: note: Revealed type is 'builtins.str'
example_1.py:10: note: Revealed type is 'Any'
example_1.py:10: note: 'reveal_type' always outputs 'Any' in unchecked functions
However, after adding the annotation (-> None), everything will work as expected:
example_2.py:4: note: Revealed type is 'builtins.str'
example_2.py:9: error: Argument "value" to "__init__" of "Base" has incompatible type "None"; expected "str"
example_2.py:10: note: Revealed type is 'builtins.str'
Found 1 error in 1 file (checked 1 source file)
You can also get the same result with the code from example_1.py if you pass --check-untyped-defs parameter to mypy.
Maybe it is worth adding this special case in mypy documentation? Maybe inside the No errors reported for obviously wrong code section in Common issues and solutions chapter?
Versions
mypy 0.761
Python 3.7.4
The text was updated successfully, but these errors were encountered:
After introduction of #604 you are not longer obligated to specify that
__init__
returnsNone
type. However, if you dealing with inheritance and the child class has no parameters, thus it is not annotated if you don't specify the__init__
return type, you unknowingly disablemypy
inspection in that class. To make those inspections work you have to explicitly pass--check-untyped-defs
argument or simply add return type annotation to__init__
.Now, I'm not saying that it is a bug but this behavior is not so obvious. Especially that after introduction of #604 most people probably removed those explicit annotations from their code or stopped writing them after that point.
Because of that, you can unknowingly make
mypy
validation pass even thought it shouldn't:However, after adding the annotation (
-> None
), everything will work as expected:You can also get the same result with the code from
example_1.py
if you pass--check-untyped-defs
parameter tomypy
.Maybe it is worth adding this special case in
mypy
documentation? Maybe inside theNo errors reported for obviously wrong code
section inCommon issues and solutions
chapter?Versions
The text was updated successfully, but these errors were encountered: