Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type_narrowing.rst: document lack of "second degree" analysis #15653

Closed
ikonst opened this issue Jul 13, 2023 · 0 comments · Fixed by #16018
Closed

type_narrowing.rst: document lack of "second degree" analysis #15653

ikonst opened this issue Jul 13, 2023 · 0 comments · Fixed by #16018
Labels
documentation topic-type-narrowing Conditional type narrowing / binder

Comments

@ikonst
Copy link
Contributor

ikonst commented Jul 13, 2023

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
@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label Jul 18, 2023
hauntsaninja pushed a commit that referenced this issue Sep 2, 2023
Fixes #15653.

I did not use erictraut's "quantum entanglement" metaphor, though I
find it to be quite illustrative :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants