Skip to content

MyPy doesn't seem to know that dataclasses add __eq__ #13836

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

Open
henryiii opened this issue Oct 7, 2022 · 2 comments
Open

MyPy doesn't seem to know that dataclasses add __eq__ #13836

henryiii opened this issue Oct 7, 2022 · 2 comments
Labels
bug mypy got something wrong topic-dataclasses

Comments

@henryiii
Copy link
Contributor

henryiii commented Oct 7, 2022

Bug Report

MyPy doesn't seem to understand that dataclasses can generate an __eq__ method. If it's required (such as in an ABC), then it doesn't know that it was created.

To Reproduce

import dataclasses
import abc

@dataclasses.dataclass(eq=False)
class AbstractEq(abc.ABC):
    @abc.abstractmethod
    def __eq__(self, other: object) -> bool:
        pass

@dataclasses.dataclass
class ConcreteEq(AbstractEq):
    x: int

print(ConcreteEq(1) == ConcreteEq(1))

Gist URL: https://gist.github.com/89d988a5c8d37bd10d17445d5e344e63
Playground URL: https://mypy-play.net/?mypy=latest&python=3.10&gist=89d988a5c8d37bd10d17445d5e344e63

Expected Behavior

This should pass. It does at runtime. dataclasses do add __eq__ unless eq=False.

Actual Behavior

main.py:14: error: Cannot instantiate abstract class "ConcreteEq" with abstract attribute "__eq__"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.981 (playground) and 0.982 (local)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10

I didn't see any other issues that were exactly this issue.

@henryiii henryiii added the bug mypy got something wrong label Oct 7, 2022
@AlexWaygood
Copy link
Member

I didn't see any other issues that were exactly this issue.

Sort of a duplicate of #12186, but you've put forward a better reason for fixing the bug than my reason :)

@dendron2000
Copy link

dendron2000 commented Jan 21, 2023

I can confirm the issue. I may be wrong, but I think it might be related to #8638. Could it be that the code for dataclass.__eq__ was simply removed without being reworked?

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-dataclasses
Projects
None yet
Development

No branches or pull requests

3 participants