Skip to content

Support subtyping Literal with StrEnum and IntEnum values #19243

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

Open
randolf-scholz opened this issue Jun 6, 2025 · 2 comments
Open

Support subtyping Literal with StrEnum and IntEnum values #19243

randolf-scholz opened this issue Jun 6, 2025 · 2 comments

Comments

@randolf-scholz
Copy link
Contributor

randolf-scholz commented Jun 6, 2025

Feature

The following (mypy playground) should pass, i.e. recognize StrEnum values as subtypes of the corresponding Literal.

from enum import StrEnum
from typing import Literal

class Options(StrEnum):
    X = "X"
    Y = "Y"
    
x: Literal["X"] = Options.X  # mypy: incompatible types in assignment
@JelleZijlstra
Copy link
Member

This seems incorrect to me. A Literal represents a value of that exact type, not a similar value of a subtype. Pyright isn't even consistent about it; it doesn't treat Literal[False] as a subtype of Literal[0].

This also allows unsound behavior in pyright:

from enum import IntEnum
from typing import Literal

class X(IntEnum):
    a = 1

    def __add__(self, value: int, /) -> int:
        return self.value + value + 42

def f(x: Literal[1]):
    print(reveal_type(x + 1))  # 44 at runtime

f(X.a)

Also, ty doesn't actually support this; it just doesn't support enums and therefore treats enum members as Any.

@erictraut
Copy link

I agree with Jelle's argument here, and I plan to back out the change to pyright.

@sterliakov sterliakov added the pending Issues that may be closed label Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants