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

1.9 Regression: __hash__ method in attrs class is ignored #17015

Closed
Hnasar opened this issue Mar 11, 2024 · 1 comment · Fixed by #17016
Closed

1.9 Regression: __hash__ method in attrs class is ignored #17015

Hnasar opened this issue Mar 11, 2024 · 1 comment · Fixed by #17016
Labels
bug mypy got something wrong topic-attrs

Comments

@Hnasar
Copy link
Contributor

Hnasar commented Mar 11, 2024

Bug Report
mypy is ignoring a manually-written __hash__ method in an attrs class.
Bisect points to 91be285 causing the regression. cc @Tinche
Possibly reported already in a comment here? #16556 (comment)

To Reproduce

import attrs
from typing import reveal_type

@attrs.define()
class A():
    name: str = attrs.field()
    def __hash__(self) -> int:
        return hash(self.name)

reveal_type(A.__hash__)

Expected Behavior

$ py -m mypy --strict foo.py  # 1.8
foo.py:10: note: Revealed type is "def (self: foo.A) -> builtins.int"
$ py foo.py
Runtime type is 'function'

Actual Behavior

$ py -m mypy --strict foo.py  # 1.9
foo.py:10: note: Revealed type is "None"
$ py foo.py
Runtime type is 'function'

Your Environment

  • Mypy version used: 1.9.0
  • Mypy command-line flags: n/a
  • Python version used: 3.12
  • attrs version: 23.2.0
@Hnasar Hnasar added the bug mypy got something wrong label Mar 11, 2024
@Tinche
Copy link
Contributor

Tinche commented Mar 11, 2024

I'll take this.

Hnasar pushed a commit to Hnasar/mypy that referenced this issue 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:/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 added a commit to Hnasar/mypy that referenced this issue 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:/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 added a commit to Hnasar/mypy that referenced this issue 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:/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 added a commit to Hnasar/mypy that referenced this issue 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:/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 added a commit to Hnasar/mypy that referenced this issue 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:/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 added a commit to Hnasar/mypy that referenced this issue 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:/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]>
JukkaL pushed a commit that referenced this issue 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:/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 <[email protected]>
Co-authored-by: Hashem Nasarat <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-attrs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants