Repository: pydantic/pydantic (Python)

Issue:
Data class with defer_build set to true causes the error AttributeError: type object '...' has no attribute '__pydantic_fields__'. Did you mean: '__pydantic_config__'
### Initial Checks

- [X] I confirm that I'm using Pydantic V2

### Description

Upgraded from 2.9.2 and the below code wll now fail unless I set **defer_build** to false

### Example Code

```Python
import typing

import pydantic


@pydantic.dataclasses.dataclass(kw_only=True, config=pydantic.config.ConfigDict(defer_build=True))
class MyObject:
    msg_id: str
    receipt_handle: str


@pydantic.validate_call
def do_work(input: MyObject|typing.Iterable[MyObject]|list[MyObject]):
    pass
```


### Python, Pydantic & OS Version

```Text
python -c "import pydantic.version; print(pydantic.version.version_info())"
             pydantic version: 2.10.1
        pydantic-core version: 2.27.1
          pydantic-core build: profile=release pgo=false
                 install path: D:\XXX\XXX\XXX\venv\Lib\site-packages\pydantic
               python version: 3.13.0 (tags/v3.13.0:60403a5, Oct  7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)]
                     platform: Windows-11-10.0.22631-SP0
             related packages: typing_extensions-4.12.2
```



Discussion:

Patch:
diff --git a/pydantic/_internal/_dataclasses.py b/pydantic/_internal/_dataclasses.py
index 8b02bfd0522..90f7d76ea56 100644
--- a/pydantic/_internal/_dataclasses.py
+++ b/pydantic/_internal/_dataclasses.py
@@ -125,6 +125,8 @@ def __init__(__dataclass_self__: PydanticDataclass, *args: Any, **kwargs: Any) -
     cls.__init__ = __init__  # type: ignore
     cls.__pydantic_config__ = config_wrapper.config_dict  # type: ignore
 
+    set_dataclass_fields(cls, ns_resolver, config_wrapper=config_wrapper)
+
     if not _force_build and config_wrapper.defer_build:
         set_dataclass_mocks(cls)
         return False
@@ -134,8 +136,6 @@ def __init__(__dataclass_self__: PydanticDataclass, *args: Any, **kwargs: Any) -
             'Support for `__post_init_post_parse__` has been dropped, the method will not be called', DeprecationWarning
         )
 
-    set_dataclass_fields(cls, ns_resolver, config_wrapper=config_wrapper)
-
     typevars_map = get_standard_typevars_map(cls)
     gen_schema = GenerateSchema(
         config_wrapper,
@@ -143,14 +143,15 @@ def __init__(__dataclass_self__: PydanticDataclass, *args: Any, **kwargs: Any) -
         typevars_map=typevars_map,
     )
 
-    # set __signature__ attr only for model class, but not for its instances
+    # set __signature__ attr only for the class, but not for its instances
     # (because instances can define `__call__`, and `inspect.signature` shouldn't
     # use the `__signature__` attribute and instead generate from `__call__`).
     cls.__signature__ = LazyClassAttribute(
         '__signature__',
         partial(
             generate_pydantic_signature,
-            # It's' important that we reference the original_init here
+            # It's important that we reference the `original_init` here,
+            # as it is the one synthesized by the stdlib `dataclass` module:
             init=original_init,
             fields=cls.__pydantic_fields__,  # type: ignore
             populate_by_name=config_wrapper.populate_by_name,


Test patch:
diff --git a/tests/test_dataclasses.py b/tests/test_dataclasses.py
index 91c1f7857bf..7ba743c0f83 100644
--- a/tests/test_dataclasses.py
+++ b/tests/test_dataclasses.py
@@ -3065,3 +3065,12 @@ class Model(BaseModel):
         model_config = ConfigDict(arbitrary_types_allowed=True)
 
         dc: DC
+
+
+def test_deferred_dataclass_fields_available() -> None:
+    # This aligns with deferred Pydantic models:
+    @pydantic.dataclasses.dataclass(config={'defer_build': True})
+    class A:
+        a: int
+
+    assert 'a' in A.__pydantic_fields__  # pyright: ignore[reportAttributeAccessIssue]