Type System#
Type Creation and Conversion#
Create a pystencils type object from a variety of specifications. |
|
Like |
|
Represent a PEP 604 union type |
|
Adds the const qualifier to a given type. |
|
Removes the const qualifier from a given type. |
Data Type Class Hierarchy#
These are the classes that make up the type system internally. Most of the time, you will not be using them directly, so you can skip over this part unless you have very particular needs.

Base class for all pystencils types. |
|
Class to model custom types by their names. |
|
Named or anonymous structured data type. |
|
Base class for subscriptable types. |
|
A C pointer with arbitrary base type. |
|
Multidimensional array of fixed shape. |
|
Numeric data type, i.e. any type that may occur inside arithmetic-logical expressions. |
|
Scalar numeric type. |
|
Packed vector of numeric type. |
|
Signed and unsigned integer types. |
|
Boolean type. |
|
Unsigned integer types. |
|
Signed integer types. |
|
IEEE-754 floating point data types |
Data Type Abbreviations#
The pystencils.types.quick
module contains aliases of most of the above data type classes,
in order to reduce verbosity of code using the type system.
|
Alias of |
|
Alias of |
|
Alias of |
|
Alias of |
|
Alias of |
|
Alias of |
|
Alias of |
|
Alias of |
|
Alias of |
|
Alias of |
Exceptions#
Indicates an error relating to incorrect usage of a pystencils type. |
Implementation Details#
Caching of Instances#
To handle and compare types more efficiently, the pystencils type system customizes class instantiation to cache and reuse existing instances of types. This means, for example, if a 32-bit const unsigned integer type gets created in two places in the program, the resulting objects are exactly the same:
>>> from pystencils.types import PsUnsignedIntegerType
>>> t1 = PsUnsignedIntegerType(32, const=True)
>>> t2 = PsUnsignedIntegerType(32, const=True)
>>> t1 is t2
True
This mechanism is implemented by the metaclass PsTypeMeta
. It is not perfect, however;
some parts of Python that bypass the regular object creation sequence, such as pickle
and
copy.copy
, may create additional instances of types.
- class pystencils.types.meta.PsTypeMeta(name, bases, namespace, **kwargs)#
Metaclass for the
PsType
hierarchy.PsTypeMeta
holds an internal cache of all created instances ofPsType
and overrides object creation such that whenever a type gets instantiated more than once with the same argument list, instead of creating a new object, the existing object is returned.
Extending the Type System#
When extending the type system’s class hierarchy, new classes need to implement at least the internal
method __args__
. This method, when called on a type object, must return a hashable sequence of arguments
– not including the const-qualifier –
that can be used to recreate that exact type. It is used internally to compute hashes and compare equality
of types, as well as for const-conversion.
- pystencils.types.PsType.__args__(self)#
Return the arguments used to create this instance, in canonical order, excluding the const-qualifier.
The tuple returned by this method must be hashable and for each instantiable subclass
MyType
ofPsType
, the following must hold:t = MyType(< arguments >) assert MyType(*t.__args__(), const=t.const) == t