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
5 changes: 4 additions & 1 deletion Doc/library/symtable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ Examining Symbol Tables

.. method:: is_nested()

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

.. method:: has_children()

Expand Down
8 changes: 6 additions & 2 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ def is_optimized(self):
return bool(self._table.type == _symtable.TYPE_FUNCTION)

def is_nested(self):
"""Return *True* if the block is a nested class
or function."""
"""Return *True* if the block is a function or class nested,
directly or indirectly, within a function.

A block defined at module level, or nested only within classes
(and not within any function), is not considered nested.
"""
return bool(self._table.nested)

def has_children(self):
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ def test_optimized(self):
def test_nested(self):
self.assertFalse(self.top.is_nested())
self.assertFalse(self.Mine.is_nested())
# gh-99350: a method of a top-level class is not nested, because
# is_nested() reflects being enclosed by a function, not merely
# being nested inside some other block.
self.assertFalse(self.a_method.is_nested())
self.assertFalse(self.spam.is_nested())
self.assertTrue(self.internal.is_nested())

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Clarify that :meth:`symtable.SymbolTable.is_nested` returns ``True`` only when
the block is nested (directly or indirectly) within a function, and not when it
is nested merely within another block such as a class.
Loading