Reference
The public API is defined under the root of the reification package.
Usage: from reification import Reified
reification
Reified
Mixin class designed to facilitate the creation of new types based on reified type parameters. In most cases, this class should be placed before the normal generic class in the class inheritance list.
>>> from reification import Reified
>>> class ReifiedList[T](Reified, list[T]):
... pass
>>> xs = ReifiedList[int](range(10))
>>> xs.targ
<class 'int'>
Attributes:
-
targ(type | tuple[type | Any, ...] | Any) –The cached representative of the type argument(s) used to create the reified class.
-
type_args(tuple[type | Any, ...]) –A tuple containing the cached representative of the type arguments.
targ = Any
class-attribute
The type argument(s) that were specified when the reified generic class was instantiated.
If there is more than one type argument, targ will be a tuple containing each given type.
If a generic reified class is used without specifying its own type arguments, Any will be returned.
A non-generic subclass of a specialized reified class inherits the specialized value instead.
Equivalent type arguments share the same reified class. This attribute is initialized when that class is first created, so an equivalent later subscription does not replace the cached value.
type_args = (Any,)
class-attribute
A tuple containing the type argument(s) provided for the reified generic class.
Unlike targ, type_args always returns a tuple of the specified type arguments,
even when there's only one type argument. If a generic reified class is used without specifying
its own type arguments, it contains a single Any. A non-generic subclass of a specialized
reified class inherits the specialized tuple instead.
Equivalent type arguments share the same reified class. This attribute is initialized when that class is first created, so an equivalent later subscription does not replace the cached value.
__class_getitem__(params)
This dunder method, which the class overrides, is used for creating a new type each time it is called with distinct type arguments. It serves a key role in handling parameterized generic classes, enabling different identities for different type arguments of the same base class.
Note that this custom method violates Python's convention that __class_getitem__ should return an instance of GenericAlias.
All type arguments must be hashable and follow Python's normal hash contract so that they
can be used as stable cache keys. Typing constructs such as Annotated are not treated
specially; their metadata must therefore also be hashable.
Raises:
-
TypeError–If this class is
Reifieditself or any type argument is not hashable.