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
When iterating over an empty iterable with afor loop , the index isn't defined. If the index is a predefined variable, it is left unchanged. Mypy, however always infers the iterable is never empty and the index is always overwritten. This causes a divergence between static analysis and runtime behavior.
(I initially found this via a false positive truthy-bool error)
To Reproduce
# example.pyfromtypingimport*l: list[str] = []
x: str|int=5forxinl:
passreveal_type(x) # Revealed type is "builtins.str" -- expected `str | int`# python3.11 example.py # Runtime type is 'int'
mypy should infer x: strinside the for statement body, but outside, it should infer the original value or'd with the type of the iterable. In this example it should be str | int
Actual Behavior
mypy infers str whereas at runtime it is int.
Your Environment
Mypy version used: master, 1.6.1, 1.5.1
Mypy command-line flags: N/A
Mypy configuration options from mypy.ini (and other config files): N/A
Python version used: 3.11
The text was updated successfully, but these errors were encountered:
Bug Report
When iterating over an empty iterable with a
for
loop , the index isn't defined. If the index is a predefined variable, it is left unchanged. Mypy, however always infers the iterable is never empty and the index is always overwritten. This causes a divergence between static analysis and runtime behavior.(I initially found this via a false positive truthy-bool error)
To Reproduce
https://mypy-play.net/?mypy=master&python=3.11&gist=eecaaa80bc226778521644f9dedbda5b
Expected Behavior
mypy should infer
x: str
inside thefor
statement body, but outside, it should infer the original value or'd with the type of the iterable. In this example it should bestr | int
Actual Behavior
mypy infers
str
whereas at runtime it isint
.Your Environment
mypy.ini
(and other config files): N/AThe text was updated successfully, but these errors were encountered: