Created
March 10, 2022 22:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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