Skip to main content

Store references of your typing arguments to be available at runtime.

Project description

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 and 3.13. 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).

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

typing_arguments-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

typing_arguments-0.1.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file typing_arguments-0.1.1.tar.gz.

File metadata

  • Download URL: typing_arguments-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for typing_arguments-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4a195797e71d32a51d5f198ff4443dd3c254fef3243c50dbcbf889dcff4f3536
MD5 7f9e562f03954856e0d881d70782032b
BLAKE2b-256 557dbfac19a1a8c18ee5045ef6c1c226e3e896b1ceef580cc383f32b5fa44fa5

See more details on using hashes here.

Provenance

The following attestation bundles were made for typing_arguments-0.1.1.tar.gz:

Publisher: release.yml on team23/typing-arguments

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file typing_arguments-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typing_arguments-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e9a68d12b691d68eb348d9c028ea80cee855a7559c318b0a0b5a88cf7a1a768d
MD5 0d53f98b20657334ff39d8f7ca81e4b2
BLAKE2b-256 7fd00cede70f657597fcbfed44f55a3c602cfc520ed4e321876e34121a667bdf

See more details on using hashes here.

Provenance

The following attestation bundles were made for typing_arguments-0.1.1-py3-none-any.whl:

Publisher: release.yml on team23/typing-arguments

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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