Skip to main content

typed-registry

A tiny, zero-dependency, fully typed service registry for Python — ~90 lines of code, no casts.

event_bus = registry.get(Services.event_bus)  # type checker knows: EventBus
mailer = registry.get(Services.mailer)        # type checker knows: Mailer | None

Why

Most service locators key services by string or class and hand back Any, forcing cast(...) at every call site. typed-registry keys services by typed tokens instead. Each token carries the type and optionality of the service it names:

  • registry.get(token) returns T for ServiceToken[T] and T | None for OptionalToken[T], inferred by MyPy, ty, and Zuban.
  • token.register(registry, value) rejects values of the wrong type at type-check time and raises DuplicateServiceError on accidental double-registration.
  • Two tokens with the same name never collide, because tokens compare by identity.
  • registry.freeze() locks the registry after composition so runtime code cannot mutate it.

Install

Requires Python 3.14+.

uv add typed-registry

Quick start

Declare a catalogue of tokens owned by your application. Token names are derived automatically from the class and attribute name via __set_name__ (or pass an explicit string):

from typing import Final

from typed_registry import OptionalToken, ServiceToken


class Services:
    event_bus: Final = ServiceToken[EventBus]()
    settings: Final = ServiceToken[Settings]()
    mailer: Final = OptionalToken[Mailer]()

Wire concrete services in your composition root and freeze the registry when wiring finishes:

from typed_registry import ServiceRegistry

registry = ServiceRegistry()
Services.settings.register(registry, Settings.from_env())
Services.event_bus.register(registry, EventBus())
registry.freeze()

Resolve them where you assemble the rest of the application:

event_bus = registry.get(Services.event_bus)  # EventBus
settings = registry.get(Services.settings)    # Settings
mailer = registry.get(Services.mailer)        # Mailer | None

Type mismatches fail before your code runs:

Services.event_bus.register(registry, Settings())
# error: Argument 2 to "register" of "_Token" has incompatible type "Settings"; expected "EventBus"

Requesting an unregistered ServiceToken raises MissingServiceError (a LookupError subclass) exposing service_name. Registering the same token twice raises DuplicateServiceError (a ValueError subclass).

from typed_registry import MissingServiceError

try:
    registry.get(Services.event_bus)
except MissingServiceError as error:
    print(error.service_name)  # "Services.event_bus"

Testing

Use copy() to clone a frozen production-wired registry into a mutable copy, then override() or unregister() individual tokens — still type-checked against the token:

registry = build_registry().copy()
Services.event_bus.override(registry, FakeEventBus())

Design notes

typed-registry belongs in the composition root — the single place where an application builds its object graph. Keep the registry there and pass narrow dependencies into domain code as plain arguments.

Deliberate non-features:

  • No autowiring or reflection. Construct services yourself, in order.
  • No scopes or lifecycles. Create one registry per scope if needed.
  • No global registry. Instantiate and pass ServiceRegistry explicitly.

API

Name Description
ServiceToken[T](name="") Required typed, identity-compared key (name defaults to Owner.attr via __set_name__).
OptionalToken[T](name="") Optional typed, identity-compared key (registry.get returns T | None).
token.register(registry, service) Store service: T; raises DuplicateServiceError if already registered or FrozenRegistryError if frozen.
token.override(registry, service) Insert or replace service: T; raises FrozenRegistryError if frozen.
token.unregister(registry) Remove the token if present; raises FrozenRegistryError if frozen.
ServiceRegistry.get(token) Return T for ServiceToken[T] (raising MissingServiceError if absent) or T | None for OptionalToken[T].
ServiceRegistry.get_optional(token) Return T | None for either token kind without raising.
token in registry Check whether a token is registered.
ServiceRegistry.freeze() / .frozen Lock the registry against further mutations.
ServiceRegistry.copy() Return an unfrozen shallow copy of the registry.
MissingServiceError LookupError subclass with service_name.
DuplicateServiceError ValueError subclass with service_name.
FrozenRegistryError RuntimeError raised when mutating a frozen registry.

Development

See docs/development.md for local setup, checks, and release procedures.

License

MIT. See LICENSE.

Metadata

Release files for typed-registry 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typed-registry 0.2.0
File Size Uploaded Provenance Actions
typed_registry-0.2.0.tar.gz 4.6 kB With provenance Details for typed_registry-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for typed-registry 0.2.0
File Interpreter ABI Platform Actions
typed_registry-0.2.0-py3-none-any.whl Python 3 none any Details for typed_registry-0.2.0-py3-none-any.whl

Total release size: 10.3 kB

Release files / typed_registry-0.2.0.tar.gz

Download URL typed_registry-0.2.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5ff5163371e9130b44789e43818c28318c2da43ee69230fbcf6c652d801c6597
BLAKE2b-256 checksum
How to use checksums
3305d3c4a659cbfb58db8698894c9987fbffa626fcf6d1edc8f1790e42fe9051
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Release files / typed_registry-0.2.0-py3-none-any.whl

Download URL typed_registry-0.2.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4396ac7a72e38403404a5757f64e0cdd0d012588319ce96302c2cf88c56b3fb7
BLAKE2b-256 checksum
How to use checksums
be16d8286ebd2c59911a33475be5073aee235da840019fbcdc81d690119cb313
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Security for typed-registry 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for typed_registry-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page