Skip to content

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

Open

Description

@finite-state-machine

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

  1. 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[/+] on Apr 22, 2024
  2. 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], ...):`[/+] on Apr 22, 2024
  3. finite-state-machine commented on Apr 22, 2024

    @finite-state-machine
    Author

    Likely related: #14764

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions