Skip to main content

Runtime helpers for inspecting and resolving Python generic types.

Project description

typetoolbox

typetoolbox provides runtime utilities for inspecting Python generics and resolving TypeVar bindings.

Installation

uv add typetoolbox

Core APIs

  • GenericTypeMap: map TypeVar names to concrete runtime types for a generic class/alias.
  • get_generic_mapping(obj_or_type): build a GenericTypeMap from a class, alias, or object instance.
  • resolve_typevars(tp, mapping_source, strict=False): recursively resolve TypeVars in nested type expressions.
  • find_specializations(base_generic, where=None): find subclasses that fully close a base generic.
  • inspect_specializations(base_generic, include_generic=False, where=None): inspect subclasses with rich mapping metadata.
  • find_specializations_by_args(base_generic, args): return exact-arg specializations.
  • build_specialization_index(base_generic): map resolved arg tuples to implementations.
  • try_to_map_generic_args_to_specialization(generic_type, specialization_type): map generic args onto a concrete specialization.

Example

from typing import Generic, TypeVar

from typetoolbox import GenericTypeMap, find_specializations, resolve_typevars

X = TypeVar("X")
Y = TypeVar("Y")


class Pair(Generic[X, Y]):
    pass


class IntStrPair(Pair[int, str]):
    pass


mapping = GenericTypeMap(IntStrPair)
assert resolve_typevars(dict[X, list[Y]], mapping) == dict[int, list[str]]
assert find_specializations(Pair) == [IntStrPair]

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

typetoolbox-0.1.0.tar.gz (42.9 kB view details)

Uploaded Source

Built Distribution

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

typetoolbox-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file typetoolbox-0.1.0.tar.gz.

File metadata

  • Download URL: typetoolbox-0.1.0.tar.gz
  • Upload date:
  • Size: 42.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","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 typetoolbox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0e3c9c7c1eaff825dac874de0947c500b39fffc8bf14ad43fbb9914c59a952e3
MD5 630d6b2292f831577bb3190dbf43257f
BLAKE2b-256 d102e56d678011b2b3c535fc8669011e6c0dc6756e5082454c3e6bd1496c3ff8

See more details on using hashes here.

File details

Details for the file typetoolbox-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: typetoolbox-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","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 typetoolbox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66d765040447f598f4afd20b5a36814f9e14682441e74b767bf57cdc944cba7a
MD5 57f291ceb547f61ef41a12907727de25
BLAKE2b-256 2d33c99762a736340eafd13c3ebb13b10a021261156765b437c69446f8849edb

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