diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 4bd164b8a9a82b..c842c2c6f262dd 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -1,4 +1,4 @@ -from contextlib import contextmanager +from contextlib import contextmanager, redirect_stdout import linecache import os import importlib @@ -1821,6 +1821,15 @@ async def coro(self): self.assertFalse(inspect.iscoroutinefunction(Cls.sync)) self.assertTrue(inspect.iscoroutinefunction(Cls.coro)) + def test_deprecated_class(self): + with self.assertWarns(DeprecationWarning) as cm: + f = StringIO() + with redirect_stdout(f): + import test.test_warnings.data.deprecated_class as test_module + help(test_module) + self.assertIn(f"Help on module {test_module.__name__}", f.getvalue()) + self.assertEqual(str(cm.warning), "Test") + def setUpModule(): py_warnings.onceregistry.clear() c_warnings.onceregistry.clear() diff --git a/Lib/test/test_warnings/data/deprecated_class.py b/Lib/test/test_warnings/data/deprecated_class.py new file mode 100644 index 00000000000000..026c5ece369d51 --- /dev/null +++ b/Lib/test/test_warnings/data/deprecated_class.py @@ -0,0 +1,9 @@ +from warnings import deprecated + +@deprecated("Test") +class A: + def __init_subclass__(self, **kwargs): + pass + +class B(A): + pass diff --git a/Lib/warnings.py b/Lib/warnings.py index f20b01372dd7a4..9e74c25f853bfb 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -611,6 +611,7 @@ def __new__(cls, *args, **kwargs): else: return original_new(cls) + __new__.__module__ = arg.__module__ arg.__new__ = staticmethod(__new__) original_init_subclass = arg.__init_subclass__ @@ -637,6 +638,7 @@ def __init_subclass__(*args, **kwargs): arg.__deprecated__ = __new__.__deprecated__ = msg __init_subclass__.__deprecated__ = msg + __init_subclass__.__module__ = arg.__module__ return arg elif callable(arg): import functools diff --git a/Misc/NEWS.d/next/Library/2025-01-17-13-38-12.gh-issue-128772.8yEVh2.rst b/Misc/NEWS.d/next/Library/2025-01-17-13-38-12.gh-issue-128772.8yEVh2.rst new file mode 100644 index 00000000000000..14c2fdf2097d7b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-01-17-13-38-12.gh-issue-128772.8yEVh2.rst @@ -0,0 +1,3 @@ +Make sure the :attr:`~method.__module__` attribute is set on :meth:`~object.__new__` +and :meth:`~object.__init_subclass__` when the :func:`warnings.deprecated` decorator +is applied on classes.