Skip to main content

Reuse generic class type arguments at runtime.

Project description

runtime_generics skeleton Supported Python versions Package version

Tests Coverage Documentation Status

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 three things:

  • makes it possible to retrieve the type arguments passed to the generic class at runtime before the class was instantiated;
  • given a parametrized generic class (generic alias), it makes every class method use generic alias cls instead of the origin class;
  • offers facilities to find how parent classes are parametrized, so e.g. if Foo[T] inherits from Dict[str, T], find that Dict[str, int] is a parent for Foo[int].

A 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

You might simply install it with pip:

pip install runtime-generics

If you use Poetry, then you might want to run:

poetry add runtime-generics

For contributors

Poetry Ruff Pre-commit

[!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. You might use pipx to install it globally (recommended):

    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
    

    Next up, simply activate the virtual environment and install pre-commit hooks:

    poetry shell
    pre-commit install
    

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-3.0.5.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

runtime_generics-3.0.5-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for runtime_generics-3.0.5.tar.gz
Algorithm Hash digest
SHA256 ac89dc93a9a231e17f7f9c2ee1e53f9ab11f1edf9ccbfbff0cac2eca0b6b2868
MD5 5d92939e4a21b394476f5f3b9c939427
BLAKE2b-256 23fe52a6e12d5712cb6380abe1ef9c1ef1bda2d0f1a77b2a586ece70a7665aa7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for runtime_generics-3.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 82f29473be3c799d11881a306d4a4d04054bcb383881d66a42a0bfb3caa987b9
MD5 a5a31604329b6b33994085925d251a96
BLAKE2b-256 08817660d05db1ec99320b53ea699001b469cf9722e047bc0bb59db9ada30396

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