Reification (Python library)
Reified generics in Python to get type parameters at runtime
from reification import Reified
class ReifiedList[T](Reified, list[T]):
pass
xs = ReifiedList[int](range(10))
print(xs) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(xs.targ) # <class 'int'>
See the detailed documentation at https://curegit.github.io/reification/.
Requirements
- Python >= 3.12
This library is written in pure Python and does not require any external modules.
Installation
pip install reification
Example Usage: Type-Checked Generic Stack
from reification import Reified
class ReifiedStack[T](Reified):
def __init__(self) -> None:
super().__init__()
self.items: list[T] = []
def push(self, item: T) -> None:
# We can do runtime check
if isinstance(item, self.targ):
self.items.append(item)
else:
raise TypeError()
def pop(self) -> T:
if self.items:
return self.items.pop()
else:
raise IndexError("pop from empty stack")
stack = ReifiedStack[str]()
stack.push("spam") # OK
stack.push(42) # raises TypeError
The ReifiedStack class created here is generic and derived from the Reified base class, and implements a simple stack with push and pop methods.
In the push method, we are checking at runtime if the item being pushed is of the specified generic type (this type is accessible via the targ attribute inherited from Reified).
If the type of the item does not match, a TypeError is raised.
In the example usage, we create an instance of the ReifiedStack class with str as the type argument. When we try to push a string "spam", the item is accepted since it matches the stack's specified type argument.
However, when we try to push an integer 42, a TypeError is raised because the type of the item does not match the stack's type argument.
This demonstrates the use of reified generics in Python where we can have runtime access to the type parameters, enabling us to type check dynamically at runtime. This is useful in situations where we need to enforce type safety in our code or use type information at runtime.
API
See the full API reference at https://curegit.github.io/reification/reference/.
License
WTFPL
Copyright (C) 2023 curegit
Metadata
Release files for reification 1.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| reification-1.2.3.tar.gz | 8.6 kB | Without provenance | Details for reification-1.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| reification-1.2.3-py3-none-any.whl | Python 3 | none | any | Details for reification-1.2.3-py3-none-any.whl |
Total release size: 14.1 kB
Release files / reification-1.2.3.tar.gz
| Download URL | reification-1.2.3.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8f0820ee880498095084ce1128bffe3be79c7e060ad56faf71d305abbbeb0569
|
|
BLAKE2b-256 checksum How to use checksums |
116406d81f08acc52e9a023b0eccf2e9cc2bea9fa8d9e45bd2b51ae737748334
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Release files / reification-1.2.3-py3-none-any.whl
| Download URL | reification-1.2.3-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cac57699be667721996025117bf347adcc6d03afbe8ea8612c7ae412d8224d32
|
|
BLAKE2b-256 checksum How to use checksums |
8012629942684710201b0a96e8c08faf2528f5fc53e9e29b2a7aa6f7361179a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Security for reification 1.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.