Last active
October 29, 2020 07:09
-
-
Save gidgid/84793f52e1f6af96ab9ed90bd60a3b26 to your computer and use it in GitHub Desktop.
The functions signatures already show the difference between these 2 approaches
This file contains hidden or 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
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