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 enum type narrowing with in #13684

Open
edaqa-uncountable opened this issue Sep 19, 2022 · 1 comment
Open

Support enum type narrowing with in #13684

edaqa-uncountable opened this issue Sep 19, 2022 · 1 comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder

Comments

@edaqa-uncountable
Copy link

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()
@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label Sep 19, 2022
@intgr
Copy link
Contributor

intgr commented Apr 25, 2023

Related issues for Literal type: #9718, #12535

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

3 participants