KEMBAR78
Fix attrs hashability detection when inheriting from mutable by Tinche · Pull Request #17012 · python/mypy · GitHub
Skip to content

Conversation

@Tinche
Copy link
Contributor

@Tinche Tinche commented Mar 10, 2024

Frozen attrs classes inheriting from mutable classes are hashable; fix this case and add a test.

Also support frozen classes with hash=False (although I'm not sure this is a common use case at all, but it was easy to add).

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@Tinche Tinche force-pushed the tin/fix-attrs-frozen-hashability branch from 633db11 to 45454ef Compare March 11, 2024 19:48
@Tinche
Copy link
Contributor Author

Tinche commented Mar 11, 2024

@sobolevn Ready for review, if you have time!

@github-actions
Copy link
Contributor

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

Hnasar pushed a commit to Hnasar/mypy that referenced this pull request Mar 11, 2024
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>
Hnasar added a commit to Hnasar/mypy that referenced this pull request Mar 11, 2024
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>
Hnasar added a commit to Hnasar/mypy that referenced this pull request Mar 11, 2024
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>
Hnasar added a commit to Hnasar/mypy that referenced this pull request Mar 11, 2024
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>
Hnasar added a commit to Hnasar/mypy that referenced this pull request Mar 11, 2024
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>
@Hnasar
Copy link
Contributor

Hnasar commented Mar 11, 2024

Thanks for the quick fix @Tinche ! I was curious to learn myself how attrs handles this, so I ended up cloning your fix and testing it out, but realized that the logic didn't always accurately match attrs'. And also it didn't fix the regression I reported in #17015. So I reworked some of the subtle issues with this patch and added several other test cases here #17016

Hnasar added a commit to Hnasar/mypy that referenced this pull request Mar 12, 2024
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>
@sobolevn
Copy link
Member

@Tinche can you please first decide which PR should be reviewed? :)

@Tinche
Copy link
Contributor Author

Tinche commented Mar 12, 2024

@Hnasar Good job! Let's use yours since it's more comprehensive. cc @sobolevn

@Tinche Tinche closed this Mar 12, 2024
JukkaL pushed a commit that referenced this pull request Mar 15, 2024
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 #17015
Fixes #16556 (comment)

Based on a patch in #17012
Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>
Co-authored-by: Hashem Nasarat <hashem@hudson-trading.com>
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.

3 participants