Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attrs: Fix emulating hash method logic #17016

Merged
merged 2 commits into from
Mar 15, 2024

Conversation

Hnasar
Copy link
Contributor

@Hnasar Hnasar commented Mar 11, 2024

This commit fixes a couple regressions in 1.9.0 from 91be285.

Attrs' logic for hashability is slightly complex:

Mypy now properly emulates attrs' logic so that custom __hash__ implementations are preserved, @frozen subclasses are always hashable, and classes are only made unhashable based on the values of eq and unsafe_hash.

Fixes #17015
Fixes #16556 (comment)

Based on a patch in #17012
Co-Authored-By: Tin Tvrtkovic tinchester@gmail.com

This commit fixes a couple regressions in 1.9.0 from 91be285.

Attrs' logic for hashability is slightly complex:

* https://www.attrs.org/en/stable/hashing.html
* https://github.com/python-attrs/attrs/blob/9e443b18527dc96b194e92805fa751cbf8434ba9/src/attr/_make.py#L1660-L1689

Mypy now properly emulates attrs' logic so that custom `__hash__`
implementations are preserved, `@frozen` subclasses are always hashable,
and classes are only made unhashable based on the values of `eq` and
`unsafe_hash`.

Fixes python#17015
Fixes python#16556 (comment)

Based on a patch in python#17012
Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>
Add another test for parameter precedence
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I checked that the new test cases match runtime behavior.

@JukkaL JukkaL merged commit 31dc503 into python:master Mar 15, 2024
18 checks passed
@Tinche
Copy link
Contributor

Tinche commented Mar 15, 2024

Great job @Hnasar !

@Hnasar Hnasar deleted the fix-attrs-hashability branch March 15, 2024 15:45
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.

1.9 Regression: __hash__ method in attrs class is ignored
3 participants