Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chrisbeardy opened this issue Sep 12, 2023 · 2 comments · May be fixed by Malika1109/mypy#3 or #16593
Open

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

chrisbeardy opened this issue Sep 12, 2023 · 2 comments · May be fixed by Malika1109/mypy#3 or #16593
Labels

Comments

@chrisbeardy
Copy link

chrisbeardy commented Sep 12, 2023

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...).

@JelleZijlstra
Copy link
Member

@bucknathanael please stop posting incorrect ChatGPT responses.

@cj81499
Copy link

cj81499 commented Jun 7, 2024

Possibly related to #12535

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants