from typing import Union
def foo(bar: Union[str, int]):
if bar in ["a", "b", "c"]:
# to a human, bar is clearly a str. mypy still thinks it's a Union[str, int]
return bar.upper()
else:
return bar
$ mypy test.py
test.py:5: error: Item "int" of "Union[str, int]" has no attribute "upper"
The same thing happens if the condition is, for example, bar == "a".
- Are you reporting a bug, or opening a feature request?
Feature request
- What is the behavior/output you expect?
Mypy to understand the effect of conditions like bar == "a" or bar in ["a"], like it already does with isinstance checks
The same thing happens if the condition is, for example,
bar == "a".Feature request
Mypy to understand the effect of conditions like
bar == "a"orbar in ["a"], like it already does withisinstancechecks