Skip to content

Incorrect inference for types with __eq__ and no __hash__ #18622

Open

Description

@jorenham

problem

class A:
    def __eq__(self, o: object, /) -> bool:
        return self is o

reveal_type(A().__hash__)  # def () -> builtins.int

https://mypy-play.net/?mypy=latest&python=3.13&flags=strict%2Clocal-partial-types&gist=2b23624b1df772175b70c08e36a6e912

yet

>>> class A:
...     def __eq__(self, o: object, /) -> bool:
...         return self is o
...         
>>> type(A.__hash__)
<class 'NoneType'>

solution

iff. class A, and only class A (i.e. excluding any base classes)

  • implements __eq__, and
  • does not implement __hash__

infer A.__hash__: ClassVar[None]

Activity

  1. added
    bug
    on Feb 6, 2025
  2. added
    topic-runtime-semantics
    on Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic-runtime-semantics

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions