subtypes module

class cls.subtypes.Subtypes(environment: dict[object, set])[source]

The Subtypes class performs subtype checking and casting in a type environment.

Parameters

environment (dict[object, set]) – A dictionary mapping objects to sets of objects.

static _reflexive_closure(env: dict[object, set]) dict[object, set][source]

Compute the reflexive closure of a given subtype environment.

Parameters

env (dict[object, set]) – The input subtype environment.

Returns

The reflexive closure of the input subtype environment.

Return type

dict[object, set]

_tgt_for_srcs(gte: Type, inseq: Sequence[tuple[cls.types.Type, cls.types.Type]]) Iterator[Type][source]

Generator for targets for a given source.

Parameters
  • gte (Type) – A type to use for the comparison.

  • inseq (Sequence[tuple[Type, Type]]) – A sequence of tuples of types.

Returns

An iterator of types.

Return type

Iterator[Type]

static _transitive_closure(env: dict[object, set]) dict[object, set][source]

Compute the transitive closure of a given subtype environment.

Parameters

env (dict[object, set]) – The input subtype environment.

Returns

The transitive closure of the input subtype environment.

Return type

dict[object, set]

cast(to: Type, castee: Type) Sequence[source]

Cast the given type castee to the type to.

Parameters
  • to (Type) – The target type to cast castee to.

  • castee (Type) – The type to be cast.

Returns

A sequence of pairs of types, representing the casting path from castee to to.

Return type

Sequence

Raises

TypeError – If to is unsupported.

check_subtype(subtype: Type, supertype: Type) bool[source]

Check if subtype is a subtype of supertype.

Parameters
  • subtype (Type) – The subtype to check.

  • supertype (Type) – The supertype to check.

Returns

True if subtype is a subtype of supertype, False otherwise.

Return type

bool

minimize(tys: set[cls.types.Type]) set[cls.types.Type][source]

Minimize a set of types.

Parameters

tys (set[Type]) – The set of types to be minimized.

Returns

The minimized set of types.

Return type

set[Type]