-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Schemas #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Schemas #190
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
37a60e2
starting field and model schemas
samuelcolvin 23146fc
field.schema()
samuelcolvin 31b237e
sub-models working
samuelcolvin 4f32861
move default in Schema and tests
samuelcolvin 3057c6a
Merge remote-tracking branch 'origin/master' into schema
samuelcolvin af3bada
adding schema
samuelcolvin 2bcb7df
tweak docs
samuelcolvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
sub-models working
- Loading branch information
commit 31b237e63dfe2fd83957f248a3de641be404c74f
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| from enum import Enum, IntEnum | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| def test_key(): | ||
| class ApplePie(BaseModel): | ||
| """ | ||
| This is a test. | ||
| """ | ||
| a: float | ||
| b: int = 10 | ||
|
|
||
| s = { | ||
| 'type': 'object', | ||
| 'title': 'ApplePie', | ||
| 'description': 'This is a test.', | ||
| 'properties': { | ||
| 'a': { | ||
| 'type': 'float', | ||
| 'required': True, | ||
| 'title': 'A', | ||
| }, | ||
| 'b': { | ||
| 'type': 'int', | ||
| 'required': False, | ||
| 'title': 'B', | ||
| 'default': 10, | ||
| }, | ||
| }, | ||
| } | ||
| assert True not in ApplePie._schema_cache | ||
| assert False not in ApplePie._schema_cache | ||
| assert ApplePie.schema() == s | ||
| assert True in ApplePie._schema_cache | ||
| assert False not in ApplePie._schema_cache | ||
| assert ApplePie.schema() == s | ||
|
|
||
|
|
||
| def test_by_alias(): | ||
| class ApplePie(BaseModel): | ||
| a: float | ||
| b: int = 10 | ||
|
|
||
| class Config: | ||
| title = 'Apple Pie' | ||
| fields = {'a': 'Snap', 'b': 'Crackle'} | ||
|
|
||
| s = { | ||
| 'type': 'object', | ||
| 'title': 'Apple Pie', | ||
| 'properties': { | ||
| 'Snap': { | ||
| 'type': 'float', | ||
| 'required': True, | ||
| 'title': 'Snap', | ||
| }, | ||
| 'Crackle': { | ||
| 'type': 'int', | ||
| 'required': False, | ||
| 'title': 'Crackle', | ||
| 'default': 10, | ||
| }, | ||
| }, | ||
| } | ||
| assert ApplePie.schema() == s | ||
| assert ApplePie.schema() == s | ||
| assert list(ApplePie.schema(by_alias=True)['properties'].keys()) == ['Snap', 'Crackle'] | ||
| assert list(ApplePie.schema(by_alias=False)['properties'].keys()) == ['a', 'b'] | ||
|
|
||
|
|
||
| def test_sub_model(): | ||
| class Foo(BaseModel): | ||
| """hello""" | ||
| b: float | ||
|
|
||
| class Bar(BaseModel): | ||
| a: int | ||
| b: Foo = None | ||
|
|
||
| assert Bar.schema() == { | ||
| 'type': 'object', | ||
| 'title': 'Bar', | ||
| 'properties': { | ||
| 'a': { | ||
| 'type': 'int', | ||
| 'title': 'A', | ||
| 'required': True, | ||
| }, | ||
| 'b': { | ||
| 'type': 'object', | ||
| 'title': 'Foo', | ||
| 'properties': { | ||
| 'b': { | ||
| 'type': 'float', | ||
| 'title': 'B', | ||
| 'required': True, | ||
| }, | ||
| }, | ||
| 'description': 'hello', | ||
| 'required': False, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| def test_choices(): | ||
| FooEnum = Enum('FooEnum', {'foo': 'f', 'bar': 'b'}) | ||
| BarEnum = IntEnum('BarEnum', {'foo': 1, 'bar': 2}) | ||
|
|
||
| class Model(BaseModel): | ||
| foo: FooEnum | ||
| bar: BarEnum | ||
|
|
||
| assert Model.schema() == { | ||
| 'type': 'object', | ||
| 'title': 'Model', | ||
| 'properties': { | ||
| 'foo': { | ||
| 'type': 'enum', | ||
| 'title': 'Foo', | ||
| 'required': True, | ||
| 'choices': [ | ||
| ('f', 'foo'), | ||
| ('b', 'bar'), | ||
| ], | ||
| }, | ||
| 'bar': { | ||
| 'type': 'int', | ||
| 'title': 'Bar', | ||
| 'required': True, | ||
| 'choices': [ | ||
| (1, 'foo'), | ||
| (2, 'bar'), | ||
| ], | ||
| }, | ||
| }, | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a docstring and/or comment would be helpful here to know what this class is for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do, I need to do docs for this in general.