Skip to content

Commit 3c0afd9

Browse files
committed
gh-99350: Fix symtable.SymbolTable.is_nested() documentation
The docstring and library reference described is_nested() as returning True for 'a nested class or function', which wrongly implies any nesting. The underlying flag is only set when the block is nested within a function, so a method of a top-level class is not nested. Reword both descriptions and add a characterizing assertion to test_nested.
1 parent 30aeeb3 commit 3c0afd9

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

Doc/library/symtable.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ Examining Symbol Tables
125125

126126
.. method:: is_nested()
127127

128-
Return ``True`` if the block is a nested class or function.
128+
Return ``True`` if the block is a function or class nested, directly or
129+
indirectly, within a function. A block defined at module level, or
130+
nested only within classes (and not within any function), is not
131+
considered nested.
129132

130133
.. method:: has_children()
131134

Lib/symtable.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ def is_optimized(self):
126126
return bool(self._table.type == _symtable.TYPE_FUNCTION)
127127

128128
def is_nested(self):
129-
"""Return *True* if the block is a nested class
130-
or function."""
129+
"""Return *True* if the block is a function or class nested,
130+
directly or indirectly, within a function.
131+
132+
A block defined at module level, or nested only within classes
133+
(and not within any function), is not considered nested.
134+
"""
131135
return bool(self._table.nested)
132136

133137
def has_children(self):

Lib/test/test_symtable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ def test_optimized(self):
234234
def test_nested(self):
235235
self.assertFalse(self.top.is_nested())
236236
self.assertFalse(self.Mine.is_nested())
237+
# gh-99350: a method of a top-level class is not nested, because
238+
# is_nested() reflects being enclosed by a function, not merely
239+
# being nested inside some other block.
240+
self.assertFalse(self.a_method.is_nested())
237241
self.assertFalse(self.spam.is_nested())
238242
self.assertTrue(self.internal.is_nested())
239243

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Clarify that :meth:`symtable.SymbolTable.is_nested` returns ``True`` only when
2+
the block is nested (directly or indirectly) within a function, and not when it
3+
is nested merely within another block such as a class.

0 commit comments

Comments
 (0)