Type Classes
A collection of classes that serve as building blocks for making your own type system.
You can use that type system to:
reason about your program’s logic
use it with runtype for customized dispatch and validation.
Types
Note: These types are not specific to the Python type system!
- class runtype.base_types.Type
Abstract Type class. Every type inherit from it.
- class runtype.base_types.AnyType
Represents the Any type.
Any may contain any other type, and be contained by any other type.
For any type ‘t’ within the typesystem, t is a subtype of Any (or: t <= Any) But also Any is a subtype of t (or: Any <= t)
- class runtype.base_types.DataType
Abstract class for a data type.
A data-type is any type that contains non-type information.
Example of possible data-types: int, float, text, list
- class runtype.base_types.SumType(types)
Implements a sum type, i.e. a disjoint union of a set of types.
Similar to Python’s typing.Union.
- class runtype.base_types.ProductType(types)
Implements a product type, i.e. a record / tuple / struct
- class runtype.base_types.ContainerType
Base class for containers, such as generics.
- class runtype.base_types.GenericType(base: Type, item: Union[type, Type], variance)
Implements a generic type. i.e. a container for items of a specific type.
For any two generic types a[i] and b[j], it’s true that a[i] <= b[j] iff a <= b and i <= j.
- class runtype.base_types.PhantomType
Implements a base for phantom types.
A phantom type is a “meta” type that can wrap existing types, but it is transparent (subtype checks may skip over it), and has no effect otherwise.
- class runtype.base_types.PhantomGenericType(base, item=All)
Implements a generic phantom type, for carrying metadata within the type signature.
For any phantom type p[i], it’s true that p[i] <= p but also p[i] <= i and i <= p[i].
- class runtype.base_types.Validator
Defines the validator interface.
- validate_instance(obj, sampler: Optional[Callable[[Sequence], Sequence]] = None)
Validates obj, raising a TypeMismatchError if it does not conform.
If sampler is provided, it will be applied to the instance in order to validate only a sample of the object. This approach may validate much faster, but might miss anomalies in the data.
- abstract test_instance(obj, sampler=None)
Tests obj, returning a True/False for whether it conforms or not.
If sampler is provided, it will be applied to the instance in order to validate only a sample of the object.
- class runtype.base_types.Constraint(for_type, predicates)
Defines a constraint, which activates during validation.