Skip to content

fields-metadata

A Python library for extracting comprehensive field metadata from dataclasses and Pydantic models, with support for derived fields, custom annotations, and recursive traversal of nested structures.

Features

  • Field types and paths: dot-separated paths for nested fields
  • Type detection: optional, multivalued, composite, numeric, and categorical field detection
  • Computed fields: properties and Pydantic computed fields
  • Custom annotations: extensible annotation system with built-in annotations
  • Filtering, sorting, grouping: query and partition fields using therismos expressions
  • Hook system: synthetic fields, custom extraction, and lifecycle callbacks
  • Recursive traversal: automatic extraction of nested structure metadata
  • Type-safe generics: generic support for custom metadata types

Quick Start

from dataclasses import dataclass
from typing import Annotated
from annotated_types import DocInfo, Ge
from fields_metadata import MetadataExtractor, HumanReadableId

@dataclass
class Person:
    name: Annotated[str, HumanReadableId(), DocInfo("Full name")]
    age: Annotated[int, Ge(0), DocInfo("Age in years")]
    email: str | None = None

extractor = MetadataExtractor()
metadata = extractor.extract(Person)

print(metadata["name"].doc)            # "Full name"
print(metadata["age"].numeric)         # True
print(metadata["email"].optional)      # True

See the User Guide for more examples.