ruff/crates/ty_python_semantic/resources/mdtest/narrow/conditionals/not.md
2025-05-03 19:49:15 +02:00

543 B

Narrowing for not conditionals

The not operator negates a constraint.

not is None

def _(flag: bool):
    x = None if flag else 1

    if not x is None:
        reveal_type(x)  # revealed: Literal[1]
    else:
        reveal_type(x)  # revealed: None

    reveal_type(x)  # revealed: None | Literal[1]

not isinstance

def _(flag: bool):
    x = 1 if flag else "a"

    if not isinstance(x, (int)):
        reveal_type(x)  # revealed: Literal["a"]
    else:
        reveal_type(x)  # revealed: Literal[1]