yanyongyu · GitHub

Bug report

Bug description:

Background:

Pydantic v1 modified builtin dataclass's __init__ method (wraps). When inspect on the modified dataclass, the class's signature will contain a self parameter.

Reproduce code with pydantic v1 installed:

from dataclasses import dataclass
import inspect
from pydantic import BaseConfig
from pydantic.dataclasses import _add_pydantic_validation_attributes
@dataclass
class A:
    x: int
print(inspect.signature(A).parameters)  # x
_add_pydantic_validation_attributes(A, BaseConfig, False, "")
print(inspect.signature(A).parameters)  # self, x
print(A.__init__, hasattr(A.__init__, "__wrapped__"))  # cyfunction A.__init__, True
meth = inspect._descriptor_get(A.__init__, A)
print(meth, hasattr(A.__init__, "__wrapped__"))  # bound method A.__init__, True
meth = inspect.unwrap(A.__init__, stop=lambda m: hasattr(m, "__signature__"))
print(meth)  # function A.__init__, this cause the signature error

This error is because of the unwrap and descriptor get behavior at

meth = _descriptor_get(meth, cls)
if follow_wrapper_chains:
meth = unwrap(meth, stop=lambda m: hasattr(m, "__signature__"))

The cyfunction __init__ descriptor return a bound method dataclass.__init__ and then unwrap it back to a function dataclass.__init__.

Related to #132055

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Read the original on github.com ↗