Skip to content

Unable to inspect class with cyfunction __init__ due to unwrap and descriptor behavior changed #137317

@yanyongyu

Description

@yanyongyu

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

cpython/Lib/inspect.py

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

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesstdlibPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy