-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
Description
Feature
Destructured tuple discrimination
Pitch
Mypy shoud be able to discriminate between a union of structured types like tuples after destructuring.
import typing as t
from typing_extensions import reveal_type
ResultTup = t.Union[t.Tuple[int, None], t.Tuple[None, ValueError]]
def f(x: int) -> ResultTup:
if x < 0:
return None, ValueError("x must be positive")
else:
return x + 1, None
# NO DESTRUCTURING WORKS AS EXPECTED
result = f(1)
if result[1] is not None:
...
else:
# mypy: Revealed type is "builtins.int"
print(reveal_type(result[0]))
# DESTRUCTURING LOSES ASSOCIATION
ok, error = f(1)
if error is not None:
...
else:
# mypy: Revealed type is "Union[builtins.int, None]"
print(reveal_type(ok))Reactions are currently unavailable