Skip to content

Instantly share code, notes, and snippets.

@lewoudar
Created March 10, 2022 22:23
Show Gist options
  • Save lewoudar/d731816af5e55540205c4eaa77657b83 to your computer and use it in GitHub Desktop.
Save lewoudar/d731816af5e55540205c4eaa77657b83 to your computer and use it in GitHub Desktop.
A third example of pydantic argument validation using validate_arguments decorator and the Annotated typing feature
import math
from pydantic import validate_arguments, ValidationError, Field
from pydantic.typing import Annotated
@validate_arguments
def get_hypotenuse(
a: Annotated[float, Field(gt=0)],
b: Annotated[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