Skip to content

"extra fields not permitted" error when using Extra.forbid on dataclass property that's a dict or list of another dataclass #4343

@DetachHead

Description

@DetachHead

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

when instanciating a dataclass where a property is a list or dict of another dataclass, it causes this error:

extra fields not permitted (type=value_error.extra)

Example Code

from pydantic import Extra
from pydantic.dataclasses import dataclass


class ExtraPropertiesForbidden:
    extra = Extra.forbid


@dataclass(config=ExtraPropertiesForbidden)
class Bar:
    ...


@dataclass(config=ExtraPropertiesForbidden)
class Foo:
    a: list[Bar]


a = Foo(a=[Bar()])  # error: extra fields not permitted (type=value_error.extra)

Python, Pydantic & OS Version

this issue seems to have been introduced after the 1.9.1 release. this occurred after installing the latest commit from master:

pip uninstall pydantic
pip install git+https://github.com/samuelcolvin/pydantic.git@a69136d2096e327ed48954a298c775dceb1551d1
pydantic version: 1.9.0
            pydantic compiled: False
                 install path: C:\Users\chungus\AppData\Local\pypoetry\Cache\virtualenvs\project-e01-Hfn1-py3.10\Lib\site-packages\pydantic
               python version: 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
                     platform: Windows-10-10.0.19042-SP0
     optional deps. installed: ['typing-extensions']

Affected Components

Metadata

Metadata

Assignees

Labels

bug V1Bug related to Pydantic V1.X

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions