mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
allow None as value in None fields
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user