Skip to content
Open
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
11 changes: 11 additions & 0 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,17 @@ def makename(c, m=object.__module__):
push(' ' + makename(base))
push('')

# List the built-in subclasses, if any:
subclasses = [
cls.__name__ for cls in object.__subclasses__()
if cls.__module__ == 'builtins'
]
if subclasses:
push("Built-in subclasses:")
for subclassname in sorted(subclasses):
push(' ' + subclassname)
push('')

# Cute little class to pump out a horizontal rule between sections.
class HorizontalRule:
def __init__(self):
Expand Down
87 changes: 87 additions & 0 deletions Lib/test/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,93 @@ def test_stripid(self):
self.assertEqual(stripid("<type 'exceptions.Exception'>"),
"<type 'exceptions.Exception'>")

def test_builtin_with_child(self):
"""Tests help on builtin object which have only child classes.

When running help() on a builtin class which has child classes, it
should contain a "Built-in subclasses" section. For example:

>>> help(ArithmeticError)
Help on class ArithmeticError in module builtins:

class ArithmeticError(Exception)
| Base class for arithmetic errors.
|
...
|
| Built-in subclasses:
| FloatingPointError
| OverflowError
| ZeroDivisionError
"""
doc = pydoc.TextDoc()
text = doc.docclass(ArithmeticError)
snip = (" | Built-in subclasses:\n"
" | FloatingPointError\n"
" | OverflowError\n"
" | ZeroDivisionError")
self.assertIn(snip, text)

def test_builtin_with_grandchild(self):
"""Tests help on builtin classes which have grandchild classes.

When running help() on a builtin class which has child classes, it
should contain a "Built-in subclasses" section. However, if it also has
grandchildren, these should not show up on the subclasses section.
For example:

>>> help(Exception)
Help on class Exception in module builtins:

class Exception(BaseException)
| Common base class for all non-exit exceptions.
|
...
|
| Built-in subclasses:
| ArithmeticError
| AssertionError
| AttributeError
...
"""
doc = pydoc.TextDoc()
text = doc.docclass(Exception)
snip = (" | Built-in subclasses:\n"
" | ArithmeticError\n"
" | AssertionError\n"
" | AttributeError")
self.assertIn(snip, text)
# Testing that the grandchild ZeroDivisionError does not show up
self.assertNotIn('ZeroDivisionError', text)

def test_builtin_no_child(self):
"""Tests help on builtin object which have no child classes.

When running help() on a builtin class which has no child classes, it
should not contain any "Built-in subclasses" section. For example:

>>> help(ZeroDivisionError)

Help on class ZeroDivisionError in module builtins:

class ZeroDivisionError(ArithmeticError)
| Second argument to a division or modulo operation was zero.
|
| Method resolution order:
| ZeroDivisionError
| ArithmeticError
| Exception
| BaseException
| object
|
| Methods defined here:
...
"""
doc = pydoc.TextDoc()
text = doc.docclass(ZeroDivisionError)
# Testing that the subclasses section does not appear
self.assertNotIn('Built-in subclasses', text)

@unittest.skipIf(sys.flags.optimize >= 2,
'Docstrings are omitted with -O2 and above')
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
help() now displays exception's builtin subclasses.