← 返回首页
bpo-29940: Add follow_wrapped option to help() by eamanu · Pull Request #22390 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
23 changes: 14 additions & 9 deletions Lib/pydoc.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1772,14 +1772,16 @@ def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
return title % desc + '\n\n' + renderer.document(object, name)

def doc(thing, title='Python Library Documentation: %s', forceload=0,
output=None):
output=None, follow_wrapped=True):
"""Display text documentation, given an object or a path to an object."""
try:
if not follow_wrapped:
thing = type(thing)
if output is None:
pager(render_doc(thing, title, forceload))
else:
output.write(render_doc(thing, title, forceload, plaintext))
except (ImportError, ErrorDuringImport) as value:
except (ImportError, ErrorDuringImport, ValueError) as value:
print(value)

def writedoc(thing, forceload=0):
Expand Down Expand Up @@ -1995,9 +1997,9 @@ def __repr__(self):
self.__class__.__qualname__)

_GoInteractive = object()
def __call__(self, request=_GoInteractive):
def __call__(self, request=_GoInteractive, follow_wrapped=True):
if request is not self._GoInteractive:
self.help(request)
self.help(request, follow_wrapped=follow_wrapped)
else:
self.intro()
self.interact()
Expand Down Expand Up @@ -2038,7 +2040,7 @@ def getline(self, prompt):
self.output.flush()
return self.input.readline()

def help(self, request):
def help(self, request, follow_wrapped=True):
if type(request) is type(''):
request = request.strip()
if request == 'keywords': self.listkeywords()
Expand All @@ -2050,13 +2052,16 @@ def help(self, request):
elif request in self.symbols: self.showsymbol(request)
elif request in ['True', 'False', 'None']:
# special case these keywords since they are objects too
doc(eval(request), 'Help on %s:')
doc(eval(request), 'Help on %s:', follow_wrapped=follow_wrapped)
elif request in self.keywords: self.showtopic(request)
elif request in self.topics: self.showtopic(request)
elif request: doc(request, 'Help on %s:', output=self._output)
else: doc(str, 'Help on %s:', output=self._output)
elif request: doc(request, 'Help on %s:', output=self._output,
follow_wrapped=follow_wrapped)
else: doc(str, 'Help on %s:', output=self._output,
follow_wrapped=follow_wrapped)
elif isinstance(request, Helper): self()
else: doc(request, 'Help on %s:', output=self._output)
else: doc(request, 'Help on %s:', output=self._output,
follow_wrapped=follow_wrapped)
self.output.write('\n')

def intro(self):
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/test_pydoc.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,44 @@ def test_builtin_on_metaclasses(self):
# Testing that the subclasses section does not appear
self.assertNotIn('Built-in subclasses', text)

def test_help_with_wrapper(self):
"""Test help on function wrapped.

When running help() on a function that is wrapped,
should be show the docstring of the last object in the chain
with __doc__ method.
"""

import functools
class TestWrapper:
"""This is the docstring of Wrapper"""
def __init__(self, func):
functools.update_wrapper(self, func)
def __call__(self):
pass

@TestWrapper
def test_func1():
"""Test func1"""
pass

buff = StringIO()

helper = pydoc.Helper(output=buff)

helper.help(test_func1)
self.assertIn('Test func1', buff.getvalue().strip())

helper.help(test_func1, follow_wrapped=False)
self.assertIn("This is the docstring of Wrapper", buff.getvalue().strip())

@TestWrapper
def test_func2():
pass

helper.help(test_func2)
self.assertIn("This is the docstring of Wrapper", buff.getvalue().strip())

@unittest.skipIf(sys.flags.optimize >= 2,
'Docstrings are omitted with -O2 and above')
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
Expand Down
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add follow_wrapped to help function to print the docstring of a wrapped
function.
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.