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

Enums and Literals: false positive with @overload/def method(self: Literal[SYMBOL], ...): #15456

Open
finite-state-machine opened this issue Jun 17, 2023 · 1 comment
Labels
bug mypy got something wrong topic-enum topic-overloads

Comments

@finite-state-machine
Copy link

finite-state-machine commented Jun 17, 2023

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
@finite-state-machine finite-state-machine added the bug mypy got something wrong label Jun 17, 2023
@finite-state-machine finite-state-machine changed the title 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 Apr 22, 2024
@finite-state-machine finite-state-machine changed the title 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], ...): Apr 22, 2024
@finite-state-machine
Copy link
Author

Likely related: #14764

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-enum topic-overloads
Projects
None yet
Development

No branches or pull requests

2 participants