8000 move default in Schema and tests · pydantic/pydantic@dbfc10e · GitHub
[go: up one dir, main page]

Skip to content

Commit dbfc10e

Browse files
committed
move default in Schema and tests
1 parent d83aee9 commit dbfc10e

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

pydantic/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .env_settings import BaseSettings
33
from .error_wrappers import ValidationError
44
from .errors import *
5-
from .fields import Required
5+
from .fields import Required, Schema
66
from .main import BaseConfig, BaseModel, create_model, validator
77
from .parse import Protocol
88
from .types import *

pydantic/fields.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ class Validator(NamedTuple):
3333

3434

3535
class Schema:
36-
__slots__ = 'alias', 'title', 'choice_names', 'extra',
36+
"""
37+
Used to provide extra information about a field in a model schema.
38+
"""
39+
__slots__ = 'default', 'alias', 'title', 'choice_names', 'extra',
3740

38-
def __init__(self, alias=None, title=None, choice_names=None, **extra):
41+
def __init__(self, default, *, alias=None, title=None, choice_names=None, **extra):
42+
self.default = default
3943
self.alias = alias
4044
self.title = title
4145
self.choice_names = choice_names
@@ -59,7 +63,7 @@ def __init__(
5963
model_config: Any,
6064
alias: str=None,
6165
allow_none: bool=False,
62-
schema: str=None):
66+
schema: Schema=None):
6367

6468
self.name: str = name
6569
self.alias: str = alias or name
@@ -83,10 +87,11 @@ def __init__(
8387
@classmethod
8488
def infer(cls, *, name, value, annotation, class_validators, config):
8589
schema_from_config = config.get_field_schema(name)
86-
if isinstance(value, tuple) and len(value) == 2 and isinstance(value[1], Schema):
87-
value, schema = value
90+
if isinstance(value, Schema):
91+
schema = value
92+
value = schema.default
8893
else:
89-
schema = Schema(**schema_from_config)
94+
schema = Schema(value, **schema_from_config)
9095
schema.alias = schema.alias or schema_from_config.get('alias')
9196
required = value == Required
9297
return cls(
@@ -142,7 +147,7 @@ def schema(self, by_alias=True):
142147
if self._schema.choice_names:
143148
s['choices'] = [(v.value, self._schema.choice_names[v.value]) for v in self.type_.__members__.values()]
144149
else:
145-
s['choices'] = [(v.value, k) for k, v in self.type_.__members__.items()]
150+
s['choices'] = [(v.value, k.title()) for k, v in self.type_.__members__.items()]
146151
s.update(self._schema.extra)
147152
return s
148153

tests/test_schema.py

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from enum import Enum, IntEnum
22

3-
from pydantic import BaseModel
3+
import pytest
4+
5+
from pydantic import BaseModel, Schema, ValidationError
46

57

68
def test_key():
@@ -104,13 +106,57 @@ class Bar(BaseModel):
104106
}
105107

106108

109+
def test_schema_class():
110+
class Model(BaseModel):
111+
foo: int = Schema(4, title='Foo is Great')
112+
bar: str = Schema(..., description='this description of bar')
113+
114+
with pytest.raises(ValidationError):
115+
Model()
116+
117+
m = Model(bar=123)
118+
assert m.dict() == {'foo': 4, 'bar': '123'}
119+
120+
assert Model.schema() == {
121+
'type': 'object',
122+
'title': 'Model',
123+
'properties': {
124+
'foo': {
125+
'type': 'int',
126+
'title': 'Foo is Great',
127+
'required': False,
128+
'default': 4,
129+
},
130+
'bar': {
131+
'type': 'str',
132+
'title': 'Bar',
133+
'required': True,
134+
'description': 'this description of bar',
135+
},
136+
},
137+
}
138+
139+
140+
def test_schema_class_by_alias():
141+
class Model(BaseModel):
142+
foo: int = Schema(4, alias='foofoo')
143+
144+
assert list(Model.schema()['properties'].keys()) == ['foofoo']
145+
assert list(Model.schema(by_alias=False)['properties'].keys()) == ['foo']
146+
147+
107148
def test_choices():
108149
FooEnum = Enum('FooEnum', {'foo': 'f', 'bar': 'b'})
109150
BarEnum = IntEnum('BarEnum', {'foo': 1, 'bar': 2})
110151

152+
class SpamEnum(str, Enum):
153+
foo = 'f'
154+
bar = 'b'
155+
111156
class Model(BaseModel):
112157
foo: FooEnum
113158
bar: BarEnum
159+
spam: SpamEnum = Schema(None, choice_names={'f': 'Sausage', 'b': 'Bacon'})
114160

115161
assert Model.schema() == {
116162
'type': 'object',
@@ -121,17 +167,26 @@ class Model(BaseModel):
121167
'title': 'Foo',
122168
'required': True,
123169
'choices': [
124-
('f', 'foo'),
125-
('b', 'bar'),
170+
('f', 'Foo'),
171+
('b', 'Bar'),
126172
],
127173
},
128174
'bar': {
129175
'type': 'int',
130176
'title': 'Bar',
131177
'required': True,
132178
'choices': [
133-
(1, 'foo'),
134-
(2, 'bar'),
179+
(1, 'Foo'),
180+
(2, 'Bar'),
181+
],
182+
},
183+
'spam': {
184+
'type': 'str',
185+
'title': 'Spam',
186+
'required': False,
187+
'choices': [
188+
('f', 'Sausage'),
189+
('b', 'Bacon'),
135190
],
136191
},
137192
},

0 commit comments

Comments
 (0)
0