allow None as value in None fields

This commit is contained in:
Samuel Colvin
2017-05-08 12:34:20 +01:00
parent 017d3f03be
commit d6edca642a
2 changed files with 11 additions and 0 deletions
+3
View File
@@ -63,6 +63,9 @@ class Field:
if self.type_ is None:
raise ConfigError(f'unable to infer type for attribute "{self.name}"')
if not self.required and not self.validate_always and self.default is None:
self.allow_none = True
# typing interface is horrible, we have to do some ugly checks
origin = getattr(self.type_, '__origin__', None)
if origin not in (None, Union):
+8
View File
@@ -223,3 +223,11 @@ def test_unable_to_infer():
class InvalidDefinitionModel(BaseModel):
x = None
assert exc_info.value.args[0] == 'unable to infer type for attribute "x"'
def test_not_required():
class Model(BaseModel):
a: float = None
assert Model(a=12.2).a == 12.2
assert Model().a is None
assert Model(a=None).a is None