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-5.0.0.tar.gz (90.2 kB view details)

Uploaded Source

Built Distribution

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

typespecs-5.0.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typespecs-5.0.0.tar.gz
  • Upload date:
  • Size: 90.2 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-5.0.0.tar.gz
Algorithm Hash digest
SHA256 f9e8ec7355391fe9e8febd9d18f33fb5b87cefe0a5ae5aa5d02c1bc919f4e93f
MD5 818c8ade23db93f964bf8474cc152314
BLAKE2b-256 6f0ccbdd9134ea4972af5bd965d6bece416dad8298b7a33f7f49b6b2509c27db

See more details on using hashes here.

File details

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

File metadata

  • Download URL: typespecs-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 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-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e9c00aebda0dfca16161a5ef0294c75978eb91b8b4dbc5563b8be465db6b114
MD5 0adf25ea0b7fbcb5195d01b8512c71b5
BLAKE2b-256 63876499a4b432991b5bad5673d3c51996455188c9ce326d5d18127d6d967c89

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