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
I have a list with type List[Optional[X]] and a loop over it:
from typing import List, Optional
a: List[Optional[str]] = [None, None, None]
for node in ['a','b','c']:
o = len(node)
while a[o] is not None:
somestr: str = a[o]
(yes, this does absolutely nothing, for a more complete example see mypy_bug.py.txt. But the bug is the same.)
Mypy shows the following error: tmp.py:9: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "str"). But a[o] can never be None, because of the loop, and thus has to be str, if this code is executed.
To Reproduce
Check the code posted above with mypy.
Expected Behavior
No error should be shown, since the types are correct (IMHO)
Your Environment
Mypy version used: 0.761
Mypy command-line flags:
Mypy configuration options from mypy.ini (and other config files):
Python version used: Python 3.8.5
Operating system and version: Ubuntu 20.04.2
The text was updated successfully, but these errors were encountered:
Another example of this behavior. I was caught off guard because the variable was inferred correctly as not None when used in a simple way, but not when used inside a lambda:
from __future__ importannotationsdeffunc(x: int|None):
whilexisnotNone:
reveal_type(x) # Revealed type is "builtins.int"x*5# passes typechecking
(lambday: x*y)(5) # fails typechecking: Unsupported operand types for * ("None" and "int")
@karoshi42, subscript expressions with dynamic indices are difficult for any static type checker to narrow because the analyzer needs to prove that the index expression remains the same between the point where it's tested and where it's accessed again. Most static type checkers in most languages therefore support narrowing only for subscript expressions that use constant indices. That's true of mypy and pyright, for example.
The workaround I recommend is to use a temporary variable and an assignment (walrus) operator. Not only does this eliminate the type error, but it also makes your loop run faster.
Bug Report
I have a list with type List[Optional[X]] and a loop over it:
(yes, this does absolutely nothing, for a more complete example see mypy_bug.py.txt. But the bug is the same.)
Mypy shows the following error:
tmp.py:9: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "str")
. Buta[o]
can never beNone
, because of the loop, and thus has to bestr
, if this code is executed.To Reproduce
Check the code posted above with mypy.
Expected Behavior
No error should be shown, since the types are correct (IMHO)
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: