Skip to content

Instantly share code, notes, and snippets.

@gidgid
Last active October 29, 2020 07:09
Show Gist options
  • Save gidgid/84793f52e1f6af96ab9ed90bd60a3b26 to your computer and use it in GitHub Desktop.
Save gidgid/84793f52e1f6af96ab9ed90bd60a3b26 to your computer and use it in GitHub Desktop.
The functions signatures already show the difference between these 2 approaches
from dataclasses import dataclass
from typing import List
# Users have:
# a name which is a non-empty string
# an age which is a positive int
# at least one hobbie (which is a non empty list)
@dataclass
class User:
name: str
age: int
hobbies: List[str]
def validate_user(possible_user: dict) -> None:
"""Validates the inputs and raises a ValueError if unsuccessful"""
def parse_user(possible_user: dict) -> User:
"""Parses the input and returns a User if successful"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment