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. All types inherit from it.

class runtype.base_types.AnyType

Represents the Any type.

For any type ‘t’ within the typesystem, t is a subtype of Any (or: t <= Any)

class runtype.base_types.DataType

Abstract class for a data type.

Example of possible data-types: int, float, text

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 tuple of types.

class runtype.base_types.ContainerType

Base class for containers, such as generics.

class runtype.base_types.GenericType(base: Type, item: Union[type, Type] = Any)

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.

class runtype.base_types.PhantomGenericType(base, item=Any)

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.

abstract 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.

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.