Improve documentation of safety constraints on hashable objects (#505)

* Clarify documentation for hashing

Makes clear that hashable objects need to be deeply immutable, in
practice even if not in theory.

Closes #503 .

* Use simple past tense in docs
This commit is contained in:
Ryan Gabbard 2019-02-14 12:06:19 -05:00 committed by Hynek Schlawack
parent 7b37354aa4
commit 659aa48f53
3 changed files with 8 additions and 5 deletions

View File

@ -0,0 +1 @@
Clarified documentation for hashing to warn that hashable objects should be deeply immutable (in their usage, even if this is not enforced).

View File

@ -66,7 +66,9 @@ For a more thorough explanation of this topic, please refer to this blog post: `
Hashing and Mutability
----------------------
Changing any field involved in hash code computation after the first call to `__hash__` (typically this would be after its insertion into a hash-based collection) can result in silent bugs.
Therefore, it is strongly recommended that hashable classes be ``frozen``.
Therefore, it is strongly recommended that hashable classes be ``frozen.``
Beware, however, that this is not a complete guarantee of safety:
if a field points to an object and that object is mutated, the hash code may change, but ``frozen`` will not protect you.
Hash Code Caching

View File

@ -843,10 +843,10 @@ def attrs(
:param bool cache_hash: Ensure that the object's hash code is computed
only once and stored on the object. If this is set to ``True``,
hashing must be either explicitly or implicitly enabled for this
class. If the hash code is cached, then no attributes of this
class which participate in hash code computation may be mutated
after object creation.
class. If the hash code is cached, avoid any reassignments of
fields involved in hash code computation or mutations of the objects
those fields point to after object creation. If such changes occur,
the behavior of the object's hash code is undefined.
.. versionadded:: 16.0.0 *slots*
.. versionadded:: 16.1.0 *frozen*