Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add section for create_model_from_{namedtuple,typeddict}
- Loading branch information
1 parent
3361496
commit d737f07
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import TypedDict | ||
|
||
from pydantic import ValidationError, create_model_from_typeddict | ||
|
||
|
||
class User(TypedDict): | ||
name: str | ||
id: int | ||
|
||
|
||
class Config: | ||
extra = 'forbid' | ||
|
||
|
||
UserM = create_model_from_typeddict(User, __config__=Config) | ||
print(repr(UserM(name=123, id='3'))) | ||
|
||
try: | ||
UserM(name=123, id='3', other='no') | ||
except ValidationError as e: | ||
print(e) |
This file contains 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