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

False negative: attrs classes and hashability #16550

Closed
Tinche opened this issue Nov 24, 2023 · 0 comments · Fixed by #16556
Closed

False negative: attrs classes and hashability #16550

Tinche opened this issue Nov 24, 2023 · 0 comments · Fixed by #16556
Labels
bug mypy got something wrong topic-attrs

Comments

@Tinche
Copy link
Contributor

Tinche commented Nov 24, 2023

Looks like non-frozen attrs classes are considered hashable by default. But they aren't.

The following snippet type-checks but explodes at runtime:

from collections.abc import Hashable

from attrs import define


@define
class A:
    a: int


def my_func(c: Hashable) -> None:
    {c}


my_func(A(1))

(The behavior can be customized.)

This might be an easy one to fix. I can grab it when I have time, or it may be an easy first issue for someone?

@Tinche Tinche added the bug mypy got something wrong label Nov 24, 2023
sobolevn pushed a commit that referenced this issue Dec 12, 2023
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=True` or `unsafe_hash=True` (new
parameter, added to stubs) to `define()`.

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.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

2 participants