Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-128772: Fix - warnings.deprecated doesn't work well with pydoc #128781

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

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
20f1fb4
store __module__ into a variable and restore after __new__ method cre…
srinivasreddy Jan 13, 2025
75c1663
Merge branch 'main' into gh_128772
srinivasreddy Jan 15, 2025
9d0e70f
Preserve __module__
srinivasreddy Jan 15, 2025
86924f2
Reintroduce space
srinivasreddy Jan 15, 2025
a7b4e83
Merge branch 'main' into gh_128772
srinivasreddy Jan 16, 2025
aab2f5b
Merge branch 'main' into gh_128772
srinivasreddy Jan 16, 2025
382299e
Add test case for help(....) function
srinivasreddy Jan 16, 2025
5b879a3
Fix indentation
srinivasreddy Jan 16, 2025
5595a8e
Fix test failure
srinivasreddy Jan 16, 2025
4b3db9e
Update test cases
srinivasreddy Jan 16, 2025
35731f3
Update tests
srinivasreddy Jan 16, 2025
eb5cb3b
order imports
srinivasreddy Jan 16, 2025
7be5d8a
Improve test cases
srinivasreddy Jan 16, 2025
4752d38
Improve test case
srinivasreddy Jan 16, 2025
8b54982
Revert the code since it is in wrong place
srinivasreddy Jan 17, 2025
3c2c9d3
Move the test case to here from Lib/idlelib/idle_test/test_warning.py
srinivasreddy Jan 17, 2025
d48801e
Add blurb
srinivasreddy Jan 17, 2025
08228dc
Update description
srinivasreddy Jan 17, 2025
09d4fa1
Merge branch 'main' into gh_128772
srinivasreddy Jan 17, 2025
57e01d8
Update Misc/NEWS.d/next/Library/2025-01-17-13-38-12.gh-issue-128772.8…
srinivasreddy Jan 21, 2025
2c0b399
Address review comments
srinivasreddy Jan 21, 2025
0eaefef
Revert the change
srinivasreddy Jan 21, 2025
3edb9b1
Remove creating an instance B()
srinivasreddy Jan 21, 2025
96bab8d
Address review comments. Move the deprecated class to a separate data…
srinivasreddy Jan 21, 2025
84175fc
Fix failure
srinivasreddy Jan 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from contextlib import contextmanager
from contextlib import contextmanager, redirect_stdout
import linecache
import os
import importlib
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_warnings/data/deprecated_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from warnings import deprecated

@deprecated("Test")
class A:
def __init_subclass__(self, **kwargs):
pass

class B(A):
pass
2 changes: 2 additions & 0 deletions Lib/warnings.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be that the existing __module__ is overridden on __new__ and __init_subclass__ here, if it already exists.

Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading