Skip to content

attrs: Fix emulating hash method logic - #17016

Merged
JukkaL merged 2 commits into
python:masterfrom
Hnasar:fix-attrs-hashability
Mar 15, 2024
Merged

JukkaL merged 2 commits into
python:masterfrom
Hnasar:fix-attrs-hashability

Conversation

@Hnasar

@Hnasar Hnasar commented Mar 11, 2024

Copy link
Copy Markdown
Contributor

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 [email protected]

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 <[email protected]>
@Hnasar
Hnasar force-pushed the fix-attrs-hashability branch from a663346 to d40909e Compare March 12, 2024 00:03
Add another test for parameter precedence
@github-actions

Copy link
Copy Markdown
Contributor

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

@JukkaL JukkaL 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.

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
@Tinche

Tinche commented Mar 15, 2024

Copy link
Copy Markdown
Contributor

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