Skip to content

Commit 7e64414

Browse files
bpo-40257: Improve help for the typing module (GH-19546)
* Show docstring for special forms. * Show docstring for special generic aliases. * Show documentation for __origin__ for generic aliases.
1 parent c606624 commit 7e64414

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

Doc/whatsnew/3.9.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ Other Language Changes
142142
grammar was much more restrictive. See :pep:`614` for details.
143143
(Contributed by Brandt Bucher in :issue:`39702`.)
144144

145+
* Improved help for the :mod:`typing` module. Docstrings are now shown for
146+
all special forms and special generic aliases (like ``Union`` and ``List``).
147+
Using :func:`help` with generic alias like ``List[int]`` will show the help
148+
for the correspondent concrete type (``list`` in this case).
149+
(Contributed by Serhiy Storchaka in :issue:`40257`.)
150+
145151

146152
New Modules
147153
===========

Lib/pydoc.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ def docother(self, object, name=None, mod=None, parent=None, maxlen=None, doc=No
14451445
if not doc:
14461446
doc = getdoc(object)
14471447
if doc:
1448-
line += '\n' + self.indent(str(doc))
1448+
line += '\n' + self.indent(str(doc)) + '\n'
14491449
return line
14501450

14511451
class _PlainTextDoc(TextDoc):
@@ -1672,8 +1672,11 @@ def render_doc(thing, title='Python Library Documentation: %s', forceload=0,
16721672
inspect.getdoc(object)):
16731673
# If the passed object is a piece of data or an instance,
16741674
# document its available methods instead of its value.
1675-
object = type(object)
1676-
desc += ' object'
1675+
if hasattr(object, '__origin__'):
1676+
object = object.__origin__
1677+
else:
1678+
object = type(object)
1679+
desc += ' object'
16771680
return title % desc + '\n\n' + renderer.document(object, name)
16781681

16791682
def doc(thing, title='Python Library Documentation: %s', forceload=0,

Lib/test/test_pydoc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,8 @@ class X:
12551255
X.attr.__doc__ = 'Custom descriptor'
12561256
self.assertEqual(self._get_summary_lines(X.attr), """\
12571257
<test.test_pydoc.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>
1258-
Custom descriptor""")
1258+
Custom descriptor
1259+
""")
12591260

12601261
X.attr.__name__ = 'foo'
12611262
self.assertEqual(self._get_summary_lines(X.attr), """\

Lib/typing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ def __init__(self, name, doc):
323323
self._name = name
324324
self._doc = doc
325325

326+
@property
327+
def __doc__(self):
328+
return self._doc
329+
326330
def __eq__(self, other):
327331
if not isinstance(other, _SpecialForm):
328332
return NotImplemented
@@ -672,6 +676,8 @@ def __init__(self, origin, params, *, inst=True, special=False, name=None):
672676
self.__slots__ = None # This is not documented.
673677
if not name:
674678
self.__module__ = origin.__module__
679+
if special:
680+
self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}'
675681

676682
@_tp_cache
677683
def __getitem__(self, params):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improved help for the :mod:`typing` module. Docstrings are now shown for all
2+
special forms and special generic aliases (like ``Union`` and ``List``).
3+
Using ``help()`` with generic alias like ``List[int]`` will show the help
4+
for the correspondent concrete type (``list`` in this case).

0 commit comments

Comments
 (0)