Skip to content

eliminate definitely-impossible types from union in equality narrowing #939

@carljm

Description

@carljm

In this example:

from typing import Literal

def test(x: Literal["a", "b", "c"] | None | int = None):
    if x in ("a", "b"):
        reveal_type(x)

We currently refuse to do any equality narrowing here because of the presence of int in the union (which may include subclasses of int with custom __eq__). But this is overly conservative; we should be able to eliminate None and Literal["c"] from the union anyway, resulting in the type Literal["a", "b"] | int.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions especially welcomenarrowingrelated to flow-sensitive type narrowing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions