Skip to content
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

Change schema frozenset #1560

Merged
merged 7 commits into from Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/1557-wangpeibao.md
@@ -0,0 +1,2 @@
change schema.py/field_class_to_schema
add frozenset type to the tuple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
change schema.py/field_class_to_schema
add frozenset type to the tuple
change `schema.field_class_to_schema` to support `frozenset` in schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I do not know how to add tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to commit this suggested change.

3 changes: 3 additions & 0 deletions changes/index-1.html
@@ -0,0 +1,3 @@
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>未知 </title></head><body>
samuelcolvin marked this conversation as resolved.
Show resolved Hide resolved
<p>Fix model validation to handle nested literals, e.g. <code>Literal['foo', Literal['bar']]</code>.</p>
</body></html>
1 change: 1 addition & 0 deletions pydantic/schema.py
Expand Up @@ -623,6 +623,7 @@ def field_singleton_sub_fields_schema(
(list, {'type': 'array', 'items': {}}),
(tuple, {'type': 'array', 'items': {}}),
(set, {'type': 'array', 'items': {}, 'uniqueItems': True}),
(frozenset, {'type': 'array', 'items': {}, 'uniqueItems': True}),
)

json_scheme = {'type': 'string', 'format': 'json-string'}
Expand Down
7 changes: 6 additions & 1 deletion tests/test_schema.py
Expand Up @@ -1837,7 +1837,10 @@ class Model(BaseModel):
def test_frozen_set():
class Model(BaseModel):
a: FrozenSet[int] = frozenset({1, 2, 3})
b: FrozenSet = frozenset({1, 2, 3})
c: frozenset = frozenset({1, 2, 3})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c: frozenset = frozenset({1, 2, 3})
c: frozenset = frozenset({1, 2, 3})
d: frozenset = ...

Just to check the case when there's no default (obviously you'll need to change the check below too.


print(Model.schema())
assert Model.schema() == {
'title': 'Model',
'type': 'object',
Expand All @@ -1848,7 +1851,9 @@ class Model(BaseModel):
'type': 'array',
'items': {'type': 'integer'},
'uniqueItems': True,
}
},
'b': {'title': 'B', 'default': frozenset({1, 2, 3}), 'type': 'array', 'items': {}, 'uniqueItems': True},
'c': {'title': 'C', 'default': frozenset({1, 2, 3}), 'type': 'array', 'items': {}, 'uniqueItems': True},
},
}

Expand Down