Skip to content

Instantly share code, notes, and snippets.

@lewoudar
Last active March 11, 2022 20:46
Show Gist options
  • Save lewoudar/65af8fb76a25bb6faf25528aec5185f8 to your computer and use it in GitHub Desktop.
Save lewoudar/65af8fb76a25bb6faf25528aec5185f8 to your computer and use it in GitHub Desktop.
An example of use of pydantic decorator validate_arguments
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