mypy icon indicating copy to clipboard operation
mypy copied to clipboard

MyPy doesn't seem to know that dataclasses add __eq__

Open henryiii opened this issue 3 years ago • 2 comments

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 avatar Oct 07 '22 22:10 henryiii

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 :)

AlexWaygood avatar Oct 07 '22 22:10 AlexWaygood

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?

dendron2000 avatar Jan 21 '23 17:01 dendron2000