You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reachability check seems to consider isinstance(obj, Hashable) as always True #11799
We can even exchange the Union annotation for a single unhashable type like List or Dict, and still get the same unreachable error. Looks like isinstance(obj, Hashable) is just considered True for any type.
Is this a mypy bug? Can we add some hints into the example code so that mypy would figure out correct reachability?
The problem is that __hash__ is defined on object as a real method.
reveal_type(object.__hash__) # N: Revealed type is "def (self: builtins.object) -> builtins.int"
We had this problem and discussion several times before.
Maybe we can try to change it to None | def (self: builtins.object) -> builtins.int?
CC @JelleZijlstra
I don't know how we can represent this idea in types, but: some object subtypes are hashable (def (self: builtins.object) -> builtins.int) and some are not (None). But, object defines that all subclasses are hashable by defining __hash__ as def (self: builtins.object) -> builtins.int.
But, on the other hand, we know that object itself is hashable. Only its subtypes can be unhashable.
Activity
sobolevn commentedon Dec 20, 2021
The problem is that
__hash__is defined onobjectas a real method.We had this problem and discussion several times before.
Maybe we can try to change it to
None | def (self: builtins.object) -> builtins.int?CC @JelleZijlstra
JelleZijlstra commentedon Dec 20, 2021
We already have
__hash__: Noneon list in typeshed. Why wouldobjecthave to change?sobolevn commentedon Dec 20, 2021
I don't know how we can represent this idea in types, but: some
objectsubtypes are hashable (def (self: builtins.object) -> builtins.int) and some are not (None). But,objectdefines that all subclasses are hashable by defining__hash__asdef (self: builtins.object) -> builtins.int.But, on the other hand, we know that
objectitself is hashable. Only its subtypes can be unhashable.JelleZijlstra commentedon Dec 20, 2021
But mypy already correctly errors when a subclass of a class that fulfills a protocol doesn't fulfill it:
Why is
Hashabledifferent?sobolevn commentedon Dec 20, 2021
Looks like we have a bug in mypy 🤔
For some reason
mypytreatsHashableas plainobject.I will look into it.
sobolevn commentedon Dec 20, 2021
I hope that #11802 will fix it! 🙏
Recognize `Hashable` as a real protocol (#11802)