typing-arguments
Typing arguments using the Generic base class in python are great, but they lack the ability to
easily access the type arguments at runtime. This library provides a mixin class that can be used
to make type arguments available in the class and its instances.
This is also true for classes based on pydantic.BaseModel for pydantic > 2.x.
Installation
Just use pip install typing-arguments to install the library.
Note: typing-arguments is tested on Python 3.10, 3.11, 3.12, 3.13 and 3.14. This is also ensured running
all tests on all those versions using tox.
Usage
Quick Example
T1 = TypeVar("T1")
T2 = TypeVar("T2", bound="SomeBaseClass")
class Something(
GenericArgumentsMixin,
Generic[T1, T2],
):
t1 = typing_arg(T1)
t2 = typing_arg(T2)
ConcreteClass = Something[str, SomeBaseClassChild]
ConcreteClass.t1 # str
ConcreteClass.t2 # SomeBaseClassChild
Hint: You may also use this with pydantic models:
T1 = TypeVar("T1")
T2 = TypeVar("T2", bound="SomeBaseClass")
class SomethingModel(
GenericArgumentsMixin,
BaseModel,
Generic[T1, T2],
):
t1: ClassVar = typing_arg(T1)
t2: ClassVar = typing_arg(T2)
ConcreteClassModel = SomethingModel[str, SomeBaseClassChild]
ConcreteClassModel.t1 # str
ConcreteClassModel.t2 # SomeBaseClassChild
Internally GenericArgumentsMixin will create a new attribute __typing_arguments__
inside the class and its instances. This attribute is a dictionary mapping the type
variables to their concrete types. This is useful if you want to access the type
arguments in a generic way.
The typing_arg function is a helper function to make the type arguments available
in the class and its instances using a nicely named attribute. This is just a
convenience function, as you can also access the type arguments directly from the
__typing_arguments__ attribute.
Note: If you are using pydantic models you should use the ClassVar annotation
to ensure pydantic will not try to catch and validate the type arguments as normal
model fields.
You may also mix different generic base classes like so:
T1 = TypeVar("T1")
T2 = TypeVar("T2", bound="SomeBaseClass")
class Base1(
GenericArgumentsMixin,
Generic[T1],
):
pass
class Base2(
GenericArgumentsMixin,
Generic[T2],
):
t2 = typing_arg(T2)
class Something(
Base1[str],
Base2[SomeBaseClassChild],
):
t1 = typing_arg(T1)
Something.t1 # str
Something.t2 # SomeBaseClassChild
In this example Base1 and Base2 are both generic base classes. Base1 only
defines a type argument T1 and Base2 only defines a type argument T2. The
Something class inherits from both Base1 and Base2. Note that Base1 does
not define a simple accessor like t1 using typing_arg, while Base2 does. This
is not a problem and can be later added by Something using typing_arg as well.
You may encounter issues using the typing_arg function when using type validator
like mypy or your IDE. If so you might need to use cast to tell the type checker
you are sure about what you are doing. For example:
T1 = TypeVar("T1", bound="SomeBaseClass")
class Something(
GenericArgumentsMixin,
Generic[T1],
):
t1 = cast(type[SomeBaseClass], typing_arg(T1))
Note: You will still need to use ClassVar when using pydantic models. This
might result in using the same type twice (inside ClassVar and cast).
Metadata
Release files for typing-arguments 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| typing_arguments-0.3.1.tar.gz | 4.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| typing_arguments-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.6 kB
Release files / typing_arguments-0.3.1.tar.gz
| Download URL | typing_arguments-0.3.1.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
169dd6c96deabfca7b4833c643dfc506c3deb7d2f3984096b8d67012ed63f041
|
|
BLAKE2b-256 checksum How to use checksums |
e508a0f8eb90d00ea1cf1a1ea8c8635e5a678c474662be178a7a08b88b82b649
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","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}
|
Release files / typing_arguments-0.3.1-py3-none-any.whl
| Download URL | typing_arguments-0.3.1-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d69b637d164328f18e1e91ecfe0c9598d9bcaf2d621e4119a6c8c337de93b43b
|
|
BLAKE2b-256 checksum How to use checksums |
558b1c6b75ce725572332c5efe24d043a76744dba29197a441e8904c25e265fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.24 {"installer":{"name":"uv","version":"0.11.24","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}
|
Security for typing-arguments 0.3.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.