-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
documentationtopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder
Description
pyright calls it aliased conditional expression and we don't support it (yet?):
def func1(x: str | None):
is_str = x is not None
if is_str:
reveal_type(x) # str
else:
reveal_type(x) # None
It occasionally comes up, and it would be handy to align expectations by mentioning it in the type narrowing docs.
Related, some users (e.g. #15332) expect even more complex analysis, which we should also clarify we don't do, and offer alternatives. For example:
a1: str | None
a2: str | None
if a1 is not None or a2 is not None:
a: str = a1 or a2
else:
raise ValueError
could be (reluctantly?) rewritten as:
a1: str | None
a2: str | None
a: str
if a1 is not None:
a = a1
elif a2 is not None:
a = a2
else:
raise ValueError
Metadata
Metadata
Assignees
Labels
documentationtopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder