You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MyIntEnum.ONE should be passable to functions expecting Literal[1] arguments #19616
This is related but distinct from #19576, #16327, and #17162. It's also not specific to IntEnums, but I use them in the example for clarity. StrEnums as well as enums based on (int, Enum), or (str, Enum), etc.
To Reproduce
importenumfromtypingimportLiteral
type One=Literal[1]
type Two=Literal[2]
type Three=Literal[3]
defoption(arg: One|Two|Three) ->None:
...
classMyEnum(enum.IntEnum):
ONE=1TWO=2THREE=3option(MyEnum.TWO) # error: Argument 1 to "option" has incompatible type "Literal[MyEnum.TWO]"; expected "Literal[1, 2, 3]" [arg-type]
Expected Behavior
No error reported.
Actual Behavior
The error in the comment in the example is a false positive. This code works fine at runtime.
An int Literal value must be an instance of int, not a subclass of int. I recently fixed a bug in pyright to enforce this. See this forum thread for more details.
In your code sample, you can make this comply with the type system by changing the final line to:
@erictraut While I see the point in general, I disagree in particular. I'd argue that IntEnum/StrEnum are special cases, as they are handled in the PR. They aren't arbitrary "int subclasses" but ways to represent int values using namespaced human-readable names. That's exactly how IntEnums were introduced to, say, the socket standard library. I'll respond on the Discourse discussion.
Activity
Make IntEnum/StrEnum values passable to functions expecting literal i…
Make IntEnum/StrEnum values passable to functions expecting literal i…
erictraut commentedon Aug 8, 2025
I think mypy is working correctly here.
An int
Literalvalue must be an instance ofint, not a subclass ofint. I recently fixed a bug in pyright to enforce this. See this forum thread for more details.In your code sample, you can make this comply with the type system by changing the final line to:
ambv commentedon Aug 8, 2025
@erictraut While I see the point in general, I disagree in particular. I'd argue that IntEnum/StrEnum are special cases, as they are handled in the PR. They aren't arbitrary "int subclasses" but ways to represent int values using namespaced human-readable names. That's exactly how IntEnums were introduced to, say, the
socketstandard library. I'll respond on the Discourse discussion.Add test for Literal and enum incompatibility
Add test for Literal and enum incompatibility
Add test for Literal and enum incompatibility
Add test for Literal and enum incompatibility (#20613)