Skip to content

Clarify the meaning dataclasses.field(..., hash=False) #130130

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

Closed
picnixz opened this issue Feb 14, 2025 · 3 comments
Closed

Clarify the meaning dataclasses.field(..., hash=False) #130130

picnixz opened this issue Feb 14, 2025 · 3 comments
Labels
docs Documentation in the Doc dir easy topic-dataclasses

Comments

@picnixz
Copy link
Member

picnixz commented Feb 14, 2025

See https://discuss.python.org/t/unclear-docs-for-dataclasses-field/80716. Currently the docs are:

hash: This can be a bool or None. If true, this field is included in the generated __hash__() method. If None (the default), use the value of compare: this would normally be the expected behavior. A field should be considered in the hash if it’s used for comparisons. Setting this value to anything other than None is discouraged.

We can change it to

 hash: This can be a bool or None. If true, this field is included in the generated :meth:`~object.__hash__` method.
+If false, this field is excluded from the generated :meth:`~object.__hash__` method.
 If :const:`None` (the default), use the value of compare: this would normally be the expected behavior.

Linked PRs

@ericvsmith
Copy link
Member

I also think it should be changed from:

If None (the default), use the value of compare: this would normally be the expected behavior. A field should be considered in the hash if it’s used for comparisons.

to

If None (the default), use the value of compare: this would normally be the expected behavior, since a field should be included in the hash if it’s used for comparisons.

@Lonwwolf14
Copy link

Hi @picnixz,

I’ve reviewed issue #130130 regarding the unclear documentation for the hash parameter in dataclasses.field(). From what I understand, the current documentation states that hash can be a bool or None, and if set to True, the field is included in the generated __hash__() method. If set to None (default), it follows the compare value, which is the expected behavior for consistency between comparisons and hashing. However, the documentation lacks clarity on what happens when hash=False, which can confuse users.

I’m planning to update the dataclasses.rst file in the documentation to incorporate this clarification. Please let me know if my understanding is correct or if there are any other aspects I should consider before proceeding.

@ericvsmith
Copy link
Member

Merged. Thanks!

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 20, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 20, 2025
sobolevn pushed a commit that referenced this issue Feb 20, 2025
…H-130324) (#130337)

gh-130130: Clarify `hash=False` docs in `dataclasses.field` (GH-130324)
(cherry picked from commit 47ace53)

Co-authored-by: Sabfo <[email protected]>
sobolevn pushed a commit that referenced this issue Feb 20, 2025
…H-130324) (#130336)

gh-130130: Clarify `hash=False` docs in `dataclasses.field` (GH-130324)
(cherry picked from commit 47ace53)

Co-authored-by: Sabfo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy topic-dataclasses
Projects
Status: Todo
Development

No branches or pull requests

3 participants