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
reportUnhashable in isinstance(x, set) clause when using recursive TypeAlias. #7824
This behavior is intended, so I don't consider this a bug.
A Mapping object does not meet the requirements of hashability, so if you attempt to add a Mapping object to a set, you will see an error:
deffunc1(obj: Mapping):
x= {obj} # Type error: obj is not hashable
In your first example (foo), you are using a type guard to narrow the type of obj from T_co | Collection[T_co] | Mapping[Any, T_co] to T_co | Collection[T_co], so when you iterate over the items in each of these, you do not receive any Mapping object.
In your second example (bar), when you iterate over the items in obj, one of the subtypes is Mapping`, which is not hashable.
Mypy doesn't emit an error here because it doesn't provide checks for hashability.
If you don't want pyright to check for hashability, you can disable the reportUnhashable check.
Eric Traut (@erictraut) We already know it's a set inside the if isinstance clause, so it is known that the elements are hashable! After your comment, I checked typeshed and was surprised that they do not bind the TypeVar of set to Hashable:
Activity
erictraut commentedon May 2, 2024
This behavior is intended, so I don't consider this a bug.
A
Mappingobject does not meet the requirements of hashability, so if you attempt to add aMappingobject to a set, you will see an error:In your first example (
foo), you are using a type guard to narrow the type ofobjfromT_co | Collection[T_co] | Mapping[Any, T_co]toT_co | Collection[T_co], so when you iterate over the items in each of these, you do not receive anyMappingobject.In your second example (
bar), when you iterate over the items in obj, one of the subtypes isMapping`, which is not hashable.Mypy doesn't emit an error here because it doesn't provide checks for hashability.
If you don't want pyright to check for hashability, you can disable the
reportUnhashablecheck.randolf-scholz commentedon May 2, 2024
Eric Traut (@erictraut) We already know it's a set inside the
if isinstanceclause, so it is known that the elements are hashable! After your comment, I checkedtypeshedand was surprised that they do not bind the TypeVar ofsettoHashable:https://github.com/python/typeshed/blob/5883898b0ab08b7ad306e37ca70dd9e646b4ce2e/stdlib/builtins.pyi#L1111
python/typeshed#9571
This seems to be the real underlying issue here.