-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Failed to inspect __new__
and __init_subclass__
methods generated by warnings.deprecated
#119605
Comments
Lines 2574 to 2588 in 2268289
Reproduce snippet: # test.py
from warnings import deprecated # or from typing_extensions import deprecated
class Foo:
def __init__(self, x, y):
self.x = x
self.y = y
@deprecated("test")
class Bar:
def __init__(self, x, y):
self.x = x
self.y = y In [1]: import inspect
In [2]: from test import Foo, Bar
In [3]: inspect.signature(Foo)
Out[3]: <Signature (x, y)>
In [4]: inspect.signature(Bar)
Out[4]: <Signature (*args, **kwargs)> |
I got a workaround to patch the + def update_signature(original_func):
+ # Ensure that the signature of the decorated callable matches the original one
+
+ def wrapper(func):
+ import inspect
+
+ try:
+ original_signature = inspect.signature(original_func)
+ except ValueError:
+ pass
+ else:
+ signature = inspect.signature(func)
+ if signature != original_signature:
+ try:
+ func.__text_signature__ = str(original_signature)
+ except (AttributeError, TypeError):
+ pass
+
+ return func
+
+ return wrapper
if isinstance(arg, type):
import functools
from types import MethodType
original_new = arg.__new__
+ @update_signature(original_new)
@functools.wraps(original_new)
def __new__(cls, *args, **kwargs):
if cls is arg:
warn(msg, category=category, stacklevel=stacklevel + 1)
if original_new is not object.__new__:
return original_new(cls, *args, **kwargs)
# Mirrors a similar check in object.__new__.
elif cls.__init__ is object.__init__ and (args or kwargs):
raise TypeError(f"{cls.__name__}() takes no arguments")
else:
return original_new(cls)
arg.__new__ = staticmethod(__new__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug report
Bug description:
PEP 702 – Marking deprecations using the type system introduces a new API
warnings.deprecated
for deprecation.While decorating a class object, it will update the
__new__
method:cpython/Lib/warnings.py
Lines 589 to 603 in 2268289
and the
__init_subclass__
method:cpython/Lib/warnings.py
Lines 605 to 625 in 2268289
For a class (
cls
) that does not implement the__new__
and__init_subclass__
methods, thewarnings.deprecated
decorator will generate these two methods (based onobject.__new__
andtype.__init_subclass__
) and assign them tocls.__dict__
.However, if users want to inspect the methods in
cls.__dict__
, theinspect
module fails to get the correct definition ofcls.__new__
. It is defined inwarnings.py
rather thanobject.__new__
.Expected to have
inspect.getsourcelines(Bar.__new__)
to be source code in/.../lib/python3.13/warnings.py
.CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
follow_wrapped
for__init__
and__new__
when getting class signature withinspect.signature
#132055The text was updated successfully, but these errors were encountered: