Reuse generic class type arguments at runtime.
Project description
runtime_generics
![Package version](https://pypi-camo.freetls.fastly.net/cb7caaf015313d674fc2f7bfef4c3b9275f06f04/68747470733a2f2f696d672e736869656c64732e696f2f707970692f762f72756e74696d652d67656e65726963733f6c6162656c3d50795049)
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 fromDict[str, T]
, find thatDict[str, int]
is a parent forFoo[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
[!Note] If you use Windows, it is highly recommended to complete the installation in the way presented below through WSL2.
-
Fork the runtime_generics repository on GitHub.
-
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
-
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
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
Built Distribution
Hashes for runtime_generics-3.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aefc6ea723750332b4f12d241b4849e9f399e4ada4a1db63e6d4a6240e29ec25 |
|
MD5 | e6664f945106e86de67016436fb5ecdd |
|
BLAKE2b-256 | 6e6b88695057f8f66be295de08b38f1f964cd1a2639d3b76e9159502c507d4f8 |