Skip to main content

Data specifications by type hints

Project description

Typespecs

Release Python Downloads DOI Tests

Data specifications by type hints

Overview

Typespecs is a lightweight Python library that leverages typing.Annotated to embed, extract, and manage metadata (such as units, categories, and descriptions) directly within your data structures. It keeps your code clean by binding specifications directly to your type hints. The extracted specifications are returned as a transparent subclass of pandas.DataFrame, making it instantly compatible with the rich PyData ecosystem.

Installation

pip install typespecs

Basic Usage

You can attach metadata to your class fields using Annotated and the typespecs.Spec object. The Spec object acts as a read-only dictionary, ensuring your metadata remains immutable and safe from runtime modifications. Once your data structure is defined, use typespecs.from_annotated to parse the instance and extract both the actual data and its associated metadata into a DataFrame object.

import typespecs as ts
from dataclasses import dataclass
from typing import Annotated as Ann, TypeVar


@dataclass
class Weather:
    temp: Ann[list[float], ts.Spec(category="data", name="Temperature", units="K")]
    wind: Ann[list[float], ts.Spec(category="data", name="Wind speed", units="m/s")]
    loc: Ann[str, ts.Spec(category="info", name="Observed location")]


weather = Weather([273.15, 280.15], [5.0, 10.0], "Tokyo")
specs = ts.from_annotated(weather)
print(specs)
      category              data               name           type units
temp      data  [273.15, 280.15]        Temperature    list[float]     K
wind      data       [5.0, 10.0]         Wind speed    list[float]   m/s
loc       info             Tokyo  Observed location  <class 'str'>  <NA>

Advanced Usage

Handling Sub-annotations

Typespecs simplifies working with nested types. You can easily create reusable type aliases with built-in specifications. Furthermore, by using the special typespecs.ITSELF object, the library dynamically captures the subtype (e.g., float in list[float]) as one of metadata.

T = TypeVar("T")
Dtype = Ann[T, ts.Spec(dtype=ts.ITSELF)]


@dataclass
class Weather:
    temp: Ann[list[Dtype[float]], ts.Spec(category="data", name="Temperature", units="K")]
    wind: Ann[list[Dtype[float]], ts.Spec(category="data", name="Wind speed", units="m/s")]
    loc: Ann[str, ts.Spec(category="info", name="Observed location")]


weather = Weather([273.15, 280.15], [5.0, 10.0], "Tokyo")
specs = ts.from_annotated(weather)
print(specs)
      category              data            dtype               name           type units
temp      data  [273.15, 280.15]  <class 'float'>        Temperature    list[float]     K
wind      data       [5.0, 10.0]  <class 'float'>         Wind speed    list[float]   m/s
loc       info             Tokyo             <NA>  Observed location  <class 'str'>  <NA>

Handling Missing Values

By default, missing metadata values are filled with pandas.NA. You can override this behavior and specify a custom fallback value by using the default parameter in from_annotated.

specs = ts.from_annotated(weather, default=None)
print(specs)
      category              data            dtype               name           type units
temp      data  [273.15, 280.15]  <class 'float'>        Temperature    list[float]     K
wind      data       [5.0, 10.0]  <class 'float'>         Wind speed    list[float]   m/s
loc       info             Tokyo             None  Observed location  <class 'str'>  None

Handling Full Specification

By default, typespecs neatly merges nested metadata (e.g., float in list[float]) into a single parent row. If you need to inspect the exact structural hierarchy of your annotations, set merge=False in from_annotated. This unpacks the tree, distinguishing between the parent collection and its elements.

specs = ts.from_annotated(weather, merge=False)
print(specs)
        category              data            dtype               name             type units
temp        data  [273.15, 280.15]             <NA>        Temperature      list[float]     K
temp/0      <NA>              <NA>  <class 'float'>               <NA>  <class 'float'>  <NA>
wind        data       [5.0, 10.0]             <NA>         Wind speed      list[float]   m/s
wind/0      <NA>              <NA>  <class 'float'>               <NA>  <class 'float'>  <NA>
loc         info             Tokyo             <NA>  Observed location    <class 'str'>  <NA>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typespecs-6.0.0.tar.gz (90.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typespecs-6.0.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file typespecs-6.0.0.tar.gz.

File metadata

  • Download URL: typespecs-6.0.0.tar.gz
  • Upload date:
  • Size: 90.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typespecs-6.0.0.tar.gz
Algorithm Hash digest
SHA256 2e482da6425a7447ac342d4044255ee880ca964c2ff777efc0e70c8a8f763b2e
MD5 850f5536207c7d6806c334d3c06fd220
BLAKE2b-256 2858ed6377f1d9aec78743b42eb053bb265f403711a8d0d8b29961c12fbccc73

See more details on using hashes here.

File details

Details for the file typespecs-6.0.0-py3-none-any.whl.

File metadata

  • Download URL: typespecs-6.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.30 {"installer":{"name":"uv","version":"0.9.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for typespecs-6.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52090ea0e0c25a4080d8e9b77a34201972708a2887d506a062bb15b09742d527
MD5 fbc007efc446abf9de80d6dadb2ebe98
BLAKE2b-256 a0deb67f41164fbd52f27e2530a2cda42074ae6784426654bc78e2361077e6c5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page