Skip to main content

Reuse type arguments explicitly passed at runtime to a generic class before instantiating.

Project description

runtime_generics Package version Supported Python versions

Tests Coverage Ruff Code style License Pre-commit

Highly into type-safe Python code?

runtime_generics is a niche Python library that allows you to reuse type arguments explicitly passed at runtime to generic classes before instantiation. The library does two things:

  • makes it possible to retrieve the type arguments passed to the generic class at runtime before the class was instantiated;
  • supports basic instance/subclass checking of runtime generic classes.
  • given a parametrized generic class (generic alias), it makes every class method use generic alias cls instead of the origin class.

Simple example

3.12+ (PEP 695 syntax):

from runtime_generics import get_type_arguments, runtime_generic

@runtime_generic
class MyGeneric[T]:
    type_argument: type[T]

    def __init__(self) -> None:
        (self.type_argument,) = get_type_arguments(self)

    @classmethod
    def whoami(cls) -> None:
        print(f"I am {cls}")

my_generic = MyGeneric[int]()
assert my_generic.type_argument is int
my_generic.whoami()  # I am MyGeneric[int]

3.8+:

from __future__ import annotations

from typing import Generic, TypeVar
from runtime_generics import get_type_arguments, runtime_generic

T = TypeVar("T")

@runtime_generic
class MyGeneric(Generic[T]):
    type_argument: type[T]

    def __init__(self) -> None:
        (self.type_argument,) = get_type_arguments(self)

    @classmethod
    def whoami(cls) -> None:
        print(f"I am {cls}")

my_generic = MyGeneric[int]()
assert my_generic.type_argument is int
my_generic.whoami()  # I am MyGeneric[int]

Installation

If you want to…

…use this tool in your project 💻

You might simply install it with pip:

pip install runtime-generics

If you use Poetry, then run:

poetry add runtime-generics

…contribute to runtime_generics 🚀

[!Note] If you use Windows, it is highly recommended to complete the installation in the way presented below through WSL2.

  1. Fork the runtime_generics repository on GitHub.

  2. Install Poetry.
    Poetry is an amazing tool for managing dependencies & virtual environments, building packages and publishing them.

    pipx install poetry
    

    If you encounter any problems, refer to the official documentation for the most up-to-date installation instructions.

    Be sure to have Python 3.8 installed—if you use pyenv, simply run:

    pyenv install 3.8
    
  3. Clone your fork locally and install dependencies.

    git clone https://github.com/your-username/runtime_generics path/to/runtime_generics
    cd path/to/runtime_generics
    poetry env use $(cat .python-version)
    poetry install
    poetry shell
    pre-commit install --hook-type pre-commit --hook-type pre-push
    

For more information on how to contribute, check out CONTRIBUTING.md.
Always happy to accept contributions! ❤️

Legal info

© Copyright by Bartosz Sławecki (@bswck).
This software is licensed under the terms of MIT License.

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

runtime_generics-2.0.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

runtime_generics-2.0.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file runtime_generics-2.0.2.tar.gz.

File metadata

  • Download URL: runtime_generics-2.0.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for runtime_generics-2.0.2.tar.gz
Algorithm Hash digest
SHA256 c7353948ccd1b098af26a22d22e19eb2685e64351e278ff31c0a2520dd886afd
MD5 5914b4d12efeb19cdf62d96a3e235411
BLAKE2b-256 9e16efa7bd0e918f62c07faca74973847ac14edd2c4486afb50d57946b557349

See more details on using hashes here.

File details

Details for the file runtime_generics-2.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for runtime_generics-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3c75d27945883f74c0060eb037209c370d4c8c63e3285095e08e0d3d831b1f98
MD5 62e3655272278f02b24b26cee5c9fef5
BLAKE2b-256 973f151432abed3a44760c123bb67e9183bccce7e7c241dc369a22cae520dff7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page