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: mapTypeVarnames to concrete runtime types for a generic class/alias.get_generic_mapping(obj_or_type): build aGenericTypeMapfrom a class, alias, or object instance.resolve_typevars(tp, mapping_source, strict=False): recursively resolveTypeVars 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
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
typetoolbox-0.3.0.tar.gz
(58.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typetoolbox-0.3.0.tar.gz.
File metadata
- Download URL: typetoolbox-0.3.0.tar.gz
- Upload date:
- Size: 58.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67936c31b9f6437314444e26585d137fd44059afe4bea25621ddca4b362fa06b
|
|
| MD5 |
4e2f613b600e72cff30b40649f402d2d
|
|
| BLAKE2b-256 |
0b3318a0194130da3ebc4ff1b3705c1612299c7b530045055e234f65f45ab8dc
|
File details
Details for the file typetoolbox-0.3.0-py3-none-any.whl.
File metadata
- Download URL: typetoolbox-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b067fa45e26e2bcbdaf0feac133caef665246836a4ac2b80f2e722d4465edfe6
|
|
| MD5 |
1b68099226de1746788aa59d08ded20d
|
|
| BLAKE2b-256 |
ce298e4b3c54abd6aff0b114f3cb259745eb1036c1c72c8005a581fa5e032b9b
|