Closed
Description
Bug Report
When refining an element of type Union[Tuple, Sequence]
with the isinstance(x, Sequence)
in a conditional, the Tuple
part of the type is wrongly discarded in the branch.
To Reproduce
from typing import Sequence, Tuple, Union
def test_func(x: Union[Tuple[int], Sequence[str]]) -> str:
if isinstance(x, Sequence):
reveal_type(x) # Revealed type is "typing.Sequence[builtins.str]"
return x[0] # raises no error
return "a" # Statement is unreachable
Expected Behavior
The revealed type of x
should be Union[Tuple[int], Sequence[str]]
or something equivalent. The line return x[0]
should be marked as an issue, because currently test_func((1,))
returns 1
so the functions shouldn't typecheck.
Actual Behavior
test.py:6:21: note: Revealed type is "typing.Sequence[builtins.str]"
test.py:8:5: error: Statement is unreachable [unreachable]
Found 1 error in 1 file (checked 1 source file)
Your Environment
Used a fresh virtual environment
- Mypy version used: 0.971
- Python version used: 3.8