8000 Should `object.__eq__` return `object`? · Issue #7150 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

Should object.__eq__ return object? #7150

@kmillikin

Description

@kmillikin

In builtins.pyi there is:

def __eq__(self, __o: object) -> bool: ...
def __ne__(self, __o: object) -> bool: ...

But I think that both of these methods should return object. I believe that they eventually get called in CPython by tp_richcompare which doesn't necessarily return bool (https://github.com/python/cpython/blob/1626bf4ac7aef1244e6f886e63a31f7ed65fbd10/Objects/object.c#L655).

There is Python code that takes advantage of this to override == to return something other than a bool (e.g., numpy arrays). Type checking code like that against the typeshed type stubs leads to incompatible overrides (e.g., numpy array is not a subtype of bool).

Should these methods be annotated as returning object?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0