Skip to main content

Reification (Python library)

Unit Tests

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

Release files for reification 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reification 1.2.2
File Size Uploaded
reification-1.2.2.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for reification 1.2.2
File Interpreter ABI Platform
reification-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / reification-1.2.2.tar.gz

Download URL reification-1.2.2.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
023d5e0673096ffc8cdea71c1e02ad1276ae558f2211bee295946e3133d3fa65
BLAKE2b-256 checksum
How to use checksums
d8d2c4fe21cbd8409775f3aac98c0d57bc50d6bf03b6bdf8c0c566cae1e8b4af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / reification-1.2.2-py3-none-any.whl

Download URL reification-1.2.2-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
24678e43e990a722dc16f71fe7bf61d6f62ad08eb44166513361b5f1d6497832
BLAKE2b-256 checksum
How to use checksums
08cfa71891618eb2c2823a089c630d12080e9f8b54d99e22399f72a6724f9f9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page