Skip to content

Commit 246ff3b

Browse files
csabellaterryjreedy
authored andcommitted
bpo-6691: Pyclbr now reports nested classes and functions. (#2503)
Original patch by Guilherme Polo. Revisions by Cheryl Sabella.
1 parent 6969eaf commit 246ff3b

File tree

3 files changed

+338
-186
lines changed

3 files changed

+338
-186
lines changed

Doc/library/pyclbr.rst

Lines changed: 92 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,107 +10,144 @@
1010

1111
--------------
1212

13-
The :mod:`pyclbr` module can be used to determine some limited information
14-
about the classes, methods and top-level functions defined in a module. The
15-
information provided is sufficient to implement a traditional three-pane
16-
class browser. The information is extracted from the source code rather
17-
than by importing the module, so this module is safe to use with untrusted
18-
code. This restriction makes it impossible to use this module with modules
19-
not implemented in Python, including all standard and optional extension
13+
The :mod:`pyclbr` module provides limited information about the
14+
functions, classes, and methods defined in a python-coded module. The
15+
information is sufficient to implement a module browser. The
16+
information is extracted from the python source code rather than by
17+
importing the module, so this module is safe to use with untrusted code.
18+
This restriction makes it impossible to use this module with modules not
19+
implemented in Python, including all standard and optional extension
2020
modules.
2121

2222

2323
.. function:: readmodule(module, path=None)
2424

25-
Read a module and return a dictionary mapping class names to class
26-
descriptor objects. The parameter *module* should be the name of a
27-
module as a string; it may be the name of a module within a package. The
28-
*path* parameter should be a sequence, and is used to augment the value
29-
of ``sys.path``, which is used to locate module source code.
25+
Return a dictionary mapping module-level class names to class
26+
descriptors. If possible, descriptors for imported base classes are
27+
included. Parameter *module* is a string with the name of the module
28+
to read; it may be the name of a module within a package. If given,
29+
*path* is a sequence of directory paths prepended to ``sys.path``,
30+
which is used to locate the module source code.
3031

3132

3233
.. function:: readmodule_ex(module, path=None)
3334

34-
Like :func:`readmodule`, but the returned dictionary, in addition to
35-
mapping class names to class descriptor objects, also maps top-level
36-
function names to function descriptor objects. Moreover, if the module
37-
being read is a package, the key ``'__path__'`` in the returned
38-
dictionary has as its value a list which contains the package search
39-
path.
35+
Return a dictionary-based tree containing a function or class
36+
descriptors for each function and class defined in the module with a
37+
``def`` or ``class`` statement. The returned dictionary maps
38+
module-level function and class names to their descriptors. Nested
39+
objects are entered into the children dictionary of their parent. As
40+
with readmodule, *module* names the module to be read and *path* is
41+
prepended to sys.path. If the module being read is a package, the
42+
returned dictionary has a key ``'__path__'`` whose value is a list
43+
containing the package search path.
4044

45+
.. versionadded:: 3.7
46+
Descriptors for nested definitions. They are accessed through the
47+
new children attibute. Each has a new parent attribute.
4148

42-
.. _pyclbr-class-objects:
49+
The descriptors returned by these functions are instances of
50+
Function and Class classes. Users are not expected to create instances
51+
of these classes.
4352

44-
Class Objects
45-
-------------
4653

47-
The :class:`Class` objects used as values in the dictionary returned by
48-
:func:`readmodule` and :func:`readmodule_ex` provide the following data
49-
attributes:
54+
.. _pyclbr-function-objects:
5055

56+
Function Objects
57+
----------------
58+
Class :class:`Function` instances describe functions defined by def
59+
statements. They have the following attributes:
5160

52-
.. attribute:: Class.module
5361

54-
The name of the module defining the class described by the class descriptor.
62+
.. attribute:: Function.file
5563

64+
Name of the file in which the function is defined.
5665

57-
.. attribute:: Class.name
5866

59-
The name of the class.
67+
.. attribute:: Function.module
6068

69+
The name of the module defining the function described.
6170

62-
.. attribute:: Class.super
6371

64-
A list of :class:`Class` objects which describe the immediate base
65-
classes of the class being described. Classes which are named as
66-
superclasses but which are not discoverable by :func:`readmodule` are
67-
listed as a string with the class name instead of as :class:`Class`
68-
objects.
72+
.. attribute:: Function.name
73+
74+
The name of the function.
6975

7076

71-
.. attribute:: Class.methods
77+
.. attribute:: Function.lineno
78+
79+
The line number in the file where the definition starts.
80+
81+
82+
.. attribute:: Function.parent
83+
84+
For top-level functions, None. For nested functions, the parent.
85+
86+
.. versionadded:: 3.7
87+
88+
89+
.. attribute:: Function.children
90+
91+
A dictionary mapping names to descriptors for nested functions and
92+
classes.
7293

73-
A dictionary mapping method names to line numbers.
94+
.. versionadded:: 3.7
95+
96+
97+
.. _pyclbr-class-objects:
98+
99+
Class Objects
100+
-------------
101+
Class :class:`Class` instances describe classes defined by class
102+
statements. They have the same attributes as Functions and two more.
74103

75104

76105
.. attribute:: Class.file
77106

78-
Name of the file containing the ``class`` statement defining the class.
107+
Name of the file in which the class is defined.
79108

80109

81-
.. attribute:: Class.lineno
110+
.. attribute:: Class.module
82111

83-
The line number of the ``class`` statement within the file named by
84-
:attr:`~Class.file`.
112+
The name of the module defining the class described.
85113

86114

87-
.. _pyclbr-function-objects:
115+
.. attribute:: Class.name
88116

89-
Function Objects
90-
----------------
117+
The name of the class.
91118

92-
The :class:`Function` objects used as values in the dictionary returned by
93-
:func:`readmodule_ex` provide the following attributes:
94119

120+
.. attribute:: Class.lineno
95121

96-
.. attribute:: Function.module
122+
The line number in the file where the definition starts.
97123

98-
The name of the module defining the function described by the function
99-
descriptor.
100124

125+
.. attribute:: Class.parent
101126

102-
.. attribute:: Function.name
127+
For top-level classes, None. For nested classes, the parent.
103128

104-
The name of the function.
129+
.. versionadded:: 3.7
105130

106131

107-
.. attribute:: Function.file
132+
.. attribute:: Class.children
108133

109-
Name of the file containing the ``def`` statement defining the function.
134+
A dictionary mapping names to descriptors for nested functions and
135+
classes.
110136

137+
.. versionadded:: 3.7
111138

112-
.. attribute:: Function.lineno
113139

114-
The line number of the ``def`` statement within the file named by
115-
:attr:`~Function.file`.
140+
.. attribute:: Class.super
141+
142+
A list of :class:`Class` objects which describe the immediate base
143+
classes of the class being described. Classes which are named as
144+
superclasses but which are not discoverable by :func:`readmodule_ex`
145+
are listed as a string with the class name instead of as
146+
:class:`Class` objects.
147+
148+
149+
.. attribute:: Class.methods
116150

151+
A dictionary mapping method names to line numbers. This can be
152+
derived from the newer children dictionary, but remains for
153+
back-compatibility.

0 commit comments

Comments
 (0)