Skip to main content

Reified generics in Python to get type parameters at runtime

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reification-1.2.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reification-1.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file reification-1.2.0.tar.gz.

File metadata

  • Download URL: reification-1.2.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for reification-1.2.0.tar.gz
Algorithm Hash digest
SHA256 709dd2b6d38f1af8eaa9f912b7e6eb20707b607acf764fa832f9fcad3c25583d
MD5 f3f5677dae1e39d54f328c798106fb80
BLAKE2b-256 c1d1253ee05a7b331ecaa45b642e5fa46180472506b239d142c93fc04340f68a

See more details on using hashes here.

File details

Details for the file reification-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: reification-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for reification-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc7f231b06c0e12824ce3bbe9e1fca069410d610013ba40ab3809d9b6411d095
MD5 a22eed3915023bd3d47b65c155c55f91
BLAKE2b-256 bb6ab0beb8c0c7c3035328aec4da00b0ff95e53ea680cccca8f3d03fe9baf747

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page