Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

inspect.unwrap() does not work with types with the__wrapped__ data descriptor #112006

Closed
Labels
3.11only security fixes3.12only security fixes3.13bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error
@serhiy-storchaka

Description

@serhiy-storchaka

Bug report

inspect.unwrap() follows the chain by links__wrapped__ and returns the last item in a chain or the original object if it does not have a__wrapped__ attribute (there is also additional stop predicate and protection against loops, but it is unrelated). It works well in most cases, except with a type that has the__wrapped__ data descriptor.

For example the following code

classW:def__init__(self,x):self._wrapped=x@propertydef__wrapped__(self):returnself._wrappedimportinspectprint(inspect.unwrap(W(chr)))print(inspect.unwrap(W))

prints

<built-in function chr><property object at 0x7f334092dc50>

The former output is correct,W(chr) wrapschr. But the latter is wrong: theW type does not wrap aproperty object.

It is not hypothetical issue.staticmethod andclassmethod have now (bpo-43682/#87848) the__wrapped__ attribute.inspect.signature() usesinspect.unwrap(), and it cannot supportstaticmethod andclassmethod even if they get correct__text_signature__.inspect.getsourcelines() also usesinspect.unwrap() indirectly and can fail with Python classes with the__wrapped__ attribute.

inspect.unwrap() should stop before such attribute. But how to detect such case? There are several ways:

  • Stop iffunc is a class.pickle does it for its special methods, this is why classes are handled separately from instances. But it means thatfunctools.wraps(),staticmethod andclassmethod cannot be used to decorate classes. Although if they are currently used, the result can be weird, because instances will have the same__wrapped__ attribute as a class. I do not know how often wrapped classes are used in the real code, but there is a test for this. It may be the right way at the end, although it can break some questionable code.
  • Stop iffunc.__wrapped__ is a data descriptor. I afraid that it will affect multidecorated properties.
  • Stop iffunc.__wrapped__ is not callable. Do not know what can be consequences.

Maybe there are other ways?

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp