python / python/mypy

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

Open
#15,456 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-enum topic-overloads
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked mypy-play reproducer with the shown Python 3.11 example and inspect the erased-type-of-self [misc] check across the overloads. Done means the Enum and Literal overloads produce no errors while preserving the reported behavior for invalid cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.