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

Confusing error message for a missing classmethod decorator #11791

Open
wrobell opened this issue Dec 18, 2021 · 7 comments
Open

Confusing error message for a missing classmethod decorator #11791

wrobell opened this issue Dec 18, 2021 · 7 comments
Labels
bug mypy got something wrong diagnostics topic-enum topic-error-reporting How we report errors topic-inheritance Inheritance and incompatible overrides

Comments

@wrobell
Copy link

wrobell commented Dec 18, 2021

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.

@wrobell wrobell added the bug mypy got something wrong label Dec 18, 2021
@sobolevn
Copy link
Member

sobolevn commented Dec 18, 2021

This works for me on latest master when I remove @unique decorator:

import typing as tp
import enum

# @enum.unique
class ColorIndex(enum.IntEnum):
    BLACK = 0

    def _missing_(cls, value: object) -> tp.Any:
        return ColorIndex.BLACK
» mypy out/ex.py --strict-optional --warn-unreachable --show-traceback
out/ex.py:7: error: Signature of "_missing_" incompatible with supertype "Enum"
out/ex.py:7: note:      Superclass:
out/ex.py:7: note:          @classmethod
out/ex.py:7: note:          def _missing_(cls, value: object) -> Any
out/ex.py:7: note:      Subclass:
out/ex.py:7: note:          def _missing_(cls, value: object) -> Any

@sobolevn
Copy link
Member

No, I was wrong. It depends on the cache.
Снимок экрана 2021-12-19 в 1 47 14

@sobolevn
Copy link
Member

This is very hard to solve, but I've got a working prototype. I am going to finish it tomorrow.
Снимок экрана 2021-12-19 в 2 52 54

@wrobell
Copy link
Author

wrobell commented Dec 19, 2021

@sobolevn Thanks for the fix.

I wonder if it is also possible to improve the main error message?

Replace

Signature of "_missing_" incompatible with supertype "Enum"

with

Supertype "Enum" defines method "_missing_" as a class method

?

@sobolevn
Copy link
Member

I think it is better to keep the existing format. It is way more informative.
When it works correctly, it shows everything we need to know.

@wrobell
Copy link
Author

wrobell commented Dec 19, 2021

Agreed on the format. Still, the error message is going to be confusing.

Please consider:

import typing as tp
import enum

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

    @classmethod
    def _missing_(cls, value: object) -> ColorIndex:
        return ColorIndex.BLACK

then

$ mypy t.py
Success: no issues found in 1 source file

Without classmethod decorator:

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

The error message suggests I should change ColorIndex to Any, while it is missing classmethod decorator.

@AlexWaygood AlexWaygood added diagnostics topic-error-reporting How we report errors topic-inheritance Inheritance and incompatible overrides labels Mar 27, 2022
@GabrieleCalarota
Copy link

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong diagnostics topic-enum topic-error-reporting How we report errors topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants