mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
fix "extra fields not permitted" error when dataclass with Extra.forbid is validated multiple times (#4354)
Co-authored-by: detachhead <detachhead@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
fix "extra fields not permitted" error when dataclass with `Extra.forbid` is validated multiple times
|
||||
@@ -386,6 +386,10 @@ def create_pydantic_model_from_dataclass(
|
||||
|
||||
|
||||
def _dataclass_validate_values(self: 'Dataclass') -> None:
|
||||
# validation errors can occur if this function is called twice on an already initialised dataclass.
|
||||
# for example if Extra.forbid is enabled, it would consider __pydantic_initialised__ an invalid extra property
|
||||
if getattr(self, '__pydantic_initialised__'):
|
||||
return
|
||||
if getattr(self, '__pydantic_has_field_info_default__', False):
|
||||
# We need to remove `FieldInfo` values since they are not valid as input
|
||||
# It's ok to do that because they are obviously the default values!
|
||||
|
||||
@@ -1371,3 +1371,27 @@ def test_kw_only():
|
||||
A(1, '')
|
||||
|
||||
assert A(b='hi').b == 'hi'
|
||||
|
||||
|
||||
def test_extra_forbid_list_no_error():
|
||||
@pydantic.dataclasses.dataclass(config=dict(extra=Extra.forbid))
|
||||
class Bar:
|
||||
...
|
||||
|
||||
@pydantic.dataclasses.dataclass
|
||||
class Foo:
|
||||
a: List[Bar]
|
||||
|
||||
assert isinstance(Foo(a=[Bar()]).a[0], Bar)
|
||||
|
||||
|
||||
def test_extra_forbid_list_error():
|
||||
@pydantic.dataclasses.dataclass(config=dict(extra=Extra.forbid))
|
||||
class Bar:
|
||||
...
|
||||
|
||||
with pytest.raises(TypeError, match=re.escape("__init__() got an unexpected keyword argument 'a'")):
|
||||
|
||||
@pydantic.dataclasses.dataclass
|
||||
class Foo:
|
||||
a: List[Bar(a=1)]
|
||||
|
||||
Reference in New Issue
Block a user