DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
http://docs.basedpyright.com/
Other
601 stars 12 forks source link

`object.__eq__` should only allow `Self` as the other argument #451

Open DetachHead opened 1 week ago

DetachHead commented 1 week ago

could this be fixed by updating the __eq__ from object to only allow Self:

def __eq__(self, __value: Self) -> bool: ...

because currently since it takes object no subtypes can override it with something more specific. your example currently doesnt work for that reason:

class SupportsEq(Protocol[T]):
    @override
    def __eq__(self, other: T) -> bool: ... # error: error: Argument 1 of "__eq__" is incompatible with supertype "object"; supertype defines the argument type as "object"

but if object's __eq__ used the Self type, then a subtype could just do this and there'd be no issue:

class A:
    @override
    def eq(self, other: Self | int) -> bool: ...

Originally posted by @DetachHead in https://github.com/KotlinIsland/basedmypy/issues/574#issuecomment-1890800523

DetachHead commented 1 week ago

probably related: #391