Skip to content

Incorrect inference for types with __eq__ and no __hash__ #18622

Open
@jorenham

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions