Skip to content

Attempt to narrow tuple.__hash__ to flag unhashable entries in the tuple - #15852

Merged
srittau merged 10 commits into
python:mainfrom
jonathandung:patch-3
Oct 14, 2026
Merged

srittau merged 10 commits into
python:mainfrom
jonathandung:patch-3

Conversation

@jonathandung

Copy link
Copy Markdown
Contributor

Closes #15851.

@github-actions

This comment has been minimized.

@jonathandung

Copy link
Copy Markdown
Contributor Author

That was somewhat surprising.

@srittau

srittau commented Jun 1, 2026 •

Copy link
Copy Markdown
Collaborator

Could you add some tests to stdlib/@tests/test_cases/builtins/check_tuple.py? Especially what happens if the element are hashable/not hashable, and testing with zero-length, variable length, and fixed length tuples. See the other tests in stdlib/@tests/test_cases for examples on how to write tests.

Important: The tests are never run, only type checked, so they don't need to be runnable, and should skip unnecessary setup etc.

@JelleZijlstra

Copy link
Copy Markdown
Member

Both tuples and hashing are special-cased by type checkers so it's not entirely surprising that this appears to do nothing to mypy. Worth checking what we do to other type checkers, though.

@AlexWaygood

AlexWaygood commented Jun 1, 2026 •

Copy link
Copy Markdown
Member

I'll do an experimental PR to ty to see what it does to us. Hashable causes lots of trouble for ty in general due to the LSP violations everywhere regarding Hashable so it's possible it might be more problematic for us (but let's see)

Edit: testing this in astral-sh/ruff#25526

@AlexWaygood

Copy link
Copy Markdown
Member

The experiment gave a clean ecosystem report for ty (again, somewhat surprising 😄): astral-sh/ruff#25526 (comment). There are a few failing tests on that PR but they're nothing to worry about -- just snapshots that would need to be updated.

@JelleZijlstra

Copy link
Copy Markdown
Member

If it doesn't do anything then that also means there's little reason to make this change, though.

@jonathandung

Copy link
Copy Markdown
Contributor Author

I think after merging #15754 (if ever), some errors are sure to emerge

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Refactor Tuple type annotations to use built-in tuple.
@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

scipy-stubs (https://github.com/scipy/scipy-stubs)
- tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is special-cased in type checkers, it still makes sense to me to be precise. Stubs are potentially used for other uses or for new type checkers that are not special casing this as much.

@srittau
srittau merged commit 9755c8b into python:main Oct 14, 2026
88 checks passed
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.

Possibility to type tuple.__hash__ to only accept hashable items

4 participants