KEMBAR78
Recognize `Hashable` as a real protocol, refs #11799 by sobolevn · Pull Request #11802 · python/mypy · GitHub
Skip to content

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Dec 20, 2021

Sorry for spamming with PR, but this problem caught my eye 🙂

When this piece of code was checked:

from typing import Awaitable, Hashable, Union, Tuple, List

obj: Union[Tuple[int], List[int]]
if isinstance(obj, Hashable):
    reveal_type(obj)

Mypy revealed that Hashable is () -> object. It happened, because is_subtype(explicit_type, default_ret_type, ignore_type_params=True) was True, where explicit_type=object and default_ret_type=Hashable. It happened because object has __hash__ method.

The only thing that poped out of my head is to simply exclude protocols from this condition.
I guess that we might double check protocols with __new__ and __init__ to be sure. But, I am not able to think of proper test cases for this. Any ideas? Or is my single test good enough?

I am adding pythoneval test, because of the complexity in how Hashable is defined and analyzed in real life.

Closes #11799

@JelleZijlstra JelleZijlstra self-assigned this Mar 21, 2022
@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit a78e60c into python:master Mar 24, 2022
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

Successfully merging this pull request may close these issues.

Reachability check seems to consider isinstance(obj, Hashable) as always True

2 participants