Skip to content

Commit

Permalink
fix "extra fields not permitted" error when dataclass with `Extra.for…
Browse files Browse the repository at this point in the history
…bid` is validated multiple times
  • Loading branch information
DetachHead committed Aug 9, 2022
1 parent f6c74a5 commit 06f7de3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/4343-detachhead.md
@@ -0,0 +1 @@
fix "extra fields not permitted" error when dataclass with `Extra.forbid` is validated multiple times
4 changes: 4 additions & 0 deletions pydantic/dataclasses.py
Expand Up @@ -385,6 +385,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!
Expand Down
24 changes: 24 additions & 0 deletions tests/test_dataclasses.py
Expand Up @@ -1360,3 +1360,27 @@ class A:
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)]

0 comments on commit 06f7de3

Please sign in to comment.