Skip to content

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

Open
jonathandung wants to merge 1 commit into
python:mainfrom
jonathandung:patch-3
Open

Attempt to narrow tuple.__hash__ to flag unhashable entries in the tuple#15852
jonathandung wants to merge 1 commit into
python:mainfrom
jonathandung:patch-3

Conversation

@jonathandung
Copy link
Copy Markdown
Contributor

Closes #15851.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 1, 2026

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@jonathandung
Copy link
Copy Markdown
Contributor Author

That was somewhat surprising.

@srittau
Copy link
Copy Markdown
Collaborator

srittau commented Jun 1, 2026

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
Copy link
Copy Markdown
Member

AlexWaygood commented Jun 1, 2026

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

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