Stable hashing of Python data structures across separate processes and platforms.
Project description
stablehash
The stablehash module provides a "pure" hash function that is stable across Python processes and runs. This is in
contrast to the builtin hash() function, which may return a different value for the same input in separate
invokations even with the Python version.
We support most Python built-in types, including mutable types such as list and dict, as well as dataclasses. The
default internal hash algorithm is Blake2b, but this can be changed by passing a different hashlib algorithm to the
stablehash function.
Usage
from stablehash import stablehash
assert stablehash({"key": "value"}, algorithm="md5").hexdigest() == "0ea2506ffbeef2699760d422d7a8b971"
Compatibility notes
Since 0.3.x
Hashing semantics are changed for certain inputs to improve stability:
- Dictionaries now hash independent of key insertion order (i.e., equal dicts produce the same digest regardless of the order keys were added).
- Serialize floats as IEEE-754 64-bit little-endian (
struct.pack('<d', x)) to guarantee cross-platform stable digests.
These changes may cause the digest produced for the same value to differ from the 0.2.x series. The public API is unchanged, but we treat changes that alter produced hashes as breaking from a user's perspective. (See PR #20 for background.)
What should downstream users do?
-
If your project depends on exact hash outputs (for example, using them as file identifiers or data fingerprints), either:
- pin an upper bound to the previous minor series:
stablehash >=0.2.0, <0.3.0, or - upgrade to
0.3.xand re-generate your stored hashes.
- pin an upper bound to the previous minor series:
Versioning policy
This project follows Semantic Versioning where applicable. During initial development (0.y.z), the public API should not be considered stable and breaking changes may occur in a minor bump. We therefore use the minor version to signal changes that can alter produced digests (semantic changes). Downstream users are encouraged to specify an upper bound on the minor version when depending on 0.y.z releases.
API
stablehash(obj=..., *, algorithm="blake2b")
Returns a hashlib-compatible object with the given algorithm and the hash of the given object. The algorithm must be
one of the algorithms supported by hashlib.
stablehash.update(obj)
Updates the hash with the given object. If the object is not supported, a TypeError is raised.
stablehash.digest()
Returns the digest of the hash as a bytes object.
stablehash.hexdigest()
Returns the digest of the hash as a string object.
Supported types
The following types are supported:
Noneboolintfloatstrbytestuplelistsetfrozensetdict@dataclassobjectsdatetimeobjects (datetime,date,timeandtimedelta)uuid.UUID- Picklable objects (e.g. those that implement
__getstate__()) typeobjects (by their full qualified name)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stablehash-0.3.0.tar.gz.
File metadata
- Download URL: stablehash-0.3.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31d89697683c664487b6f995759aa14bc65bb9de31ad8beca5e99c8678b66baf
|
|
| MD5 |
df736a11abe954bd8130f127fc1d57b0
|
|
| BLAKE2b-256 |
5f01159196c21db96a7adde6d52e7691762ef873a708c6d56ea4e8bd99ba8392
|
Provenance
The following attestation bundles were made for stablehash-0.3.0.tar.gz:
Publisher:
python.yaml on NiklasRosenstein/python-stablehash
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stablehash-0.3.0.tar.gz -
Subject digest:
31d89697683c664487b6f995759aa14bc65bb9de31ad8beca5e99c8678b66baf - Sigstore transparency entry: 644593867
- Sigstore integration time:
-
Permalink:
NiklasRosenstein/python-stablehash@fd6b485d84bfb0cdd5f3171289dc551f0bb142e0 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/NiklasRosenstein
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python.yaml@fd6b485d84bfb0cdd5f3171289dc551f0bb142e0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file stablehash-0.3.0-py3-none-any.whl.
File metadata
- Download URL: stablehash-0.3.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82b59223e7c5c9b6df42dd4d3f1dbdc1356ecf80102405f5355dc287b28b77f6
|
|
| MD5 |
fee62ba0d9d16dad08d965542173efed
|
|
| BLAKE2b-256 |
7b7a4eb9fc77dececb5478f1d105b6b6c5a64706aa0715134b0bf50a1081406f
|
Provenance
The following attestation bundles were made for stablehash-0.3.0-py3-none-any.whl:
Publisher:
python.yaml on NiklasRosenstein/python-stablehash
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stablehash-0.3.0-py3-none-any.whl -
Subject digest:
82b59223e7c5c9b6df42dd4d3f1dbdc1356ecf80102405f5355dc287b28b77f6 - Sigstore transparency entry: 644593890
- Sigstore integration time:
-
Permalink:
NiklasRosenstein/python-stablehash@fd6b485d84bfb0cdd5f3171289dc551f0bb142e0 -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/NiklasRosenstein
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python.yaml@fd6b485d84bfb0cdd5f3171289dc551f0bb142e0 -
Trigger Event:
push
-
Statement type: