python / python/typeshed

Possibility to type `tuple.__hash__` to only accept hashable items

Open
#15,851 2 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.