Pydantic dataclasses do not respect the extra-fields in Config
See original GitHub issueQuestion
Pydantic dataclasses do not seem to respect the configuration passed in the @dataclass
-decorator. I am wondering if this is a bug or expected functionality? The documentation indicates that the Config
is respected.
pydantic.dataclasses.dataclass’s arguments are the same as the standard decorator, except one extra keyword argument config which has the same meaning as Config.
In particular I would like to ignore extra fields when initialising the object. The documentation suggests that the default behaviour is Extra.ignore
, but it does not seem to work. Making this explicit does not seem to have an effect either…
from pydantic.dataclasses import dataclass
>>> class C:
... extra = Extra.ignore
...
>>> @dataclass(config=C)
... class Foo:
... a: str
... b: str
...
>>> Foo(**{"a": "bar", "b": "foo", "c": 1})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'c'
Please complete:
- OS: os x
- Python version:
>>> import sys; print(sys.version)
3.7.2 (default, Mar 22 2019, 11:08:57)
[Clang 10.0.0 (clang-1000.10.44.4)]
- Pydantic version:
>>> import pydantic; print(pydantic.VERSION)
1.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Pydantic dataclasses do not respect the extra-fields in Config
Question Pydantic dataclasses do not seem to respect the configuration passed in the @dataclass-decorator. I am wondering if this is a bug ...
Read more >Dataclasses - pydantic
Dataclass Config After v1. 10, pydantic dataclasses support Config. extra but some default behaviour of stdlib dataclasses may prevail. For example, when...
Read more >pydantic [python-library] - Occam :: Details
Data validation and settings management using Python type hinting. Fast and extensible, pydantic plays nicely with your linters/IDE/brain. Define how data ...
Read more >How does one ignore extra arguments passed to a dataclass?
Don't use cls.__annotations__ , use dataclass.fields() so you can introspect their configuration (e.g. ignore init=False fields).
Read more >8 Reasons to Start Using Pydantic to Improve Data Parsing ...
In one of my previous posts, I looked at dataclasses as a way of ... “Pydantic is a library that provides data validation...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
At the very least the docs should mention that the
extra
setting is ignored on dataclasses. EDIT: It’s a massive turn-off for using pydantic dataclasses in the context of FastApiI guess we could do something like this