scenic.core.type_support
Support for checking Scenic types.
This module provides a system for checking that values passed to Scenic operators and
functions have the expected types. The top-level function toTypes
and its
specializations toType
, toVector
, toScalar
, etc. can also coerce closely-related
types into the desired type in some cases. For lazily-evaluated values (random values and
delayed arguments of specifiers), it may not be possible to determine the type at object
creation time: in such cases these functions return a lazily-evaluated object that
performs the type check either during specifier resolution or sampling as needed.
In general, the only objects which are coercible to a type T are instances of that type,
together with Distribution
objects whose _valueType is a type coercible to T (and
therefore whose sampled value can be coerced to T). However, we also have the following
exceptional rules:
- Coercible to a scalar (type
float
):
Instances of
numbers.Real
(coerced by callingfloat
on them); this includes NumPy types such asnumpy.single
- Coercible to a heading (type
Heading
):
Anything coercible to a scalar
Any type with a toHeading method (including
OrientedPoint
)
Summary of Module Members
Functions
Can this value be coerced into the given type? |
|
Can values of typeA be coerced into typeB? |
|
Coerce something into the given type. |
|
Coerce something into any of the given types, raising an error if impossible. |
|
|
|
|
|
Evaluate the func, assuming thingA and thingB have the same type. |
|
Is this guaranteed to evaluate to a member of the given Scenic type? |
|
Whether this is a pre-3.9 generic type from the typing module. |
|
Convert something to a heading, raising an error if impossible. |
|
Convert something to an orientation, raising an error if impossible. |
|
Convert something to a scalar, raising an error if impossible. |
|
Convert something to a given type, raising an error if impossible. |
|
Convert something to any of the given types, raising an error if impossible. |
|
Convert something to a vector, raising an error if impossible. |
|
What type this value ultimately evaluates to, if we can tell. |
|
Most specific type unifying the given types. |
|
Most specific type unifying the given values. |
Classes
Dummy class used as a target for type coercions to headings. |
|
Checks that a given lazy value has one of a given list of types. |
|
Evaluates a function after checking that two lazy values have the same type. |
|
Distribution which typechecks its value at sampling time. |
Exceptions
Raised by coercion functions when coercion is impossible. |
Member Details
- class Heading(x=0, /)[source]
Bases:
float
Dummy class used as a target for type coercions to headings.
- coerce(thing, ty, error='wrong type')[source]
Coerce something into the given type.
Used internally by
toType
, etc.; this function should not otherwise be called directly.
- exception CoercionFailure[source]
Bases:
Exception
Raised by coercion functions when coercion is impossible.
Only used internally; will be converted to a parse error for reporting to the user.
- class TypecheckedDistribution(dist, ty, errorMessage, coercer=None)[source]
Bases:
Distribution
Distribution which typechecks its value at sampling time.
Only for internal use by the typechecking system; introduced by
coerce
when it is unable to guarantee that a random value will have the correct type after sampling. Note that the type check is not a purely passive operation, and may actually transform the sampled value according to the coercion rules above (e.g. a sampledPoint
will be converted to aVector
in a context which expects the latter).
- coerceToAny(thing, types, error)[source]
Coerce something into any of the given types, raising an error if impossible.
Only for internal use by the typechecking system; called from
toTypes
.- Raises:
TypeError – if it is impossible to coerce the value into any of the types.
- toTypes(thing, types, typeError='wrong type')[source]
Convert something to any of the given types, raising an error if impossible.
Types are tried in the order they are given: the first one compatible with the given value is used. Coercions of closely-related types may take place as described in the module documentation above.
If the given value requires lazy evaluation, this function returns a
TypeChecker
object that performs the type conversion after specifier resolution.
- toType(thing, ty, typeError='wrong type')[source]
Convert something to a given type, raising an error if impossible.
Equivalent to
toTypes
with a single destination type.
- toScalar(thing, typeError='non-scalar in scalar context')[source]
Convert something to a scalar, raising an error if impossible.
See
toTypes
for details.
- toHeading(thing, typeError='non-heading in heading context')[source]
Convert something to a heading, raising an error if impossible.
See
toTypes
for details.
- toOrientation(thing, typeError='non-orientation in orientation context')[source]
Convert something to an orientation, raising an error if impossible.
See
toTypes
for details.
- toVector(thing, typeError='non-vector in vector context')[source]
Convert something to a vector, raising an error if impossible.
See
toTypes
for details.
- evaluateRequiringEqualTypes(func, thingA, thingB, typeError='type mismatch')[source]
Evaluate the func, assuming thingA and thingB have the same type.
If func produces a lazy value, it should not have any required properties beyond those of thingA and thingB.
- Raises:
TypeError – if thingA and thingB do not have the same type.
- class TypeChecker(*args, _internal=False, **kwargs)[source]
Bases:
DelayedArgument
Checks that a given lazy value has one of a given list of types.
- class TypeEqualityChecker(*args, _internal=False, **kwargs)[source]
Bases:
DelayedArgument
Evaluates a function after checking that two lazy values have the same type.