-
Notifications
You must be signed in to change notification settings - Fork 182
Flatten handlers #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jayvdb
wants to merge
1
commit into
PyCQA:main
Choose a base branch
from
jayvdb:flatten
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Flatten handlers #70
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -820,9 +820,42 @@ def on_conditional_branch(): | |
self.report(messages.UndefinedName, node, name) | ||
|
||
def handleChildren(self, tree, omit=None): | ||
"""Handle all children recursively, but may be flattened.""" | ||
for node in iter_child_nodes(tree, omit=omit): | ||
self.handleNode(node, tree) | ||
|
||
def handleChildrenNested(self, node): | ||
"""Handle all children recursively.""" | ||
self.handleChildren(node) | ||
|
||
def _iter_flattened(self, tree, omit, _fields_order=_FieldsOrder()): | ||
""" | ||
Yield child nodes of *node* and their children, with handler. | ||
|
||
The value yielded is a tuple of the node, its parent and its handler. | ||
The handler may be False to indicate that no handler and no recursion | ||
is required as the node is part of a flattened list. | ||
""" | ||
_may_flatten = (self.handleChildren, | ||
self.handleChildrenFlattened) | ||
|
||
nodes = [(tree, None)] | ||
for node, parent in nodes: | ||
# Skip the root of the tree, which has parent None | ||
handler = self.getNodeHandler(node.__class__) if parent else False | ||
if handler and handler not in _may_flatten: | ||
yield node, parent, handler | ||
else: | ||
nodes[:] += ((child, node) | ||
for child in iter_child_nodes(node, | ||
omit, | ||
_fields_order)) | ||
|
||
def handleChildrenFlattened(self, tree, omit=None): | ||
"""Handle all children recursively as a flat list where possible.""" | ||
for node, parent, handler in self._iter_flattened(tree, omit=omit): | ||
self.handleNode(node, parent, handler) | ||
|
||
def isLiteralTupleUnpacking(self, node): | ||
if isinstance(node, ast.Assign): | ||
for child in node.targets + [node.value]: | ||
|
@@ -852,7 +885,12 @@ def getDocstring(self, node): | |
|
||
return (node.s, doctest_lineno) | ||
|
||
def handleNode(self, node, parent): | ||
def handleNode(self, node, parent, handler=None): | ||
""" | ||
Handle a single node, invoking its handler, which may recurse. | ||
|
||
If handler is None, the default handler is used. | ||
""" | ||
if node is None: | ||
return | ||
if self.offset and getattr(node, 'lineno', None) is not None: | ||
|
@@ -863,11 +901,18 @@ def handleNode(self, node, parent): | |
if self.futuresAllowed and not (isinstance(node, ast.ImportFrom) or | ||
self.isDocstring(node)): | ||
self.futuresAllowed = False | ||
self.nodeDepth += 1 | ||
node.depth = self.nodeDepth | ||
|
||
node.depth = self.nodeDepth + 1 | ||
node.parent = parent | ||
try: | ||
|
||
if handler is False: | ||
return | ||
|
||
if not handler: | ||
handler = self.getNodeHandler(node.__class__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've moved this outside of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will need to do better than suspect. :P |
||
|
||
self.nodeDepth += 1 | ||
try: | ||
handler(node) | ||
finally: | ||
self.nodeDepth -= 1 | ||
|
@@ -964,21 +1009,22 @@ def ignore(self, node): | |
pass | ||
|
||
# "stmt" type nodes | ||
DELETE = PRINT = FOR = ASYNCFOR = WHILE = IF = WITH = WITHITEM = \ | ||
ASYNCWITH = ASYNCWITHITEM = TRYFINALLY = EXEC = \ | ||
EXPR = ASSIGN = handleChildren | ||
DELETE = PRINT = EXEC = EXPR = handleChildrenFlattened | ||
ASSIGN = TRYFINALLY = handleChildren | ||
FOR = ASYNCFOR = WHILE = IF = WITH = ASYNCWITH = handleChildren | ||
WITHITEM = ASYNCWITHITEM = handleChildrenFlattened | ||
|
||
PASS = ignore | ||
|
||
# "expr" type nodes | ||
BOOLOP = BINOP = UNARYOP = IFEXP = SET = \ | ||
COMPARE = CALL = REPR = ATTRIBUTE = SUBSCRIPT = \ | ||
STARRED = NAMECONSTANT = handleChildren | ||
STARRED = NAMECONSTANT = handleChildrenFlattened | ||
|
||
NUM = STR = BYTES = ELLIPSIS = ignore | ||
|
||
# "slice" type nodes | ||
SLICE = EXTSLICE = INDEX = handleChildren | ||
SLICE = EXTSLICE = INDEX = handleChildrenFlattened | ||
|
||
# expression contexts are node instances too, though being constants | ||
LOAD = STORE = DEL = AUGLOAD = AUGSTORE = PARAM = ignore | ||
|
@@ -1003,7 +1049,8 @@ def RAISE(self, node): | |
self.report(messages.RaiseNotImplemented, node) | ||
|
||
# additional node types | ||
COMPREHENSION = KEYWORD = FORMATTEDVALUE = JOINEDSTR = handleChildren | ||
COMPREHENSION = handleChildren | ||
KEYWORD = FORMATTEDVALUE = JOINEDSTR = handleChildrenFlattened | ||
|
||
def DICT(self, node): | ||
# Complain if there are duplicate keys with different values | ||
|
@@ -1083,7 +1130,7 @@ def GENERATOREXP(self, node): | |
self.handleChildren(node) | ||
self.popScope() | ||
|
||
LISTCOMP = handleChildren if PY2 else GENERATOREXP | ||
LISTCOMP = handleChildrenNested if PY2 else GENERATOREXP | ||
|
||
DICTCOMP = SETCOMP = GENERATOREXP | ||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the
[:]
do here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List resizing. https://stackoverflow.com/a/10155987