Reuse type arguments explicitly passed at runtime to a generic class before instantiating.
Project description
runtime_generics 1.0.3
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.
Simple example
3.12+ (PEP 695 syntax):
from runtime_generics import get_arg, runtime_generic
@runtime_generic
class MyGeneric[T]:
type_argument: type[T]
def __init__(self) -> None:
self.type_argument = get_arg(self)
my_generic = MyGeneric[int]()
assert my_generic.type_argument is int
3.8+:
from __future__ import annotations
from typing import Generic, TypeVar
from runtime_generics import get_arg, runtime_generic
T = TypeVar("T")
@runtime_generic
class MyGeneric(Generic[T]):
type_argument: type[T]
def __init__(self) -> None:
self.type_argument = get_arg(self)
my_generic = MyGeneric[int]()
assert my_generic.type_argument is int
Installation
For users 💻
pip install runtime-generics
For developers ❤️
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.
curl -sSL https://install.python-poetry.org | python3 -
This way of installing Poetry on Linux is valid as of 2023-10-27.
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 && cd runtime_generics && ./install && poetry shell
Legal info
© Copyright by 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.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3834bfec6c0591fb220b473bf6da191f90c9217182f719c46d0508ad2c9bdf9 |
|
MD5 | 94a4f3f075eaa40638f28bb588ffebe3 |
|
BLAKE2b-256 | 1cce6bb80e162e9dd564b5e06d6f4d47457920f1a62897c42fad019edc253a70 |