Skip to content

Mypy incorrectly infers that parameters are positional-only for __eq__ methods autogenerated with @dataclass #12186

Open
@AlexWaygood

Description

@AlexWaygood

For a class decorated with @dataclass, mypy assumes that the class's __eq__ method will have the exact same signature as object.__eq__. However, this is subtly incorrect. Whereas the signature of object.__eq__ is def __eq__(self, other: object, /) -> bool: ..., the signature of MyDataclass.__eq__ is def __eq__(self, other: object) -> bool: .... I.e., for the method auto-generated by @dataclass, the second argument is positional-or-keyword; whereas the second argument is positional-only for object.__eq__.

Minimal repro (using mypy 0.931, Python 3.10 -- try it on mypy playground here):

from dataclasses import dataclass

@dataclass
class Foo: ...

reveal_type(Foo.__eq__)  # Revealed type is "def (builtins.object, builtins.object) -> builtins.bool"

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions