question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pydantic dataclasses do not respect the extra-fields in Config

See original GitHub issue

Question

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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

9reactions
ilya-kolpakovcommented, Nov 17, 2021

To me this sounds unnecessarily complicated. If you want extra, best to use BaseModel.

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 FastApi

6reactions
PrettyWoodcommented, Dec 14, 2020

I guess we could do something like this

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found