Possibility to type `tuple.__hash__` to only accept hashable items
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 82
Description
If a tuple contains unhashable entries, hashing it would raise TypeError. Is this possible to type? Currently, tuple.__hash__ is just a regular method that returns int.
This initial idea comes to mind, but on second thought, it would likely break a lot of code:
class tuple(Sequence[_T_co]):
def __hash__(self: tuple[Hashable, ...]) -> int: ...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the typeshed definition of class tuple and its tuple.hash method, as described in the issue. Investigate whether restricting hashing to tuples of Hashable items is sound without breaking existing code, then document a decided typing approach and validate it against relevant type-checking behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100