Skip to content

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

@ikonst

Description

@ikonst

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

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions