Index: Lib/pydoc.py =================================================================== --- Lib/pydoc.py (revision 86636) +++ Lib/pydoc.py (working copy) @@ -1139,6 +1139,26 @@ push(' ' + makename(base)) push('') + # List the built-in subclasses, if any: + subclasses = [] + for attr, attrval in __builtins__.items(): + if not attrval is object: + try: + if issubclass(attrval, object): + subclasses.append(attrval) + except TypeError: + pass + for i in range(len(subclasses)-1, -1, -1): + if issubclass(subclasses[i], tuple(subclasses[:i]+subclasses[i+1:])): + del subclasses[i] + # This removes any subclass which is a child of some other subclass, + # i.e. keep only immediate child classes of "object", not grandchildren etc. + if subclasses: + push("Built-in subclasses:") + for subclassname in sorted([cls.__name__ for cls in subclasses]): + push(' ' + subclassname) + push('') + # Cute little class to pump out a horizontal rule between sections. class HorizontalRule: def __init__(self): Index: Lib/test/test_pydoc.py =================================================================== --- Lib/test/test_pydoc.py (revision 86636) +++ Lib/test/test_pydoc.py (working copy) @@ -327,7 +327,94 @@ self.assertEqual(stripid(""), "") + 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) + + class TestDescriptions(unittest.TestCase): def test_module(self):