Skip to content

Commit

Permalink
Update book example with typing.Literal example
Browse files Browse the repository at this point in the history
  • Loading branch information
pappasam committed Nov 24, 2020
1 parent d935fe3 commit 38a3213
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion example/books/book.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"firstname": "Jane"
},
"title": "The Ultimate Database Study Guide",
"category": ["Non-Fiction", "Technology"]
"category": ["Non-Fiction", "Technology"],
"difficulty": "medium"
}
6 changes: 3 additions & 3 deletions example/books/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
from dataclasses import dataclass
from pprint import pprint
from typing import List, TypeVar
from typing import List, Literal, TypeVar

import serdelicacy
from serdelicacy import OptionalProperty
Expand Down Expand Up @@ -34,10 +34,9 @@ class Book:
title: str
category: List[str]
second_author: OptionalProperty[str]
difficulty: Literal["easy", "medium", "hard"]

def __post_init__(self) -> None:
# if self.second_author is UNDEFINED:
# self.second_author = Person("John", "Doe")

if len(self.category) < 2:
raise ValueError("Must have at least 2 caregories")
Expand All @@ -54,6 +53,7 @@ def __post_init__(self) -> None:
print(loaded.title)
print(type(loaded.author.firstname))
print(loaded.second_author)
print(loaded.difficulty)

unloaded1 = serdelicacy.dump(loaded)
pprint(unloaded1)
Expand Down

0 comments on commit 38a3213

Please sign in to comment.