h5t
h5t loads HDF5 groups into detached, typed Python records. Group attributes and
ordinary NumPy-array fields are materialized while the file is open. Typed datasets
keep snapshot metadata and can read their payload lazily without retaining an open
file descriptor.
Installation
pip install h5t # or: uv add h5t
Requires Python 3.11+. h5py, numpy, and Pydantic v2 are installed automatically.
Example
import json
from pathlib import Path
from typing import Annotated, Any
import numpy as np
import h5t
class Measurement(h5t.Dataset, extras="forbid"):
unit: str
class Nested(h5t.Group):
label: str
class Result(h5t.Group, extras="ignore"):
version: int # implicit HDF5 attribute
title: Annotated[str, h5t.Name("name")] # renamed attribute
config: Annotated[
dict[str, Any],
h5t.Attr(converter=json.loads),
]
array_attr: Annotated[np.ndarray, h5t.Attr()]
values: np.ndarray # eager dataset payload
measurement: Measurement # lazy detached dataset
eager_measurement: Annotated[Measurement, h5t.Eager()]
nested: Nested # recursively loaded group
note: str | None # absent becomes None
revision: int = 1 # absent uses a validated default
result = Result.from_file(Path("result.h5"), root="/")
result.attrs and result.measurement.attrs are immutable mappings keyed by their
on-disk HDF5 names. Declared attributes contain their Pydantic-processed values;
undeclared attributes retained under extras="ignore" contain the raw h5py values.
Typed datasets expose snapshot metadata and explicit data access:
dataset = result.measurement
dataset.path, dataset.shape, dataset.dtype, dataset.ndim
complete = dataset.data # first access reads and caches an ndarray
assert dataset.read() is complete
with dataset.open() as live:
first_hundred = live[:100] # fresh file view, useful for slices
Both from_file() and Dataset.open() close every handle on normal and exceptional
exits. Schema objects cannot be directly constructed, written, or serialized by h5t.
Field rules
| Annotation | HDF5 representation | Loading behavior |
|---|---|---|
Group subclass |
child group | recursively materialized |
Dataset or subclass |
child dataset | metadata/attrs snapshot, payload lazy |
Annotated[DatasetSubtype, Eager()] |
child dataset | complete payload cached during loading |
np.ndarray |
child dataset | complete payload loaded as an ndarray |
Annotated[T, Attr(...)] |
attribute | converter, then Pydantic validation |
scalar or Literal[...] |
attribute | Pydantic validation |
Name("stored-name") renames any field kind. Attr is valid only for attributes and
Eager only for typed datasets. A parameterized alias such as
numpy.typing.NDArray[np.floating] is accepted wherever np.ndarray is; the dtype
parameter is not validated. Unsupported collection-shaped child annotations raise
SchemaError; dynamic collections are not yet supported. Declarations are compiled at
the class statement, so a SchemaError surfaces there. A schema class that names a
class defined later in its module instead compiles on first use.
Each Group and Dataset subclass accepts extras="ignore" (the default) or
extras="forbid". A group policy applies to its immediate child and attribute
names. A typed dataset policy applies to its attributes. Nested schemas keep their own
policy, while a plain np.ndarray field never checks the dataset's attributes.
Snapshot consistency
A loaded model is a snapshot, with one deliberate exception:
- Group and dataset attributes, dataset shape/dtype/path, eager datasets, and plain
arrays reflect the file during
from_file(). - A lazy dataset's first
.data/.read()observes the file at that later moment and caches the resulting array permanently. .open()always opens the current file and current dataset. It may therefore observe replacements or fail if the source was changed or deleted.
CLI
h5t check result.h5 --schema mypackage.schemas:Result --root /results/latest
Exit status is 0 on success, 1 for the first file/schema mismatch, and 2 for import, declaration, usage, or I/O errors.
Development
uv sync
uv run pytest
uv run ty check
uv run ruff check .
Release files for h5t 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| h5t-0.2.0.tar.gz | 15.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| h5t-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.4 kB
Release files / h5t-0.2.0.tar.gz
| Download URL | h5t-0.2.0.tar.gz |
|---|---|
| Size | 15.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
38b637d4f4780c5802cd7cba97beded258f120d54c364f373eabba286c5a8bef
|
|
BLAKE2b-256 checksum How to use checksums |
6930d3c6814cc6e23056b6b931bc7950c0e5f50b1732eaeccf7176886c7e1c9c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency logRelease files / h5t-0.2.0-py3-none-any.whl
| Download URL | h5t-0.2.0-py3-none-any.whl |
|---|---|
| Size | 17.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a665f5e171f84a152e3294852c0bdc7e5688de145928c8fb13a3c031c97398eb
|
|
BLAKE2b-256 checksum How to use checksums |
af6fe5c5dea52490f99be6570a197b6f699116e6ab023699c74fede583857fe8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.
Transparency log