-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc91939
commit 457188e
Showing
2 changed files
with
58 additions
and
3 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
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,55 @@ | ||
|
||
import json | ||
from ju.pydantic_util import ModelSource, pydantic_model_to_code, data_to_pydantic_model | ||
|
||
|
||
|
||
def test_pydantic_model_to_code() -> str: | ||
""" | ||
>>> json_schema: str = '''{ | ||
... "type": "object", | ||
... "properties": { | ||
... "number": {"type": "number"}, | ||
... "street_name": {"type": "string"}, | ||
... "street_type": {"type": "string", | ||
... "enum": ["Street", "Avenue", "Boulevard"] | ||
... } | ||
... } | ||
... }''' | ||
>>> print(pydantic_model_to_code(json_schema)) | ||
from __future__ import annotations | ||
<BLANKLINE> | ||
from enum import Enum | ||
from typing import Optional | ||
<BLANKLINE> | ||
from pydantic import BaseModel | ||
<BLANKLINE> | ||
<BLANKLINE> | ||
class StreetType(Enum): | ||
Street = 'Street' | ||
Avenue = 'Avenue' | ||
Boulevard = 'Boulevard' | ||
<BLANKLINE> | ||
<BLANKLINE> | ||
class Model(BaseModel): | ||
number: Optional[float] = None | ||
street_name: Optional[str] = None | ||
street_type: Optional[StreetType] = None | ||
<BLANKLINE> | ||
This means you can get some model code from an example data dict, | ||
using pydantic_model_to_code | ||
>>> M = data_to_pydantic_model({"name": "John", "age": 30}, "Simple") | ||
>>> print(pydantic_model_to_code(M)) | ||
from __future__ import annotations | ||
<BLANKLINE> | ||
from pydantic import BaseModel, Field | ||
<BLANKLINE> | ||
<BLANKLINE> | ||
class Simple(BaseModel): | ||
name: str = Field(..., title='Name') | ||
age: int = Field(..., title='Age') | ||
<BLANKLINE> | ||
""" |