Improve attrs hashability detection#16556
Merged
sobolevn merged 5 commits intopython:masterfrom Dec 12, 2023
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
ikonst
approved these changes
Nov 26, 2023
5711b3a to
6067f38
Compare
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
@sobolevn Does this look good to you? 🙏 |
sobolevn
reviewed
Dec 9, 2023
sobolevn
reviewed
Dec 9, 2023
This comment has been minimized.
This comment has been minimized.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
sobolevn
approved these changes
Dec 9, 2023
Member
sobolevn
left a comment
There was a problem hiding this comment.
Let's wait a bit for more possible input. I will merge this in several days.
|
@Tinche are you sure about the inheritance behavior? I just upgraded to mypy 1.9 and encountered this. I have a In [1]: import attrs
In [2]: @attrs.define
...: class A:
...: a: int
...:
In [3]: @attrs.define(frozen=True)
...: class B(A):
...: b: int
...:
In [4]: hash(B(a=1, b=2))
Out[4]: -1340312973636267308
In [5]: hash(A(a=1))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 hash(A(a=1))
TypeError: unhashable type: 'A'However mypy doesn't seem to think it is: import attrs
import functools
@attrs.define
class A:
a: int
@attrs.define(frozen=True)
class B(A):
b: int
b = B(a=1, b=2)
@functools.lru_cache()
def foo(b: B) -> int:
return b.b
foo(b)This is using attrs 23.3.0, mypy 1.9.0. |
Contributor
Author
|
@brianmedigate I tested the other direction :/ I'll look into handling this case in a new PR. |
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
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>
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16550
Improve hashability detection for attrs classes.
I added a new parameter to
add_attribute_to_class,overwrite_existing, since I needed it.Frozen classes remain hashable, non-frozen default to no. This can be overriden by passing in
hash=Trueorunsafe_hash=True(new parameter, added to stubs) todefine().Inheritance-wise I think we're correct, if a non-hashable class inherits from a hashable one, it's still unhashable at runtime.
Accompanying tests.