Skip to content

bpo-40257: Improve help for the typing module #19546

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

Merged
merged 4 commits into from
Apr 18, 2020
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
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ Other Language Changes
grammar was much more restrictive. See :pep:`614` for details.
(Contributed by Brandt Bucher in :issue:`39702`.)

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


New Modules
===========
Expand Down
9 changes: 6 additions & 3 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ def docother(self, object, name=None, mod=None, parent=None, maxlen=None, doc=No
if not doc:
doc = getdoc(object)
if doc:
line += '\n' + self.indent(str(doc))
line += '\n' + self.indent(str(doc)) + '\n'
return line

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

def doc(thing, title='Python Library Documentation: %s', forceload=0,
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,8 @@ class X:
X.attr.__doc__ = 'Custom descriptor'
self.assertEqual(self._get_summary_lines(X.attr), """\
<test.test_pydoc.TestDescriptions.test_custom_non_data_descriptor.<locals>.Descr object>
Custom descriptor""")
Custom descriptor
""")

X.attr.__name__ = 'foo'
self.assertEqual(self._get_summary_lines(X.attr), """\
Expand Down
6 changes: 6 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ def __init__(self, name, doc):
self._name = name
self._doc = doc

@property
def __doc__(self):
return self._doc

def __eq__(self, other):
if not isinstance(other, _SpecialForm):
return NotImplemented
Expand Down Expand Up @@ -672,6 +676,8 @@ def __init__(self, origin, params, *, inst=True, special=False, name=None):
self.__slots__ = None # This is not documented.
if not name:
self.__module__ = origin.__module__
if special:
self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}'

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