python / python/mypy

`mypy` doesn't infer `__eq__` and `__hash__` implementations for dataclasses

Open
#19,799 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

mypy doesn't infer __eq__ and __hash__ implementations for dataclasses.

To Reproduce

from abc import ABC, abstractmethod
from dataclasses import dataclass


class FType(ABC):
    @abstractmethod
    def __eq__(self, other): ...

    @abstractmethod
    def __hash__(self): ...


@dataclass(eq=True, frozen=True)
class ConcreteType(FType):
    a: int
    b: str


if __name__ == "__main__":

    ct = ConcreteType(1, "b")

    print(ct.__eq__)

Expected Behavior

It should pass.

Actual Behavior

mypy_playground.py:21: error: Cannot instantiate abstract class "ConcreteType" with abstract attributes "__eq__" and "__hash__"  [abstract]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.17.1

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 reproduction in mypy_playground.py with mypy 1.17.1 and confirm the abstract-class error for ConcreteType. Trace how dataclasses and abstract methods are analyzed, then verify that the reproduction no longer reports abstract eq and hash attributes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.