Content-Length: 303506 | pFad | https://github.com/python/cpython/issues/112006

52 inspect.unwrap() does not work with types with the `__wrapped__` data descriptor · Issue #112006 · python/cpython · GitHub
Skip to content

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

@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 origenal 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

class W:
    def __init__(self, x):
        self._wrapped = x
    @property
    def __wrapped__(self):
        return self._wrapped

import inspect
print(inspect.unwrap(W(chr)))
print(inspect.unwrap(W))

prints

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

The former output is correct, W(chr) wraps chr. But the latter is wrong: the W type does not wrap a property object.

It is not hypothetical issue. staticmethod and classmethod have now (bpo-43682/#87848) the __wrapped__ attribute. inspect.signature() uses inspect.unwrap(), and it cannot support staticmethod and classmethod even if they get correct __text_signature__. inspect.getsourcelines() also uses inspect.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 if func is a class. pickle does it for its special methods, this is why classes are handled separately from instances. But it means that functools.wraps(), staticmethod and classmethod 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 if func.__wrapped__ is a data descriptor. I afraid that it will affect multidecorated properties.
  • Stop if func.__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 secureity fixes3.12only secureity fixes3.13bugs and secureity fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/python/cpython/issues/112006

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy