I think it would be great if mypy supported control flow analysis of aliased conditions like, e.g., introduced in TypeScript v4.4.
Currently, mypy works as follows:
from typing import Sequence, Union
def f(value: Union[str, Sequence[str]]):
is_string = isinstance(value, str)
if is_string:
reveal_type(value) # Revealed type is "Union[builtins.str, typing.Sequence[builtins.str]]"
else:
reveal_type(value) # Revealed type is "Union[builtins.str, typing.Sequence[builtins.str]]"
But it would be nice if it could infer the type of value correctly in the condition:
from typing import Sequence, Union
def f(value: Union[str, Sequence[str]]):
is_string = isinstance(value, str)
if is_string:
reveal_type(value) # Revealed type is "builtins.str"
else:
reveal_type(value) # Revealed type is "typing.Sequence[builtins.str]"
Feature
I think it would be great if mypy supported control flow analysis of aliased conditions like, e.g., introduced in TypeScript v4.4.
Currently, mypy works as follows:
But it would be nice if it could infer the type of
value
correctly in the condition:Pitch
See, e.g., https://github.com/copier-org/copier/pull/812:
Source: https://github.com/copier-org/copier/blob/1f40ddaac2cc1b1d90ab08c681a3109e329c0206/copier/main.py#L193-L214
It would be nice to rewrite the above snippet as follows: