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.

from dataclasses import dataclass
from typespecs import ITSELF, Spec, from_annotated
from typing import Annotated as Ann, TypeVar


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


weather = Weather([273.15, 280.15], [5.0, 10.0], "Tokyo")
specs = 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, Spec(dtype=ITSELF)]


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


weather = Weather([273.15, 280.15], [5.0, 10.0], "Tokyo")
specs = 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 = 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 = 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-3.0.0.tar.gz (88.4 kB view details)

Uploaded Source

Built Distribution

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

typespecs-3.0.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typespecs-3.0.0.tar.gz
  • Upload date:
  • Size: 88.4 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-3.0.0.tar.gz
Algorithm Hash digest
SHA256 00f68752834d6d5cdbe6e302cf58e7bf2d39576de963818f33ee54fa84c00d42
MD5 a68a331f058000aa9fdd4a32689a7b03
BLAKE2b-256 e3c70a586f5a9084b422108d889716923a0b04a8e4667a4cabf44d62bb011075

See more details on using hashes here.

File details

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

File metadata

  • Download URL: typespecs-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 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-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8dfc388a4aeaf30220ce6d1fb246f11a1045bf1914fe076398cd262a7bd75192
MD5 dac335b097b70d7aaa11d87ca9acba05
BLAKE2b-256 dd44bfe71013ac88b2a8cf6a3c4079889201700d356c3fdd16037c771c5d1939

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