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
Consider a code checking whether all elements of a list are true. If so, it prints every element to the console with appended exclamation mark. If not, it should skip execution and print appropriate message
To Reproduce
example 1 - mypy returns failure
li= [None, "a"]
ifnotall(li):
print("Not all elems of li are true")
else:
foreleminli:
print(elem+"!")
example 2 - mypy ok
li= ["a", "b"]
ifnotall(li):
print("Not all elems of li are true")
else:
foreleminli:
print(elem+"!")
Expected Behavior
mypy should not report an error in line 7: print(elem + "!") - python cannot reach this code due to the condition: if not all(li):
Actual Behavior
example 1 fails:
main.py:7: error: Unsupported left operand typefor + ("None") [operator]
main.py:7: note: Left operand is of type"str | None"
Found 1 error in 1 file (checked 1 source file)
JelleZijlstra
changed the title
Variable is None-checked but mypy prompts Unsupported left operand type for + ("None")
Support type narrowing on all()Jan 19, 2024
Bug Report
Consider a code checking whether all elements of a list are true. If so, it prints every element to the console with appended exclamation mark. If not, it should skip execution and print appropriate message
To Reproduce
example 1 - mypy returns failure
example 2 - mypy ok
Expected Behavior
mypy should not report an error in line 7:
print(elem + "!")
- python cannot reach this code due to the condition:if not all(li):
Actual Behavior
example 1 fails:
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: