Proposed changes to pyflakes's traversal code.
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start by reading the traversal path through Checker.handleNode and Checker.getNodeHandler, then review the proposed handleFields and visitor changes in the issue. Compare the existing traversal behavior with the referenced prototype PRs, especially ordering and _unknown_handler coverage. Done means the refactor preserves traversal behavior, removes the listed mechanisms, and has tests for the changed behavior.
Written by the indexing model from the issue text.
Description
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
omitand_fieldsOrderkwargs and the_FieldsOrderclass. - 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:
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
- Eliminate
iter_child_nodes. - Add a
handleFieldsmethod:
def handleFields(self, node, fields):
"""Visit only the *given* children of node in the given order."""
for field in fields:
child = getattr(node, field, None)
if isinstance(child, list):
for item in child:
if isinstance(item, ast.AST):
self.handleNode(item, node)
elif isinstance(child, ast.AST):
self.handleNode(child, node)
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:
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'))
- 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
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 207
- Avg merge
- 4m
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PyCQA/pyflakes
-
F542 is not a crimeOpen
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
-
Use of await in a function that is not async should be shown as a syntax error by flake8May be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
F821 with Annotated import aliasMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
separate_packages splits imports from the same explicitly configured packagePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day