Repository: pydantic/pydantic

Issue:
"extra fields not permitted" error when using `Extra.forbid` on `dataclass` property that's a `dict` or `list` of another `dataclass`
### Initial Checks

- [X] I have searched GitHub for a duplicate issue and I'm sure this is something new
- [X] I have searched Google & StackOverflow for a solution and couldn't find anything
- [X] I have read and followed [the docs](https://pydantic-docs.helpmanual.io) and still think this is a bug
- [X] I am confident that the issue is with pydantic  (not my code, or another library in the ecosystem like [FastAPI](https://fastapi.tiangolo.com) or [mypy](https://mypy.readthedocs.io/en/stable))


### 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

```Python
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
```
```Text
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

- [ ] [Compatibility between releases](https://pydantic-docs.helpmanual.io/changelog/)
- [ ] [Data validation/parsing](https://pydantic-docs.helpmanual.io/usage/models/#basic-model-usage)
- [ ] [Data serialization](https://pydantic-docs.helpmanual.io/usage/exporting_models/) - `.dict()` and `.json()`
- [ ] [JSON Schema](https://pydantic-docs.helpmanual.io/usage/schema/)
- [X] [Dataclasses](https://pydantic-docs.helpmanual.io/usage/dataclasses/)
- [X] [Model Config](https://pydantic-docs.helpmanual.io/usage/model_config/)
- [ ] [Field Types](https://pydantic-docs.helpmanual.io/usage/types/) - adding or changing a particular data type
- [ ] [Function validation decorator](https://pydantic-docs.helpmanual.io/usage/validation_decorator/)
- [ ] [Generic Models](https://pydantic-docs.helpmanual.io/usage/models/#generic-models)
- [ ] [Other Model behaviour](https://pydantic-docs.helpmanual.io/usage/models/) - `construct()`, pickling, private attributes, ORM mode
- [ ] [Settings Management](https://pydantic-docs.helpmanual.io/usage/settings/)
- [ ] [Plugins](https://pydantic-docs.helpmanual.io/) and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.


Discussion:
Very good catch, thank you @DetachHead.

Resolving patch:
diff --git a/pydantic/dataclasses.py b/pydantic/dataclasses.py
--- a/pydantic/dataclasses.py
+++ b/pydantic/dataclasses.py
@@ -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!


Test patch:
diff --git a/tests/test_dataclasses.py b/tests/test_dataclasses.py
--- a/tests/test_dataclasses.py
+++ b/tests/test_dataclasses.py
@@ -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)]