Last active
March 11, 2022 20:46
-
-
Save lewoudar/65af8fb76a25bb6faf25528aec5185f8 to your computer and use it in GitHub Desktop.
An example of use of pydantic decorator validate_arguments
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 dataclasses import dataclass | |
from pydantic import validate_arguments, ValidationError | |
@dataclass | |
class Point: | |
x: int | |
y: int | |
@validate_arguments | |
def compute_distance(a: Point, b: Point) -> float: | |
x = math.pow(a.x - b.x, 2) | |
y = math.pow(a.y - b.y, 2) | |
return math.sqrt(x + y) | |
p1 = Point(1, 2) | |
p2 = Point(3, 5) | |
try: | |
print('distance between p1 and p2 is:', compute_distance(p1, p2)) | |
except ValidationError as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment