Repository navigation
Enums and Literals: false positive with @overload/def method(self: Literal[SYMBOL], ...): #15456
Description
Bug Report
This is probably the same underlying cause as issue #11759, but the proposed workaround there doesn't seem particularly appropriate for the case of Enums and Literals. I'd like to respectfully suggest that the issue be reconsidered in that light.
I apologize if adding a comment to the closed issue was the right thing to do; I've filed a new issue out of concern that comments on a closed issue may not be noticed by folks' triage workflows.
As always: my thanks for this indispensable tool!
Hypothesis
The "erased type of self" ([misc]) check applies individually to each @overload of a method, and not to the signature of the method as a whole. This results in undesirable errors when different @overloads in combination handle the entire type of self (as with Literals that add up to the entire Enum or both values of bool), and even when one of the other @overloads handles the erased self type in full (as with the third @overload below).
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=52fd5335c1b73b28275aab67c2e01568
from __future__ import annotations
from enum import (
auto,
Enum,
)
from typing import (
Literal,
overload,
)
class SomeEnum(Enum):
ALFA = auto()
BRAVO = auto()
CHARLIE = auto()
ZERO = auto()
@overload
def is_letter(self: Letter) -> Literal[True]: ...
# mypy error: [misc] (paraphrased for brevity):
# The erased type of self "Literal[ALFA, BRAVO,
# CHARLIE]" is not a supertype of its class "SomeEnum"
@overload
def is_letter(self: Digit) -> Literal[False]: ...
# mypy error: [misc] (paraphrased for brevity):
# The erased type of self "Literal[ZERO]" is not a
# supertype of its class "SomeEnum"
@overload
def is_letter(self) -> bool: ...
def is_letter(self) -> bool:
return self is not SomeEnum.ZERO
Letter = Literal[SomeEnum.ALFA, SomeEnum.BRAVO, SomeEnum.CHARLIE]
Digit = Literal[SomeEnum.ZERO]Expected Behavior
No errors are expected, since the overall signature of is_letter() accepts all possible values of self.
Actual Behavior
main.py:19: error: The erased type of self "Union[Literal[__main__.SomeEnum.ALFA], Literal[__main__.SomeEnum.BRAVO], Literal[__main__.SomeEnum.CHARLIE]]" is not a supertype of its class "__main__.SomeEnum" [misc]
main.py:24: error: The erased type of self "Literal[__main__.SomeEnum.ZERO]" is not a supertype of its class "__main__.SomeEnum" [misc]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.9.0, master as of 2024-03-28
- Mypy command-line flags: (none required)
- Mypy configuration options from
mypy.ini(and other config files): (none) - Python version used: several, including CPython 3.11, 3.12
Activity
[-]Enums and Literals: Overloads in super-class for sub-class specific return types (revisited)[/-][+]Enums and Literals: false positive where `@overload` and `def ...(self: Literal[SYMBOL], ...)` are used together[/+][-]Enums and Literals: false positive where `@overload` and `def ...(self: Literal[SYMBOL], ...)` are used together[/-][+]Enums and Literals: false positive with `@overload`/`def method(self: Literal[SYMBOL], ...):`[/+]finite-state-machine commentedon Apr 22, 2024
Likely related: #14764