Auto Hash¶
auto_hash
¶
Auto-hash decorator for generating __hash__ on class instances.
Provides the auto_hash decorator that generates __hash__
based on class fields. Works on plain classes with __slots__ or
__annotations__.
Can be used as @auto_hash, @auto_hash(), or
@auto_hash(fields=[...]) to hash only specific attributes.
Does NOT generate __eq__ — combine with auto_eq when structural
equality is needed alongside hashing.
Useful for: Hashable data types and any type that requires
consistent hashing for sets or dictionary keys.
Example
@auto_hash
class Point2D:
__slots__ = ("x", "y")
def __init__(self, x: float, y: float) -> None:
self.x = x
self.y = y
p1 = Point2D(1.0, 2.0)
p2 = Point2D(1.0, 2.0)
assert hash(p1) == hash(p2)
With selective fields:
auto_hash(class_: type[T] | None = None, *, fields: Sequence[str] | None = None) -> type[T] | Callable[[type[T]], type[T]]
¶
Generate __hash__ for a class based on its fields.
Can be used as @auto_hash, @auto_hash(), or
@auto_hash(fields=[...]). Generates __hash__ only — does NOT
generate __eq__. Use auto_eq for structural equality
comparisons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_
|
type[T] | None
|
The target class (when used directly as |
None
|
fields
|
Sequence[str] | None
|
Optional sequence of field names to include in the hash. |
None
|
Returns:
| Type | Description |
|---|---|
type[T] | Callable[[type[T]], type[T]]
|
The decorated class if class_ is provided; otherwise a callable |
type[T] | Callable[[type[T]], type[T]]
|
that can be used as a decorator. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If no field names can be determined automatically and |