Skip to content

Commit a95ac77

Browse files
bpo-41224: Document is_annotated() in symtable module and update doc strings (GH-21369)
* Document is_annotate() and update doc strings * Move quotes to the next line. Co-authored-by: Pablo Galindo <[email protected]> Co-authored-by: Pablo Galindo <[email protected]>
1 parent 8f42748 commit a95ac77

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

Doc/library/symtable.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ Examining Symbol Tables
156156

157157
Return ``True`` if the symbol is local to its block.
158158

159+
.. method:: is_annotated()
160+
161+
Return ``True`` if the symbol is annotated.
162+
159163
.. method:: is_free()
160164

161165
Return ``True`` if the symbol is referenced in its block, but not assigned

Lib/symtable.py

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
__all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]
1111

1212
def symtable(code, filename, compile_type):
13+
""" Return the toplevel *SymbolTable* for the source code.
14+
15+
*filename* is the name of the file with the code
16+
and *compile_type* is the *compile()* mode argument.
17+
"""
1318
top = _symtable.symtable(code, filename, compile_type)
1419
return _newSymbolTable(top, filename)
1520

@@ -55,6 +60,11 @@ def __repr__(self):
5560
self._filename)
5661

5762
def get_type(self):
63+
"""Return the type of the symbol table.
64+
65+
The values retuned are 'class', 'module' and
66+
'function'.
67+
"""
5868
if self._table.type == _symtable.TYPE_MODULE:
5969
return "module"
6070
if self._table.type == _symtable.TYPE_FUNCTION:
@@ -65,27 +75,51 @@ def get_type(self):
6575
"unexpected type: {0}".format(self._table.type)
6676

6777
def get_id(self):
78+
"""Return an identifier for the table.
79+
"""
6880
return self._table.id
6981

7082
def get_name(self):
83+
"""Return the table's name.
84+
85+
This corresponds to the name of the class, function
86+
or 'top' if the table is for a class, function or
87+
global respectively.
88+
"""
7189
return self._table.name
7290

7391
def get_lineno(self):
92+
"""Return the number of the first line in the
93+
block for the table.
94+
"""
7495
return self._table.lineno
7596

7697
def is_optimized(self):
98+
"""Return *True* if the locals in the table
99+
are optimizable.
100+
"""
77101
return bool(self._table.type == _symtable.TYPE_FUNCTION)
78102

79103
def is_nested(self):
104+
"""Return *True* if the block is a nested class
105+
or function."""
80106
return bool(self._table.nested)
81107

82108
def has_children(self):
109+
"""Return *True* if the block has nested namespaces.
110+
"""
83111
return bool(self._table.children)
84112

85113
def get_identifiers(self):
114+
"""Return a list of names of symbols in the table.
115+
"""
86116
return self._table.symbols.keys()
87117

88118
def lookup(self, name):
119+
"""Lookup a *name* in the table.
120+
121+
Returns a *Symbol* instance.
122+
"""
89123
sym = self._symbols.get(name)
90124
if sym is None:
91125
flags = self._table.symbols[name]
@@ -94,6 +128,9 @@ def lookup(self, name):
94128
return sym
95129

96130
def get_symbols(self):
131+
"""Return a list of *Symbol* instances for
132+
names in the table.
133+
"""
97134
return [self.lookup(ident) for ident in self.get_identifiers()]
98135

99136
def __check_children(self, name):
@@ -102,6 +139,8 @@ def __check_children(self, name):
102139
if st.name == name]
103140

104141
def get_children(self):
142+
"""Return a list of the nested symbol tables.
143+
"""
105144
return [_newSymbolTable(st, self._filename)
106145
for st in self._table.children]
107146

@@ -120,30 +159,40 @@ def __idents_matching(self, test_func):
120159
if test_func(self._table.symbols[ident]))
121160

122161
def get_parameters(self):
162+
"""Return a tuple of parameters to the function.
163+
"""
123164
if self.__params is None:
124165
self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
125166
return self.__params
126167

127168
def get_locals(self):
169+
"""Return a tuple of locals in the function.
170+
"""
128171
if self.__locals is None:
129172
locs = (LOCAL, CELL)
130173
test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
131174
self.__locals = self.__idents_matching(test)
132175
return self.__locals
133176

134177
def get_globals(self):
178+
"""Return a tuple of globals in the function.
179+
"""
135180
if self.__globals is None:
136181
glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
137182
test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
138183
self.__globals = self.__idents_matching(test)
139184
return self.__globals
140185

141186
def get_nonlocals(self):
187+
"""Return a tuple of nonlocals in the function.
188+
"""
142189
if self.__nonlocals is None:
143190
self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
144191
return self.__nonlocals
145192

146193
def get_frees(self):
194+
"""Return a tuple of free variables in the function.
195+
"""
147196
if self.__frees is None:
148197
is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
149198
self.__frees = self.__idents_matching(is_free)
@@ -155,6 +204,8 @@ class Class(SymbolTable):
155204
__methods = None
156205

157206
def get_methods(self):
207+
"""Return a tuple of methods declared in the class.
208+
"""
158209
if self.__methods is None:
159210
d = {}
160211
for st in self._table.children:
@@ -175,40 +226,63 @@ def __repr__(self):
175226
return "<symbol {0!r}>".format(self.__name)
176227

177228
def get_name(self):
229+
"""Return a name of a symbol.
230+
"""
178231
return self.__name
179232

180233
def is_referenced(self):
234+
"""Return *True* if the symbol is used in
235+
its block.
236+
"""
181237
return bool(self.__flags & _symtable.USE)
182238

183239
def is_parameter(self):
240+
"""Return *True* if the symbol is a parameter.
241+
"""
184242
return bool(self.__flags & DEF_PARAM)
185243

186244
def is_global(self):
245+
"""Return *True* if the sysmbol is global.
246+
"""
187247
return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT))
188248

189249
def is_nonlocal(self):
250+
"""Return *True* if the symbol is nonlocal."""
190251
return bool(self.__flags & DEF_NONLOCAL)
191252

192253
def is_declared_global(self):
254+
"""Return *True* if the symbol is declared global
255+
with a global statement."""
193256
return bool(self.__scope == GLOBAL_EXPLICIT)
194257

195258
def is_local(self):
259+
"""Return *True* if the symbol is local.
260+
"""
196261
return bool(self.__scope in (LOCAL, CELL))
197262

198263
def is_annotated(self):
264+
"""Return *True* if the symbol is annotated.
265+
"""
199266
return bool(self.__flags & DEF_ANNOT)
200267

201268
def is_free(self):
269+
"""Return *True* if a referenced symbol is
270+
not assigned to.
271+
"""
202272
return bool(self.__scope == FREE)
203273

204274
def is_imported(self):
275+
"""Return *True* if the symbol is created from
276+
an import statement.
277+
"""
205278
return bool(self.__flags & DEF_IMPORT)
206279

207280
def is_assigned(self):
281+
"""Return *True* if a symbol is assigned to."""
208282
return bool(self.__flags & DEF_LOCAL)
209283

210284
def is_namespace(self):
211-
"""Returns true if name binding introduces new namespace.
285+
"""Returns *True* if name binding introduces new namespace.
212286
213287
If the name is used as the target of a function or class
214288
statement, this will be true.
@@ -225,7 +299,7 @@ def get_namespaces(self):
225299
return self.__namespaces
226300

227301
def get_namespace(self):
228-
"""Returns the single namespace bound to this name.
302+
"""Return the single namespace bound to this name.
229303
230304
Raises ValueError if the name is bound to multiple namespaces.
231305
"""

0 commit comments

Comments
 (0)