python / python/mypy

Confusing error message for a missing classmethod decorator

Open
#11,791 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug diagnostics topic-enum topic-error-reporting topic-inheritance
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Please consider

import typing as tp
import enum

@enum.unique
class ColorIndex(enum.IntEnum):
    BLACK = 0
    GREY = 1
    WHITE = 2

    def _missing_(cls, value: object) -> tp.Any:
        return ColorIndex.BLACK

then mypy shows very confusing error (both signatures, for the subclass and the superclass, are the same)

$ mypy t.py
t.py:10: error: Signature of "_missing_" incompatible with supertype "Enum"
t.py:10: note:      Superclass:
t.py:10: note:          def _missing_(cls, value: object) -> Any
t.py:10: note:      Subclass:
t.py:10: note:          def _missing_(cls, value: object) -> Any
Found 1 error in 1 file (checked 1 source file)

The real issue is that _missing_ should be decorated with classmethod.

Python 3.10.1, mypy 0.930.

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 reproducing the supplied Python example with mypy and trace the override diagnostic that reports the identical superclass and subclass signatures. Look for the existing regression tests around method override or decorator handling. Done means the diagnostic explains that missing needs a classmethod decorator and the example is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.