Skip to content

Hashable doesn't work #1746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
miedzinski opened this issue Jun 25, 2016 · 6 comments
Closed

Hashable doesn't work #1746

miedzinski opened this issue Jun 25, 2016 · 6 comments

Comments

@miedzinski
Copy link
Contributor

Typechecking Hashable seems broken.

from typing import Hashable


def f(x: Hashable):
    pass


f(0)
f('x')
$ mypy mypy.py
mypy.py:8: error: Argument 1 to "f" has incompatible type "int"; expected "Hashable"
mypy.py:9: error: Argument 1 to "f" has incompatible type "str"; expected "Hashable"
$ mypy -V
mypy 0.4.3-dev
@gvanrossum
Copy link
Member

There are two solutions here; mypy could learn that Hashable is related to
hash, or we could inherit int, str etc. from it explicitly in typeshed.

--Guido (mobile)

@miedzinski
Copy link
Contributor Author

Second option doesn't fix an issue with classes that have __hash__ defined.

Is there any chance this will be put somewhere in roadmap?

@gvanrossum
Copy link
Member

I agree (1) isn't very attractive. In terms of roadmap it would be part of "structural type checking". python/typing#11

Maybe we should just close it here? There's already lots of discussion at that issue.

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 26, 2016

object is hashable, so just inheriting Hashable or is not a good approach, as then every type would be hashable. I think that mypy would have to know about the __hash__ = None idiom for marking a type as not hashable. This clearly breaks Liskov substitutability but mypy could still catch some clear violations such as using a list as a dictionary key even if it wouldn't be sound.

@pkch
Copy link
Contributor

pkch commented Apr 9, 2017

In addition, currently deriving from Hashable breaks the normal hashability because typing.Hashable is a non-virtual subclass of collections.abc.Hashable, and the latter disables hashability by overriding object.__hash__ with a virtual method __hash__:

from typing import Hashable
class X(Hashable): ...
x = X() # Can't instantiate abstract class X with abstract methods

This is something that maybe can be documented or fixed in the process of future upgrades to Hashable.

@ilevkivskyi
Copy link
Member

Superseded by python/typeshed#2148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants