FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-99350: Fix symtable.SymbolTable.is_nested() documentation by iamsharduld · Pull Request #151901 · python/cpython · GitHub

/ cpython Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion Doc/library/symtable.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL