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

Recognize Hashable as a real protocol, refs #11799 #11802

Merged
merged 2 commits into from
Mar 24, 2022
Merged

Recognize Hashable as a real protocol, refs #11799 #11802

merged 2 commits into from
Mar 24, 2022

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