Skip to content

Instantly share code, notes, and snippets.

@lewoudar
Last active March 11, 2022 20:41
Show Gist options
  • Select an option

  • Save lewoudar/23423cbc58a8f4264aa82386c3e549e1 to your computer and use it in GitHub Desktop.

Select an option

Save lewoudar/23423cbc58a8f4264aa82386c3e549e1 to your computer and use it in GitHub Desktop.
An example of argument validation without using pydantic
import math
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
def compute_distance(a: Point, b: Point) -> float:
if not isinstance(a, Point):
raise TypeError
if not isinstance(b, Point):
raise TypeError
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)
print('distance between p1 and p2 is:', compute_distance(p1, p2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment