-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Open
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixes3.15new features, bugs and security fixesnew features, bugs and security fixesstdlibPython modules in the Lib dirPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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
Lines 1924 to 1926 in fe0e921
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
Metadata
Metadata
Assignees
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixes3.15new features, bugs and security fixesnew features, bugs and security fixesstdlibPython modules in the Lib dirPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error