Skip to content
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

gh-120541: Improve the "less" prompt in pydoc #120543

Merged
merged 5 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,14 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0,
"""Display text documentation, given an object or a path to an object."""
if output is None:
try:
what = thing if isinstance(thing, str) else type(thing).__name__
if isinstance(thing, str):
what = thing
else:
what = getattr(thing, '__qualname__', None)
if not isinstance(what, str):
what = getattr(thing, '__name__', None)
if not isinstance(what, str):
what = type(thing).__name__ + ' object'
pager(render_doc(thing, title, forceload), f'Help on {what!s}')
except ImportError as exc:
if is_cli:
Expand Down
62 changes: 49 additions & 13 deletions Lib/test/test_pydoc/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from test.support.script_helper import (assert_python_ok,
assert_python_failure, spawn_python)
from test.support import threading_helper
from test.support import (reap_children, captured_output, captured_stdout,
from test.support import (reap_children, captured_stdout,
captured_stderr, is_emscripten, is_wasi,
requires_docstrings, MISSING_C_DOCSTRINGS)
from test.support.os_helper import (TESTFN, rmtree, unlink)
Expand Down Expand Up @@ -680,9 +680,8 @@ def test_help_output_redirect(self, pager_mock):
help_header = textwrap.dedent(help_header)
expected_help_pattern = help_header + expected_text_pattern

with captured_output('stdout') as output, \
captured_output('stderr') as err, \
StringIO() as buf:
with captured_stdout() as output, captured_stderr() as err:
buf = StringIO()
helper = pydoc.Helper(output=buf)
helper.help(module)
result = buf.getvalue().strip()
Expand All @@ -706,9 +705,8 @@ def test_help_output_redirect_various_requests(self, pager_mock):

def run_pydoc_for_request(request, expected_text_part):
"""Helper function to run pydoc with its output redirected"""
with captured_output('stdout') as output, \
captured_output('stderr') as err, \
StringIO() as buf:
with captured_stdout() as output, captured_stderr() as err:
buf = StringIO()
helper = pydoc.Helper(output=buf)
helper.help(request)
result = buf.getvalue().strip()
Expand Down Expand Up @@ -742,6 +740,45 @@ def run_pydoc_for_request(request, expected_text_part):
run_pydoc_for_request(pydoc.Helper.help, 'Help on function help in module pydoc:')
# test for pydoc.Helper() instance skipped because it is always meant to be interactive

@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
'trace function introduces __locals__ unexpectedly')
@requires_docstrings
def test_help_output_pager(self):
def run_pydoc_pager(request, what, expected_first_line):
with (captured_stdout() as output,
captured_stderr() as err,
unittest.mock.patch('pydoc.pager') as pager_mock,
self.subTest(repr(request))):
helper = pydoc.Helper()
helper.help(request)
self.assertEqual('', err.getvalue())
self.assertEqual('\n', output.getvalue())
pager_mock.assert_called_once()
result = clean_text(pager_mock.call_args.args[0])
self.assertEqual(result.splitlines()[0], expected_first_line)
self.assertEqual(pager_mock.call_args.args[1], f'Help on {what}')

run_pydoc_pager('%', 'EXPRESSIONS', 'Operator precedence')
run_pydoc_pager('True', 'bool object', 'Help on bool object:')
run_pydoc_pager(True, 'bool object', 'Help on bool object:')
run_pydoc_pager('assert', 'assert', 'The "assert" statement')
run_pydoc_pager('TYPES', 'TYPES', 'The standard type hierarchy')
run_pydoc_pager('pydoc.Helper.help', 'pydoc.Helper.help',
'Help on function help in pydoc.Helper:')
run_pydoc_pager(pydoc.Helper.help, 'Helper.help',
'Help on function help in module pydoc:')
run_pydoc_pager('str', 'str', 'Help on class str in module builtins:')
run_pydoc_pager(str, 'str', 'Help on class str in module builtins:')
run_pydoc_pager('str.upper', 'str.upper', 'Help on method_descriptor in str:')
run_pydoc_pager(str.upper, 'str.upper', 'Help on method_descriptor:')
run_pydoc_pager(str.__add__, 'str.__add__', 'Help on wrapper_descriptor:')
run_pydoc_pager(int.numerator, 'int.numerator',
'Help on getset descriptor builtins.int.numerator:')
run_pydoc_pager(list[int], 'list',
'Help on GenericAlias in module builtins:')
run_pydoc_pager('sys', 'sys', 'Help on built-in module sys:')
run_pydoc_pager(sys, 'sys', 'Help on built-in module sys:')

def test_showtopic(self):
with captured_stdout() as showtopic_io:
helper = pydoc.Helper()
Expand Down Expand Up @@ -775,9 +812,8 @@ def test_showtopic_output_redirect(self, pager_mock):
# Helper.showtopic should be redirected
self.maxDiff = None

with captured_output('stdout') as output, \
captured_output('stderr') as err, \
StringIO() as buf:
with captured_stdout() as output, captured_stderr() as err:
buf = StringIO()
helper = pydoc.Helper(output=buf)
helper.showtopic('with')
result = buf.getvalue().strip()
Expand All @@ -790,23 +826,23 @@ def test_showtopic_output_redirect(self, pager_mock):
def test_lambda_with_return_annotation(self):
func = lambda a, b, c: 1
func.__annotations__ = {"return": int}
with captured_output('stdout') as help_io:
with captured_stdout() as help_io:
pydoc.help(func)
helptext = help_io.getvalue()
self.assertIn("lambda (a, b, c) -> int", helptext)

def test_lambda_without_return_annotation(self):
func = lambda a, b, c: 1
func.__annotations__ = {"a": int, "b": int, "c": int}
with captured_output('stdout') as help_io:
with captured_stdout() as help_io:
pydoc.help(func)
helptext = help_io.getvalue()
self.assertIn("lambda (a: int, b: int, c: int)", helptext)

def test_lambda_with_return_and_params_annotation(self):
func = lambda a, b, c: 1
func.__annotations__ = {"a": int, "b": int, "c": int, "return": int}
with captured_output('stdout') as help_io:
with captured_stdout() as help_io:
pydoc.help(func)
helptext = help_io.getvalue()
self.assertIn("lambda (a: int, b: int, c: int) -> int", helptext)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the prompt in the "less" pager when :func:`help` is called with
non-string argument.
Loading