This release is a pre-release and may not be stable for production use.
tuplehash
Pure Python reimplementation of CPython's tuplehash function with exact overflow behavior, designed for hashing user-defined immutable sequences.
The Current Pain Point
Currently in Python, if you want your custom immutable sequence to hash like a tuple:
from typing import TypeVar, Sequence
T = TypeVar('T', covariant=True)
class MySequence(Sequence[T]):
def __hash__(self):
return hash(tuple(self))
This creates memory overhead for large collections.
Until such stdlib functionality exists, tuplehash provides:
# Explicitly provide a non-negative int to `len` if `len(iterable)` doesn't work
def tuplehash(iterable, length=None): ...
So you can do this:
from typing import TypeVar, Sequence
from tuplehash import tuplehash
T = TypeVar('T', covariant=True)
class MySequence(Sequence[T]):
def __hash__(self):
return tuplehash(self)
This gives you stdlib-quality hashing today.
Features
- Strict overflow behavior using custom fixed-width integer types
- Version-specific implementations matching:
- Python 2.7/3.0-3.7: Original multiplicative hash
- Python 3.8+: Simplified xxHash algorithm
Installation
pip install tuplehash
Usage
from tuplehash import tuplehash
# Basic usage
assert tuplehash((1, 2, 3)) == hash((1, 2, 3))
# Works with any collection
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
assert tuplehash(Point(3, 4)) == hash(Point(3, 4))
Implementation Details
Version-Specific Algorithms
| Python Version | Algorithm | Key Characteristics |
|---|---|---|
| <3.8 | Multiplicative hash | Initial value 0x345678, multiplier 1000003, length-dependent addend |
| ≥3.8 | Simplified xxHash | Single accumulator, 31/13-bit rotations, prime multiplications |
Overflow Handling
Uses custom Signed/Unsigned types to exactly replicate:
- 32-bit overflow on 32-bit platforms
- 64-bit overflow on 64-bit platforms
- All intermediate casting behaviors
Limitations
- Performance overhead vs native implementation
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Metadata
Release files for tuplehash 0.1.0a2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tuplehash-0.1.0a2.tar.gz | 4.2 kB | Without provenance | Details for tuplehash-0.1.0a2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tuplehash-0.1.0a2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for tuplehash-0.1.0a2-py2.py3-none-any.whl |
Total release size: 8.8 kB
Release files / tuplehash-0.1.0a2.tar.gz
| Download URL | tuplehash-0.1.0a2.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9132e840b8f1441294a0de75b963c2fdda7112d1618e49acbffff7d1427de7f
|
|
BLAKE2b-256 checksum How to use checksums |
63fa4095361002158a80aa9ff53ef88047b83dd0a35286886bca04a331aad2bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Release files / tuplehash-0.1.0a2-py2.py3-none-any.whl
| Download URL | tuplehash-0.1.0a2-py2.py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
964159b05c40ab052c7d187d0e1692ce71432cf1f8d6c282453abc02ab056a7f
|
|
BLAKE2b-256 checksum How to use checksums |
276b9d1224377c4b5eec298d52e3e998286af71159473ece58b610aaacac1594
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Security for tuplehash 0.1.0a2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.