Skip to content

dataclasses with generated __hash__ subclassing Hashable are considered abstract #18962

Open
@ralismark

Description

@ralismark

Bug Report

Dataclasses that are hashable (e.g. frozen=True) that extend any abc/protocol requiring __hash__ (e.g. collections.abc.Hashable) is considered abstract and gives an error when trying to be instantiated.

In addition to frozen=True/Hashable, you also get the same error with unsafe_hash=True, as well as using your own protocol or abc that requires __hash__.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=390504f01055e489b12b388eec1de256

from dataclasses import dataclass
from collections.abc import Hashable


@dataclass(frozen=True)
class A:
    a: int


@dataclass(frozen=True)
class B(Hashable):
    a: int


a: Hashable = A(1)  # ok
b = B(1)  # error: Cannot instantiate abstract class "A" with abstract attribute "__hash__"  [abstract]

print(hash(b))  # prints -6644214454873602895

Expected Behavior

This works at runtime -- running the script prints the hash, demonstrating that it has __hash__ defined.

Actual Behavior

main.py:16: error: Cannot instantiate abstract class "B" with abstract attribute "__hash__"  [abstract]

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions