-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
x has incompatible type "int"; expected "Hashable" #2412
Comments
This is a complex issue. I suppose we could try to "solve" it in typeshed by making int, and float, and str, and lots of other types, inherit from Hashable -- but that's not really enough, given this comment in the definition of Hashable in stdlib/3/typing.pyi there: class Hashable(metaclass=ABCMeta):
# TODO: This is special, in that a subclass of a hashable class may not be hashable
# (for example, list vs. object). It's not obvious how to represent this. This class
# is currently mostly useless for static checking.
@abstractmethod
def __hash__(self) -> int: ... Note that even in Python itself Hashable is special -- it overrides isinstance() to dynamically check for Do you have a work-around? I can't think of a really good one apart from from typing import TYPE_CHECKING
if TYPE_CHECKING:
Hashable = object
else:
from typing import Hashable |
I don't. I'll take the workaround, but I'm all for a clean solution. So if I have to wait for structural type checking to land, I'll wait. I came accross several issues linked to duck typing, so I guess they all require structural type checking. I can # type: ignore those for now. |
So I'm not sure if there's a benefit to keeping this issue open then. It's waiting for a major feature to be designed and land, so it'll just stay open for a long time, and once that feature lands, it's presumably a typeshed issue to mark up Hashable as a protocol, not an additional mypy action item. |
Right. If you need beta testers for the feature in the future, I'll spend some time doing it. I'm currently using a lot asyncio and mypy for various projects, and testing is the least I can do. |
I used
collections.Hashable
as a type for something expected to be a dictionary key. Passing an integer gives me the errorx has incompatible type "int"; expected "Hashable"
, despite the fact thatisinstance(1, Hashable) == True
.The text was updated successfully, but these errors were encountered: