blueprints.utils.typechecker module#
The Typechecker class is used to restrict the argument types of functions and methods. If a function or method is decoraded with TypeChecker.restrict its arguments and returned values are checked against the type hints, if provided and a TypeError is raised if they are violated.
- exception blueprints.utils.typechecker.ArgumentError[source]#
Bases:
ExceptionThis class is used to construct the error message for restricted type violations and to be caught in the methods
@restrictwrapper call as distinct from other errors that might have occured further down the traceback.- __init__(arg, arg_type, arg_name, name)[source]#
The ArgumentError is used to construct the Error message for a restricted type violation.
- Parameters:
arg (
object) – The argument given to the function.arg_type (
type) – The type as restricted by the function signature.arg_name (
str) – The name of the argument from the function signature.name (
str) – The name of the method or function called.
- class blueprints.utils.typechecker.TypeChecker[source]#
Bases:
objectTypeChecker controls function and class methods arguments for their validity. If the given type of an argument violates the type hint, a TypeError is raised. Use TypeChecker.restrict as a decorator to restrict a function or methods types.
- classmethod restrict(func)[source]#
Used as a decorator it ansurses that the decorated methods or functions arguments are type restricted according to the arguments type hints if provided. You can use None as an optional type hint instead of Nonetype.
- Parameters:
func (
FunctionType | MethodType) – A function or method to be type restricted.- Returns:
A wrapped function that resitrcts argument and return value types and raises a TypeError if violated.
- Return type:
TYPE
- blueprints.utils.typechecker.restrict(func)#
Used as a decorator it ansurses that the decorated methods or functions arguments are type restricted according to the arguments type hints if provided. You can use None as an optional type hint instead of Nonetype.
- Parameters:
func (
FunctionType | MethodType) – A function or method to be type restricted.- Returns:
A wrapped function that resitrcts argument and return value types and raises a TypeError if violated.
- Return type:
TYPE