Skip to content

Support enum type narrowing with in #13684

Description

@edaqa-uncountable

The syntax some_enum_value in [list_of_values] should narrow the enum to that list of values.

This is a list extension to #10915 "support narrowing enum values using == and !="
Unlike the == case, there is no equivalent is syntax for lists that will narrow the type.

This example shows the motivation for this feature, as well as the syntax that is expected to work.

from typing import Literal, Union, cast
from enum import Enum


class SomeType(Enum):
    a = 1
    b = 2
    c = 3


LimitType = Union[Literal[SomeType.a, SomeType.b]]
AllowedLimitType = [SomeType.a, SomeType.b]


def inner_check(limit: LimitType) -> None:
    pass


def outer_check(some: SomeType) -> None:
    if some in [SomeType.a, SomeType.b]:
        inner_check(some)  # Incompatible type

    if some in AllowedLimitType:
        inner_check(some)  # Incompatible type

    if some == SomeType.a or some == SomeType.b:
        inner_check(some)  # Incompatible type
        inner_check(cast(LimitType, some))

    if some is SomeType.a or some is SomeType.b:
        inner_check(some)

    if some == SomeType.a:
        inner_check(SomeType.a)

    # This would be ideal
    # if some in LimitType:
    #   inner_check(some)

def main() -> None:
    outer_check(SomeType.a)


main()

Activity

  1. intgr commented on Apr 25, 2023

    @intgr
    Contributor

    Related issues for Literal type: #9718, #12535

  2. hauntsaninja commented on Feb 10, 2026

    @hauntsaninja
    Collaborator

    (Note behaviour on master is better here if you use a tuple instead of a list)

  3. added a commit that references this issue on May 13, 2026
    d2b0234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions