Skip to content

Commit

Permalink
fix extra in dataclasses with bases
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Nov 5, 2021
1 parent aefe042 commit d9bc078
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pydantic/dataclasses.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Type, TypeVar, Union, overload
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Type, TypeVar, Union, get_type_hints, overload

from .class_validators import gather_all_validators
from .error_wrappers import ValidationError
Expand Down Expand Up @@ -163,7 +163,9 @@ def _pydantic_post_init(self: 'Dataclass', *initvars: Any) -> None:

if getattr(config, 'extra', Extra.forbid) is not Extra.forbid:
def allow_extra_init(self, *args, **kwargs):
self.__original_init__(*args, **{k: v for k, v in kwargs.items() if k in self.__annotations__})
self.__original_init__(*args, **{
k: v for k, v in kwargs.items() if k in get_type_hints(type(self))
})

cls.__original_init__ = cls.__init__
cls.__init__ = allow_extra_init
Expand Down
15 changes: 15 additions & 0 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,18 @@ class Foo:
b: str

Foo(**{"a": "bar", "b": "foo", "c": 1})


def test_allow_extra_subclass():
class C:
extra = Extra.ignore

@pydantic.dataclasses.dataclass(config=C)
class Foo:
a: str

@pydantic.dataclasses.dataclass(config=C)
class Bar(Foo):
b: str

Bar(**{"a": "bar", "b": "foo", "c": 1})

0 comments on commit d9bc078

Please sign in to comment.