Reuse type arguments explicitly passed at runtime to a generic class before instantiating.
Project description
runtime_generics
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 argument 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.
Simple example
3.12+ (PEP 695 syntax):
from runtime_generics import runtime_generic, select
@runtime_generic
class MyGeneric[T]:
type_argument: type[T]
def __init__(self) -> None:
self.type_argument = select[T](self)
@classmethod
def whoami(cls):
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 runtime_generic, select
T = TypeVar("T")
@runtime_generic
class MyGeneric(Generic[T]):
type_argument: type[T]
def __init__(self) -> None:
self.type_argument = select[T](self)
@classmethod
def whoami(cls):
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 🚀
Happy to accept contributions!
[!Note] If you use Windows, it is highly recommended to complete the installation in the way presented below through WSL2.
First, 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
Then, run:
git clone https://github.com/bswck/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
Legal info
© Copyright by Bartosz Sławecki (@bswck).
This software is licensed under the 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-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b543f4ff141a27b7152f43dba50173742e43346746fa6ae3f4d92e3ef093d30 |
|
MD5 | 4c5953a19c449a6e085c0a96fc9a05da |
|
BLAKE2b-256 | 88442b861fe3d5c110f7838608e30fde8125c63b53c03b2e9b90f08c0b03f42f |