Skip to content

Improve hash() builtin docstring with caveats.#125229

Open
gpshead wants to merge 3 commits intopython:mainfrom
gpshead:docs/builtins/hash
Open

Improve hash() builtin docstring with caveats.#125229
gpshead wants to merge 3 commits intopython:mainfrom
gpshead:docs/builtins/hash

Conversation

@gpshead
Copy link
Copy Markdown
Member

@gpshead gpshead commented Oct 9, 2024

Mention its return type and that the value can be expected to change between processes (hash randomization).

Why? The hash builtin gets reached for and used by a lot of people whether it is the right tool or not. IDEs surface docstrings and people use pydoc and help(hash).

There are more possible caveats we could go into here such as classes implementing their own dunder methods like __eq__ or __hash__ naturally being able to violate the constraint stated in this docstring. But that feels like too much for a beginner friendly docstring.

Mention its return type and that the value can be expected to change between
processes (hash randomization).

Why? The `hash` builtin gets reached for and used by a lot of people whether it
is the right tool or not. IDEs surface docstrings and people use pydoc and
`help(hash)`.

There are more possible caveats we could go into here such as classes
implementing their own dunder methods like `__eq__` or `__hash__` naturally
being able to violate the constraint stated in this docstring. But _that_ feels
like too much for a beginner friendly docstring.
@gpshead gpshead added docs Documentation in the Doc dir skip issue skip news needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Oct 9, 2024
Comment thread Python/bltinmodule.c Outdated
Comment thread Python/bltinmodule.c Outdated
Comment thread Python/bltinmodule.c Outdated
Comment thread Python/bltinmodule.c Outdated
Comment thread Python/bltinmodule.c Outdated
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Should we explicitly say that hash should not be used for hashing in crypto operations?

This is something I've noticed several times.

However, sometimes adding a note to not do something actually make people do this even more 🙈

Copy link
Copy Markdown
Contributor

@ncoghlan ncoghlan left a comment

Choose a reason for hiding this comment

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

General idea seems sound, wordsmithing is tricky :(

Comment thread Python/bltinmodule.c Outdated
Comment thread Python/bltinmodule.c Outdated
Comment thread Python/bltinmodule.c Outdated
@antiseebs
Copy link
Copy Markdown

I like the "within this process" language, and while I can see that it is also covered by something in another paragraph... I also think this is a thing that is empirically very easy for people to miss or get confused by, and I think the extra couple of words to make sure that someone who reads one paragraph and stops there because they think they know what the function does are probably a very good investment in reduced user pain.

Copy link
Copy Markdown
Contributor

@willingc willingc left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks @gpshead

@hugovk hugovk removed the needs backport to 3.12 only security fixes label Apr 10, 2025
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label May 8, 2025
@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Apr 19, 2026
Merges current upstream main into the branch (the PR had gone stale since
Oct 2024) and rewords the hash() builtin docstring to incorporate review
comments from @JelleZijlstra, @ncoghlan, @nedbat, and others:

- Drop "within this process" from the first sentence (Jelle, ncoghlan):
  the process caveat is still stated plainly in the body, so the first-line
  qualifier is redundant.
- Drop the "dict and set hash tables" sentence (Jelle, ncoghlan):
  implementation details of built-in containers don't belong in the user-
  facing docstring for hash().
- Add the "Not all objects are hashable; ... raises TypeError" note
  (Jelle's suggestion, phrasing informed by ncoghlan's rewrite).
- Keep the original "equal => same hash, but not the reverse" formulation,
  which is what the Python data model documents.
- Do not add a cryptographic warning (sobolevn raised the question but also
  flagged that such warnings can encourage misuse).

https://claude.ai/code/session_01UumhMLqHB8fqUuNWsC8RCe
@gpshead gpshead enabled auto-merge (squash) April 21, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge docs Documentation in the Doc dir needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip issue skip news stale Stale PR or inactive for long period of time.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

9 participants