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
> mypy .\test_hashable.py
test_hashable.py:7: error: Incompatible types in assignment (expression has type "List[int]", variable has type "List[Hashable]")
test_hashable.py:10: error: Incompatible types in assignment (expression has type "Set[int]", variable has type "Set[Hashable]")
test_hashable.py:15: error: Argument 1 to "f" has incompatible type "Set[int]"; expected "Set[Hashable]"
> mypy -V
mypy 0.701
> python -V
Python 3.7.3
int itself seems to be recognized as Hashable correctly since #1746, but collections of Hashable seems not accepting collections of int.
Is this behavior expected and I'm doing something wrong? If so, then what type annotaion should I write when I want a collection of dictionary keys?
The text was updated successfully, but these errors were encountered:
This is related to invariance; you can read up on this in the docs. Your code is unsafe because something like this could happen:
x: List[int] = [1, 2, 3]
y: List[Hashable] =xy.append("string")
forelementinx:
print(element+1) # blows up because x now contains a string, even though it's a List[int]
You can fix type checking by using covariant container types like Sequence or Iterable instead.
int
itself seems to be recognized asHashable
correctly since #1746, but collections ofHashable
seems not accepting collections ofint
.Is this behavior expected and I'm doing something wrong? If so, then what type annotaion should I write when I want a collection of dictionary keys?
The text was updated successfully, but these errors were encountered: