Between#
- class overload_numpy.constraints.Between(lower_bound: type, upper_bound: type)#
Bases:
TypeConstraint
Type constrained between two types.
This combines the functionality of
Covariant
andContravariant
.- Parameters:
- lower_boundtype
The child type of the argument.
- upper_boundtype
The parent type of the argument.
Notes
When compiled this class permits interpreted subclasses, see https://mypyc.readthedocs.io/en/latest/native_classes.html
Examples
For this example we need a type hierarchy:
>>> class A: pass >>> class B(A): pass >>> class C(B): pass >>> class D(C): pass >>> class E(D): pass
Construct the constraint object:
>>> constraint = Between(D, B)
This can be used to validate argument types:
>>> constraint.validate_type(A) False >>> constraint.validate_type(B) True >>> constraint.validate_type(C) True >>> constraint.validate_type(D) True >>> constraint.validate_type(E) False
- Attributes:
bounds
Return tuple of (lower, upper) bounds.
Methods
validate_object
(arg, /)Validate an argument.
validate_type
(arg_type, /)Validate the argument type.
Attributes Summary
Return tuple of (lower, upper) bounds.
Methods Summary
validate_type
(arg_type, /)Validate the argument type.
Attributes Documentation
- bounds#
Return tuple of (lower, upper) bounds.
The lower bound is contravariant, the upper bound covariant.
Examples
For this example we need a type hierarchy:
>>> class A: pass >>> class B(A): pass >>> class C(B): pass
>>> constraint = Between(C, B) >>> constraint.bounds (C, B)
Methods Documentation
- validate_type(arg_type: type, /) bool #
Validate the argument type.
This is used in
overload_numpy.mixin.NPArrayFuncOverloadMixin
and subclasses likeoverload_numpy.mixin.NPArrayOverloadMixin
to ensure that the input is of the correct set of types to work with the__array_function__
override.- Parameters:
- arg_typetype, positional-only
The type of the argument that must fit the type constraint.
- Returns:
- bool
Whether the type is valid.
Examples
The simplest built-in type constraint is
overload_numpy.constraints.Invariant
.>>> from overload_numpy.constraints import Invariant >>> constraint = Invariant(int) >>> constraint.validate_type(int) # exact type True >>> constraint.validate_type(bool) # subclass False