Skip to content

Instantly share code, notes, and snippets.

@lewoudar
Last active March 10, 2022 22:23
Show Gist options
  • Save lewoudar/a5e0ff8b00856073e86f0d0e5740e60e to your computer and use it in GitHub Desktop.
Save lewoudar/a5e0ff8b00856073e86f0d0e5740e60e to your computer and use it in GitHub Desktop.
A second example of pydantic argument validation using validate_arguments decorator
import math
from pydantic import validate_arguments, ValidationError, Field
@validate_arguments
def get_hypotenuse(
a: float = Field(..., gt=0),
b: float = Field(..., gt=0)
) -> float:
return math.sqrt(a ** 2 + b ** 2)
try:
print(get_hypotenuse(1, 3))
except ValidationError as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment