PyCQA / pyflakes

A simple program which checks Python source files for errors
https://pypi.org/project/pyflakes
MIT License
1.36k stars 179 forks source link

Proposed changes to pyflakes's traversal code. #816

Open edreamleo opened 2 months ago

edreamleo commented 2 months ago

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:

With these changes, Checker.getNodeHandler becomes:

handler = getattr(self, node.__class__.__name__, self.visit)
handler(node)

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

def visit(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:
```python
def arguments(self, node):
    # Visit all fields except 'defaults' and 'kw_defaults'.
    fields = ('posonlyargs', 'args', 'var arg', 'kwonlyargs', 'kwarg')
    self.handleFields(node, fields)

def DictComp(self, node):
    with self.in_scope(GeneratorScope):
        # Order matters.
        self.handleFields(node, ('generators', 'key', 'value'))

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

edreamleo commented 2 months ago

I am going to close this issue for (justified) lack of interest.