Covariant#
- class overload_numpy.constraints.Covariant(bound: type)#
Bases:
TypeConstraint
A covariant constraint – permitting subclasses.
This is the most common constraint, equivalent to
issubclass(arg_type, bound)
.- Parameters:
- 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
Construct the constraint object:
>>> constraint = Covariant(int)
This can be used to validate argument types:
>>> constraint.validate_type(int) # exact type True >>> constraint.validate_type(bool) # subclass True >>> constraint.validate_type(object) # superclass False
Methods
validate_object
(arg, /)Validate an argument.
validate_type
(arg_type, /)Validate the argument type.
Methods Summary
validate_type
(arg_type, /)Validate the argument type.
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