Commit a78e60c
authored
Recognize
When this piece of code was checked:
```python
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)`](https://github.com/python/mypy/blob/56684e43a14e3782409c47e99bb47191d631a3de/mypy/typeops.py#L130) was `True`, where `explicit_type=object` and `default_ret_type=Hashable`. It happened because `object` has `__hash__` method.
The only thing that popped 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 #11799Hashable as a real protocol (#11802)1 parent fa9921a commit a78e60c
2 files changed
+17
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1575 | 1575 | | |
1576 | 1576 | | |
1577 | 1577 | | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
1578 | 1591 | | |
1579 | 1592 | | |
1580 | 1593 | | |
| |||
0 commit comments