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
The linter should not complain as after the if check both variables a and b can't be None.
Actual Behavior
The linter says that the None possibility for the variable lacks the method. Is not correct since we have
taken action to prevent this.
testmypy.py:11: error: Item "None" of "Optional[Test]" has no attribute "double"
testmypy.py:11: error: Argument 1 to "double" of "Test" has incompatible type "Optional[int]"; expected "int"
Your Environment
Mypy version used: 0.931
Mypy command-line flags: mypy testmypy.py
Mypy configuration options from mypy.ini (and other config files):
Python version used: 3.9.10 and 3.7
Operating system and version: EndeavourOS Linux x86_64 - 5.16.4-arch1-1
The text was updated successfully, but these errors were encountered:
The pattern None in (a, b) is not current supported for type narrowing by mypy. If you use the pattern a is None or b is None, it will work as expected.
I have a similar problem. See, if I try to check if several elements are not None with all function, mypy ins't capable to infer the variable isn't Optional anymore. Example:
my_model: Optional[MyModel] =optionalDict.get('my_model')
my_second_model: Optional[MySecondModel] =optionalDict.get('my_second_model')
ifnotall([my_model, my_second_model]):
raiseBaseException('Models cannot be None')
my_model.attr# <--- still infers like Optional[MyModel]
JelleZijlstra
changed the title
Mypy fails to recognize after a explicit check that a set of variables can't be None
Type narrowing using "in tuple"
Nov 5, 2022
Mypy fails to recognize after a explicit check that a set of variables can't be None
To Reproduce
Run mypy on the following script
Expected Behavior
The linter should not complain as after the if check both variables
a
andb
can't beNone
.Actual Behavior
The linter says that the
None
possibility for the variable lacks the method. Is not correct since we havetaken action to prevent this.
Your Environment
mypy testmypy.py
mypy.ini
(and other config files):EndeavourOS Linux x86_64 - 5.16.4-arch1-1
The text was updated successfully, but these errors were encountered: