GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support enum type narrowing with `in` · Issue #13684 · python/mypy

Support enum type narrowing with in #13684

New issue
Copy link
New issue
Copy link
Closed
#21461
Closed
Support enum type narrowing with in#13684
#21461
Copy link
Labels
featuretopic-type-narrowingConditional type narrowing / binder

Description

@edaqa-uncountable
edaqa-uncountable
opened on Sep 19, 2022
Issue body actions

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()
Reacted by Marti Raudsepp, IvarVirusiim, Finite State Machine, Fabian Keller, Tomer Bin, adasium and Cal Jacobson

Activity

  1. edaqa-uncountable
    added
    feature
    on Sep 19, 2022
  2. AlexWaygood
    added
    topic-type-narrowingConditional type narrowing / binder
    on Sep 19, 2022
  3. intgr commented on Apr 25, 2023

    @intgr
    intgr
    on Apr 25, 2023
    Contributor
    More actions

    Related issues for Literal type: #9718, #12535

  4. hauntsaninja commented on Feb 10, 2026

    @hauntsaninja
    hauntsaninja
    on Feb 10, 2026
    Collaborator
    More actions

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

  5. hauntsaninja
    closed this as completedin #21461on May 13, 2026
  6. hauntsaninja
    added a commit that references this issue on May 13, 2026

    Narrow membership in statically known containers (#21461)

    This issue will close once commit d2b0234 is merged into the 'master' branch.
    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

    Labels

    featuretopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.