Mypy doesn't recognize that truth value testing for sequences like `and my_list` result in a boolean type
Closed this issue · 2 comments
Yazan-Sharaya commented
Bug Report
Mypy fails to recognize that truth value testing results in boolean for sequences.
To Reproduce
my_list = [1]
should_update: bool = True and my_list
Actual Behavior
Mypy thinks the type of the new variable is whatever the type of the sequence is, list[int] in this case.
error: Incompatible types in assignment (expression has type "list[int]", variable has type "bool") [assignment]
Your Environment
- Mypy version used: 1.12.0
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files): None - Python version used: 3.12.7
JelleZijlstra commented
Mypy is correct here. The and
operator evaluates to its right-hand side if the left-hand side is truthy. You can try it in the REPL:
>>> my_list = [1]
... should_update: bool = True and my_list
...
>>> should_update
[1]
Yazan-Sharaya commented
You are right, I don't know how I didn't catch that.
Thanks for the quick response!