Invariant#

class overload_numpy.constraints.Invariant(bound: type)#

Bases: TypeConstraint

Type constraint for invariance – the exact type.

This is equivalent to arg_type is bound.

Parameters:
boundtype

The exact 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 = Invariant(int)

This can be used to validate argument types:

>>> constraint.validate_type(int)  # exact type
True
>>> constraint.validate_type(bool)  # subclass
False
>>> 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 like overload_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