Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve attrs hashability detection#16556

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
sobolevn merged 5 commits intopython:masterfromTinche:tin/fix-attrs-hash
Dec 12, 2023

Conversation

@Tinche
Copy link
Contributor

Fixes#16550

Improve hashability detection for attrs classes.

I added a new parameter toadd_attribute_to_class,overwrite_existing, since I needed it.

Frozen classes remain hashable, non-frozen default to no. This can be overriden by passing inhash=True orunsafe_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.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Tinche
Copy link
ContributorAuthor

@sobolevn Does this look good to you? 🙏

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

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

Copy link
Member

@sobolevnsobolevn left a comment

Choose a reason for hiding this comment

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

Let's wait a bit for more possible input. I will merge this in several days.

@sobolevnsobolevn merged commit91be285 intopython:masterDec 12, 2023
@brianmedigate
Copy link

brianmedigate commentedMar 10, 2024
edited
Loading

@Tinche are you sure about the inheritance behavior? I just upgraded to mypy 1.9 and encountered this. I have afrozen=True attrs class which inherits from a non-frozen one, and it does seem to be hashable at runtime:

In [1]:importattrsIn [2]: @attrs.define   ...:classA:   ...:a:int   ...:In [3]: @attrs.define(frozen=True)   ...:classB(A):   ...:b:int   ...:In [4]:hash(B(a=1,b=2))Out[4]:-1340312973636267308In [5]:hash(A(a=1))---------------------------------------------------------------------------TypeErrorTraceback (mostrecentcalllast)CellIn[5],line1---->1hash(A(a=1))TypeError:unhashabletype:'A'

However mypy doesn't seem to think it is:

importattrsimportfunctools@attrs.defineclassA:a:int@attrs.define(frozen=True)classB(A):b:intb=B(a=1,b=2)@functools.lru_cache()deffoo(b:B)->int:returnb.bfoo(b)
$ mypy test.pytest.py:23: error: Argument 1 to "__call__" of "_lru_cache_wrapper" has incompatible type "B"; expected "Hashable"  [arg-type]test.py:23: note: Following member(s) of "B" have conflicts:test.py:23: note:     __hash__: expected "Callable[[], int]", got "None"

This is using attrs 23.3.0, mypy 1.9.0.

@TincheTinche deleted the tin/fix-attrs-hash branchMarch 10, 2024 11:24
@Tinche
Copy link
ContributorAuthor

@brianmedigate I tested the other direction :/

I'll look into handling this case in a new PR.

brianmedigate reacted with thumbs up emoji

Hnasar pushed a commit to Hnasar/mypy that referenced this pull requestMar 11, 2024
This commit fixes a couple regressions in 1.9.0 from91be285.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-L1689Mypy 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`.Fixespython#17015Fixespython#16556 (comment)Based on a patch inpython#17012Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>
Hnasar added a commit to Hnasar/mypy that referenced this pull requestMar 11, 2024
This commit fixes a couple regressions in 1.9.0 from91be285.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-L1689Mypy 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`.Fixespython#17015Fixespython#16556 (comment)Based on a patch inpython#17012Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>
Hnasar added a commit to Hnasar/mypy that referenced this pull requestMar 11, 2024
This commit fixes a couple regressions in 1.9.0 from91be285.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-L1689Mypy 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`.Fixespython#17015Fixespython#16556 (comment)Based on a patch inpython#17012Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>
Hnasar added a commit to Hnasar/mypy that referenced this pull requestMar 11, 2024
This commit fixes a couple regressions in 1.9.0 from91be285.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-L1689Mypy 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`.Fixespython#17015Fixespython#16556 (comment)Based on a patch inpython#17012Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>
Hnasar added a commit to Hnasar/mypy that referenced this pull requestMar 11, 2024
This commit fixes a couple regressions in 1.9.0 from91be285.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-L1689Mypy 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`.Fixespython#17015Fixespython#16556 (comment)Based on a patch inpython#17012Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>
Hnasar added a commit to Hnasar/mypy that referenced this pull requestMar 12, 2024
This commit fixes a couple regressions in 1.9.0 from91be285.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-L1689Mypy 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`.Fixespython#17015Fixespython#16556 (comment)Based on a patch inpython#17012Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>
JukkaL pushed a commit that referenced this pull requestMar 15, 2024
This commit fixes a couple regressions in 1.9.0 from91be285.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-L1689Mypy 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#17015Fixes#16556 (comment)Based on a patch in#17012Co-Authored-By: Tin Tvrtkovic <tinchester@gmail.com>Co-authored-by: Hashem Nasarat <hashem@hudson-trading.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@sobolevnsobolevnsobolevn approved these changes

+1 more reviewer

@ikonstikonstikonst approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

False negative: attrs classes and hashability

4 participants

@Tinche@brianmedigate@ikonst@sobolevn

[8]ページ先頭

©2009-2025 Movatter.jp