Description
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:
Lines 589 to 603 in 2268289
and the __init_subclass__
method:
Lines 605 to 625 in 2268289
For a class (cls
) that does not implement the __new__
and __init_subclass__
methods, the warnings.deprecated
decorator will generate these two methods (based on object.__new__
and type.__init_subclass__
) and assign them to cls.__dict__
.
However, if users want to inspect the methods in cls.__dict__
, the inspect
module fails to get the correct definition of cls.__new__
. It is defined in warnings.py
rather than object.__new__
.
# test.py
from warnings import deprecated # or from typing_extensions import deprecated
class Foo:
def __new__(cls):
return super().__new__(cls)
@deprecated("test")
class Bar:
pass
In [1]: import inspect
In [2]: from test import Foo, Bar
In [3]: inspect.getsourcelines(Foo.__new__)
Out[3]: ([' def __new__(cls):\n', ' return super().__new__(cls)\n'], 7)
In [4]: inspect.getsourcelines(Bar.__new__)
TypeError: module, class, method, function, traceback, frame, or code object was expected, got builtin_function_or_method
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