You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have spent many happy hours studying pyflakes and its tree traversal methods. This issue proposes several possible simplifications. It's fine with me if you aren't interested.
Overview
All the simplifications follow from these changes:
Eliminate the omit and _fieldsOrder kwargs and the _FieldsOrder class.
Add/change visitors to specify required traversal order.
Rename the visitors so that the visitor's names are the same as the corresponding Ast nodes.
With these changes, Checker.getNodeHandler becomes:
There is no need for the _nodeHandlers cache because the new code creates no strings.
Only Checker.handleNode calls getNodeHandler so the code above may as well appear in-line.
Other changes
Eliminate iter_child_nodes.
Add a handleFields method:
defhandleFields(self, node, fields):
"""Visit only the *given* children of node in the given order."""forfieldinfields:
child=getattr(node, field, None)
ifisinstance(child, list):
foriteminchild:
ifisinstance(item, ast.AST):
self.handleNode(item, node)
elifisinstance(child, ast.AST):
self.handleNode(child, node)
defvisit(self, node):
self.visitFields(node._fields) # Or duplicate the code above to eliminate the call.
Add or change visitors to specify the required traversal order. Examples:
defarguments(self, node):
# Visit all fields except 'defaults' and 'kw_defaults'.fields= ('posonlyargs', 'args', 'var arg', 'kwonlyargs', 'kwarg')
self.handleFields(node, fields)
defDictComp(self, node):
withself.in_scope(GeneratorScope):
# Order matters.self.handleFields(node, ('generators', 'key', 'value'))
Eliminate Checker._unknown_handler. A new unit test would cover its absence.
Summary
These changes are merely suggestions. You can find the prototype code in the five PRs at ekr-fork-pyflakes. I'll happily make any changes you like in a real PR.
Eliminating kwargs invariably simplifies and generalizes code. For example, it would be straightforward to subclass the new traversal code. The new visitors increase the traversal's speed and make explicit the required traversal order of the visitor's children.
Edward
The text was updated successfully, but these errors were encountered:
I have spent many happy hours studying pyflakes and its tree traversal methods. This issue proposes several possible simplifications. It's fine with me if you aren't interested.
Overview
All the simplifications follow from these changes:
omit
and_fieldsOrder
kwargs and the_FieldsOrder
class.With these changes,
Checker.getNodeHandler
becomes:There is no need for the
_nodeHandlers
cache because the new code creates no strings.Only
Checker.handleNode
callsgetNodeHandler
so the code above may as well appear in-line.Other changes
iter_child_nodes
.handleFields
method:Checker._unknown_handler
. A new unit test would cover its absence.Summary
These changes are merely suggestions. You can find the prototype code in the five PRs at ekr-fork-pyflakes. I'll happily make any changes you like in a real PR.
Eliminating kwargs invariably simplifies and generalizes code. For example, it would be straightforward to subclass the new traversal code. The new visitors increase the traversal's speed and make explicit the required traversal order of the visitor's children.
Edward
The text was updated successfully, but these errors were encountered: