-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inspect.unwrap() does not work with types with the __wrapped__
data descriptor
#112006
Comments
…data descriptor This also fixes inspect.Signature.from_callable() for builtins classmethod() and staticmethod().
__wrapper__
data descriptor__wrapped__
data descriptor
After more consideration I think that option 1 is the right one. A class should never be a wrapper with the |
… descriptor (GH-115540) This also fixes inspect.Signature.from_callable() for builtins classmethod() and staticmethod().
…a data descriptor (pythonGH-115540) This also fixes inspect.Signature.from_callable() for builtins classmethod() and staticmethod(). (cherry picked from commit 68c79d2) Co-authored-by: Serhiy Storchaka <[email protected]>
…a data descriptor (pythonGH-115540) This also fixes inspect.Signature.from_callable() for builtins classmethod() and staticmethod(). (cherry picked from commit 68c79d2) Co-authored-by: Serhiy Storchaka <[email protected]>
… a data descriptor (GH-115540) (GH-115966) (cherry picked from commit 68c79d2) Co-authored-by: Serhiy Storchaka <[email protected]>
… a data descriptor (GH-115540) (GH-115965) (cherry picked from commit 68c79d2) Co-authored-by: Serhiy Storchaka <[email protected]>
…a data descriptor (pythonGH-115540) This also fixes inspect.Signature.from_callable() for builtins classmethod() and staticmethod().
…a data descriptor (pythonGH-115540) This also fixes inspect.Signature.from_callable() for builtins classmethod() and staticmethod().
…a data descriptor (pythonGH-115540) This also fixes inspect.Signature.from_callable() for builtins classmethod() and staticmethod().
Python upstream recently fixed the behavior of inspect with wrappers: python/cpython#112006 . The assertion here relies on the broken behavior, we only get None if `inspect(Wrapped)` fails and raises `ValueError`. Now it works, we actually get the correct answer, 1. This changes it so we assert the correct thing depending on the Python version (the fix was backported to 3.11.9 and 3.12.3, so the check has to be a bit complicated). Signed-off-by: Adam Williamson <[email protected]>
Python upstream recently fixed the behavior of inspect with wrappers: python/cpython#112006 . The assertion here relies on the broken behavior, we only get None if `inspect(Wrapped)` fails and raises `ValueError`. Now it works, we actually get the correct answer, 1. This changes it so we assert the correct thing depending on the Python version (the fix was backported to 3.11.9 and 3.12.3, so the check has to be a bit complicated). Signed-off-by: Adam Williamson <[email protected]>
Python upstream recently fixed the behavior of inspect with wrappers: python/cpython#112006 . The assertion here relies on the broken behavior, we only get None if `inspect(Wrapped)` fails and raises `ValueError`. Now it works, we actually get the correct answer, 1. This changes it so we assert the correct thing depending on the Python version (the fix was backported to 3.11.9 and 3.12.3, so the check has to be a bit complicated). Signed-off-by: Adam Williamson <[email protected]>
Python upstream recently fixed the behavior of inspect with wrappers: python/cpython#112006 . The assertion here relies on the broken behavior, we only get None if `inspect(Wrapped)` fails and raises `ValueError`. Now it works, we actually get the correct answer, 1. This changes it so we assert the correct thing depending on the Python version (the fix was backported to 3.11.9 and 3.12.3, so the check has to be a bit complicated).
I see it's referenced above (specifically with:
but just having found this issue, I'm left a bit confused what the suggested fix is, as this change does break what seems to me to be the simple use of from functools import wraps
import inspect
class Foo:
pass
@wraps(Foo, updated=())
class Bar:
pass
print(inspect.unwrap(Bar) is Foo) on py3.10 returns True and on later versions returns False. Other than ignoring |
…a data descriptor (pythonGH-115540) This also fixes inspect.Signature.from_callable() for builtins classmethod() and staticmethod().
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
prints
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:func
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.func.__wrapped__
is a data descriptor. I afraid that it will affect multidecorated properties.func.__wrapped__
is not callable. Do not know what can be consequences.Maybe there are other ways?
Linked PRs
The text was updated successfully, but these errors were encountered: