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 exhaustiveness checking for 'in' statements (enums) · Issue #16093 · python/mypy

Support exhaustiveness checking for 'in' statements (enums) #16093

New issue
Copy link
New issue
Copy link
Closed
#21456
Malika1109/mypy
#3
Malika1109/mypy#3
Closed
Support exhaustiveness checking for 'in' statements (enums)#16093
#21456
Malika1109/mypy
#3
Copy link
Labels
feature

Description

@chrisbeardy
chrisbeardy
opened on Sep 12, 2023
Issue body actions

Feature

Allow the use of the in statement when doing exhaustiveness checking.

from enum import Enum
from typing import assert_never


class MyEnum(Enum):
    A = 1
    B = 2
    C = 3 


def my_function(a: MyEnum) -> bool:
    if a == MyEnum.A:
        return True
    elif a in (MyEnum.B, MyEnum.C):
        return False
    assert_never(a)


my_function(MyEnum.A)

Currently, this will raise the following exception:

error: Argument 1 to "assert_never" has incompatible type
"Literal[MyEnum.B, MyEnum.C]"; expected "NoReturn"  [arg-type]
        assert_never(a)

where the following would not:

def my_function(a: MyEnum) -> bool:
    if a == MyEnum.A:
        return True
    elif a == MyEnum.B:
        return False
    elif a == MyEnum.C:
        return False
    assert_never(a)

Pitch

If you wish to do the same thing for multiple enums in that particular function and also want exhaustiveness checking, you need to type each case out individually and then return the same thing, which is not great (DRY etc...).

Reacted by Cal Jacobson, Oleg Levantin, Miikka Koskinen and quentin-ag

Activity

  1. chrisbeardy
    added
    feature
    on Sep 12, 2023
  2. JelleZijlstra commented on Sep 21, 2023

    @JelleZijlstra
    JelleZijlstra
    on Sep 21, 2023
    Member
    More actions

    @bucknathanael please stop posting incorrect ChatGPT responses.

  3. cj81499 commented on Jun 7, 2024

    @cj81499
    cj81499
    on Jun 7, 2024
    More actions

    Possibly related to #12535

  4. hauntsaninja
    closed this as completedin #21456on May 11, 2026
  5. hauntsaninja
    added a commit that references this issue on May 11, 2026

    Improve negative narrowing for membership checks on tuples (#21456)

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

    feature

    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.