Skip to main content

Dynamic typing utilities for Swarmauri annotated unions, intersection metadata, and registry-driven component models.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_typing Discord

Swarmauri Typing

swarmauri_typing provides typing utilities for Swarmauri's dynamic component model. It exposes annotated intersection and union helpers used by swarmauri_base to build Pydantic-compatible component unions from runtime registries.

Why Swarmauri Typing?

swarmauri_typing isolates the small dynamic typing helpers used by Swarmauri's component model. It lets foundational packages generate metadata-rich Annotated unions without coupling those helpers to a specific component family.

FAQ

Q: What does UnionFactory do?

A: UnionFactory builds an Annotated[Union[...], ...] type from a caller-provided lookup function. swarmauri_base uses this pattern for dynamic component subtype unions.

Q: What does Intersection do?

A: Intersection computes common classes from input types and returns an annotated type that carries IntersectionMetadata.

Q: Do most application developers import this package directly?

A: Usually no. Most users get this behavior through swarmauri_base.DynamicBase and SubclassUnion.

Features

  • Intersection creates an annotated union of common classes from multiple input types.
  • IntersectionMetadata preserves the source classes used to build the intersection annotation.
  • UnionFactory builds annotated union types from a user-supplied type lookup function.
  • UnionFactoryMetadata records the source model or key used to build a dynamic union.
  • Annotation extenders allow callers to attach additional metadata such as Pydantic discriminators.
  • Python 3.10, 3.11, 3.12, 3.13, and 3.14 support.

Installation

Install with uv:

uv add swarmauri_typing

Install with pip:

pip install swarmauri_typing

Usage

Create an intersection annotation:

from typing import get_args

from swarmauri_typing import Intersection, IntersectionMetadata


class Root:
    pass


class Left(Root):
    pass


class Right(Root):
    pass


Common = Intersection[Left, Right]
metadata = [item for item in get_args(Common)[1:] if isinstance(item, IntersectionMetadata)]

assert metadata[0].classes == (Left, Right)

Create a dynamic annotated union:

from typing import Any

from swarmauri_typing import UnionFactory, UnionFactoryMetadata


class JsonStore:
    pass


class SqlStore:
    pass


def store_types(model_name: str) -> list[type]:
    if model_name == "Store":
        return [JsonStore, SqlStore]
    return []


StoreUnion = UnionFactory(store_types, name="store_union")["Store"]

assert any(
    isinstance(item, UnionFactoryMetadata) and item.name == "store_union"
    for item in getattr(StoreUnion, "__metadata__", ())
)

Swarmauri Component Usage

swarmauri_base.DynamicBase uses UnionFactory to turn registered component subtypes into discriminated unions. This lets models typed with SubclassUnion[BaseComponent] hydrate concrete subclasses from serialized payloads that include a type field.

from pydantic import Field

from swarmauri_typing import UnionFactory


def component_types(model_name: str) -> list[type]:
    return []


SubclassUnion = UnionFactory(
    component_types,
    name="subclass_union",
    annotation_extenders=[Field(discriminator="type")],
)

Related Packages

Foundational packages:

  • swarmauri_base uses these typing helpers for dynamic component deserialization.
  • swarmauri_core provides the interface contracts for Swarmauri component families.
  • swarmauri provides the namespace importer and plugin discovery layer.
  • swarmauri_standard provides first-party components that depend on the core/base typing model.

Component-kind packages that benefit from dynamic typing:

When To Use This Package

Use swarmauri_typing directly when you need dynamic Annotated type construction in a Swarmauri-compatible package. Most application developers interact with it indirectly through swarmauri_base.ComponentBase, DynamicBase, and SubclassUnion.

License

Apache-2.0

Contributing

When changing these helpers, keep the API small and compatible with Pydantic metadata usage in swarmauri_base, add focused tests for generated annotations, and follow the Swarmauri SDK contribution guide.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

swarmauri_typing-0.11.0.dev1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

swarmauri_typing-0.11.0.dev1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file swarmauri_typing-0.11.0.dev1.tar.gz.

File metadata

  • Download URL: swarmauri_typing-0.11.0.dev1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_typing-0.11.0.dev1.tar.gz
Algorithm Hash digest
SHA256 4ccc69a5ed9f5f35f8fb0da8e1f5e351614ad9652e7fc8630cdc6bcfccb1e89d
MD5 5d850f14b1896353e62c123efe6df71d
BLAKE2b-256 72ba6e0b6c357ec3d0e2e36c5681495fa333c5a88256a1ca413bf5647c06fc52

See more details on using hashes here.

File details

Details for the file swarmauri_typing-0.11.0.dev1-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_typing-0.11.0.dev1-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_typing-0.11.0.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 0238746c46efb0700e253f3f01f8557b2dd2bed2477eb4f84c0278f1a10b3ef2
MD5 92686c0717d0dfd6f2f10d2eb1e5c446
BLAKE2b-256 fad5d4377fba61762466c65a9911035dc0298c5edf2af305658b5d196d31db0b

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