Skip to content
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

Non-hashable types should declare __hash__: None in typeshed #2148

Closed
ilevkivskyi opened this issue May 18, 2018 · 7 comments · Fixed by #3219
Closed

Non-hashable types should declare __hash__: None in typeshed #2148

ilevkivskyi opened this issue May 18, 2018 · 7 comments · Fixed by #3219
Labels
stubs: false negative Type checkers do not report an error, but should stubs: false positive Type checkers report false errors

Comments

@ilevkivskyi
Copy link
Member

Currently this passes mypy:

from typing import Hashable

def f(x: Hashable):
    pass

f([])

A possible solution may be to add __hash__: None to non-hashable types like list.

@JelleZijlstra
Copy link
Member

I tried doing this in #2221, but I get errors like:

stdlib/3/builtins.pyi:515: error: Incompatible types in assignment (expression has type "None", base class "object" defined the type as "Callable[[object], int]")

We probably need some more special casing in mypy first.

@ilevkivskyi
Copy link
Member Author

We probably need some more special casing in mypy first.

Yes. This particular one can be fixed by changing definition on object to Optional[...], but there are other errors, for example one can define a hashable subclass of dict. Simplest solution is to just silence the error if is because of a dunder with None type in superclass.

@gvanrossum
Copy link
Member

We probably need some more special casing in mypy first.

Can you file a separate mypy issue for that? Having that fixes is a prerequisite for fixing this (and hence for #2221).

@srittau
Copy link
Collaborator

srittau commented Nov 2, 2018

mypy issue is now python/mypy#4266.

@ilevkivskyi
Copy link
Member Author

Note that this can also cause false positives, see python/mypy#7328.

Honestly, I don't think having few # type: ignores is a big problem, maybe we should revive #2221 to mitigate this until we implement some special-casing in mypy?

@ilevkivskyi ilevkivskyi added the stubs: false positive Type checkers report false errors label Aug 13, 2019
@srittau
Copy link
Collaborator

srittau commented Aug 13, 2019

So from what I understand we would add __hash__ = None # type: ignore to unhashable classes? I'm fine with that. (Although not needing # type: ignore would be better, of course.)

@ilevkivskyi
Copy link
Member Author

So from what I understand we would add __hash__ = None # type: ignore to unhashable classes?

Yes, or maybe better __hash__: None # type: ignore (to have a consistent style, plus IIRC pytype likes the latter more).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false negative Type checkers do not report an error, but should stubs: false positive Type checkers report false errors
Projects
None yet
4 participants