Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Provide feedback

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

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit31d1d72

Browse files
gh-120541: Improve the "less" prompt in pydoc (GH-120543)
When help() is called with non-string argument, use __qualname__ or__name__ if available, otherwise use "{typename} object".
1 parent9e0b11e commit31d1d72

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

‎Lib/pydoc.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,14 @@ def doc(thing, title='Python Library Documentation: %s', forceload=0,
17551755
"""Display text documentation, given an object or a path to an object."""
17561756
ifoutputisNone:
17571757
try:
1758-
what=thingifisinstance(thing,str)elsetype(thing).__name__
1758+
ifisinstance(thing,str):
1759+
what=thing
1760+
else:
1761+
what=getattr(thing,'__qualname__',None)
1762+
ifnotisinstance(what,str):
1763+
what=getattr(thing,'__name__',None)
1764+
ifnotisinstance(what,str):
1765+
what=type(thing).__name__+' object'
17591766
pager(render_doc(thing,title,forceload),f'Help on{what!s}')
17601767
exceptImportErrorasexc:
17611768
ifis_cli:

‎Lib/test/test_pydoc/test_pydoc.py‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
fromtest.support.script_helperimport (assert_python_ok,
3232
assert_python_failure,spawn_python)
3333
fromtest.supportimportthreading_helper
34-
fromtest.supportimport (reap_children,captured_output,captured_stdout,
34+
fromtest.supportimport (reap_children,captured_stdout,
3535
captured_stderr,is_emscripten,is_wasi,
3636
requires_docstrings,MISSING_C_DOCSTRINGS)
3737
fromtest.support.os_helperimport (TESTFN,rmtree,unlink)
@@ -680,9 +680,8 @@ def test_help_output_redirect(self, pager_mock):
680680
help_header=textwrap.dedent(help_header)
681681
expected_help_pattern=help_header+expected_text_pattern
682682

683-
withcaptured_output('stdout')asoutput, \
684-
captured_output('stderr')aserr, \
685-
StringIO()asbuf:
683+
withcaptured_stdout()asoutput,captured_stderr()aserr:
684+
buf=StringIO()
686685
helper=pydoc.Helper(output=buf)
687686
helper.help(module)
688687
result=buf.getvalue().strip()
@@ -706,9 +705,8 @@ def test_help_output_redirect_various_requests(self, pager_mock):
706705

707706
defrun_pydoc_for_request(request,expected_text_part):
708707
"""Helper function to run pydoc with its output redirected"""
709-
withcaptured_output('stdout')asoutput, \
710-
captured_output('stderr')aserr, \
711-
StringIO()asbuf:
708+
withcaptured_stdout()asoutput,captured_stderr()aserr:
709+
buf=StringIO()
712710
helper=pydoc.Helper(output=buf)
713711
helper.help(request)
714712
result=buf.getvalue().strip()
@@ -742,6 +740,45 @@ def run_pydoc_for_request(request, expected_text_part):
742740
run_pydoc_for_request(pydoc.Helper.help,'Help on function help in module pydoc:')
743741
# test for pydoc.Helper() instance skipped because it is always meant to be interactive
744742

743+
@unittest.skipIf(hasattr(sys,'gettrace')andsys.gettrace(),
744+
'trace function introduces __locals__ unexpectedly')
745+
@requires_docstrings
746+
deftest_help_output_pager(self):
747+
defrun_pydoc_pager(request,what,expected_first_line):
748+
with (captured_stdout()asoutput,
749+
captured_stderr()aserr,
750+
unittest.mock.patch('pydoc.pager')aspager_mock,
751+
self.subTest(repr(request))):
752+
helper=pydoc.Helper()
753+
helper.help(request)
754+
self.assertEqual('',err.getvalue())
755+
self.assertEqual('\n',output.getvalue())
756+
pager_mock.assert_called_once()
757+
result=clean_text(pager_mock.call_args.args[0])
758+
self.assertEqual(result.splitlines()[0],expected_first_line)
759+
self.assertEqual(pager_mock.call_args.args[1],f'Help on{what}')
760+
761+
run_pydoc_pager('%','EXPRESSIONS','Operator precedence')
762+
run_pydoc_pager('True','bool object','Help on bool object:')
763+
run_pydoc_pager(True,'bool object','Help on bool object:')
764+
run_pydoc_pager('assert','assert','The "assert" statement')
765+
run_pydoc_pager('TYPES','TYPES','The standard type hierarchy')
766+
run_pydoc_pager('pydoc.Helper.help','pydoc.Helper.help',
767+
'Help on function help in pydoc.Helper:')
768+
run_pydoc_pager(pydoc.Helper.help,'Helper.help',
769+
'Help on function help in module pydoc:')
770+
run_pydoc_pager('str','str','Help on class str in module builtins:')
771+
run_pydoc_pager(str,'str','Help on class str in module builtins:')
772+
run_pydoc_pager('str.upper','str.upper','Help on method_descriptor in str:')
773+
run_pydoc_pager(str.upper,'str.upper','Help on method_descriptor:')
774+
run_pydoc_pager(str.__add__,'str.__add__','Help on wrapper_descriptor:')
775+
run_pydoc_pager(int.numerator,'int.numerator',
776+
'Help on getset descriptor builtins.int.numerator:')
777+
run_pydoc_pager(list[int],'list',
778+
'Help on GenericAlias in module builtins:')
779+
run_pydoc_pager('sys','sys','Help on built-in module sys:')
780+
run_pydoc_pager(sys,'sys','Help on built-in module sys:')
781+
745782
deftest_showtopic(self):
746783
withcaptured_stdout()asshowtopic_io:
747784
helper=pydoc.Helper()
@@ -775,9 +812,8 @@ def test_showtopic_output_redirect(self, pager_mock):
775812
# Helper.showtopic should be redirected
776813
self.maxDiff=None
777814

778-
withcaptured_output('stdout')asoutput, \
779-
captured_output('stderr')aserr, \
780-
StringIO()asbuf:
815+
withcaptured_stdout()asoutput,captured_stderr()aserr:
816+
buf=StringIO()
781817
helper=pydoc.Helper(output=buf)
782818
helper.showtopic('with')
783819
result=buf.getvalue().strip()
@@ -790,23 +826,23 @@ def test_showtopic_output_redirect(self, pager_mock):
790826
deftest_lambda_with_return_annotation(self):
791827
func=lambdaa,b,c:1
792828
func.__annotations__= {"return":int}
793-
withcaptured_output('stdout')ashelp_io:
829+
withcaptured_stdout()ashelp_io:
794830
pydoc.help(func)
795831
helptext=help_io.getvalue()
796832
self.assertIn("lambda (a, b, c) -> int",helptext)
797833

798834
deftest_lambda_without_return_annotation(self):
799835
func=lambdaa,b,c:1
800836
func.__annotations__= {"a":int,"b":int,"c":int}
801-
withcaptured_output('stdout')ashelp_io:
837+
withcaptured_stdout()ashelp_io:
802838
pydoc.help(func)
803839
helptext=help_io.getvalue()
804840
self.assertIn("lambda (a: int, b: int, c: int)",helptext)
805841

806842
deftest_lambda_with_return_and_params_annotation(self):
807843
func=lambdaa,b,c:1
808844
func.__annotations__= {"a":int,"b":int,"c":int,"return":int}
809-
withcaptured_output('stdout')ashelp_io:
845+
withcaptured_stdout()ashelp_io:
810846
pydoc.help(func)
811847
helptext=help_io.getvalue()
812848
self.assertIn("lambda (a: int, b: int, c: int) -> int",helptext)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the prompt in the "less" pager when:func:`help` is called with
2+
non-string argument.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp