Skip to main content
100%

Contracts in Python

✓ Published0🌍 Public
Aadewes
Last edited Jul 12, 2017
Created on Jul 12, 2017

This example demonstrates runtime type checking for Python function arguments and return values using function annotations and custom validator classes. The code defines a `Validated` metaclass and validator classes like `TypeChecked`, `Range`, and `Positive`, which are combined through the `checked` decorator to enforce constraints such as type and value ranges on parameters like `x`, `y`, and `z`. It uses the `inspect` module to parse function signatures and annotations, and the `functools` module for wrapping. The sample function `f` shows valid and invalid calls, raising `TypeError` for type mismatches and `ValueError` for out-of-range values.

AI-generated description

Similar vizzes