Proposed changes to pyflakes's traversal code.

オープン
#816 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
リファクタリング
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
python
領域
tooling

調査の方向性

まず Checker.handleNode と Checker.getNodeHandler を通るトラバーサルパスを読み、次に issue で提案されている handleFields と visitor の変更を確認します。既存のトラバーサルの動作を、参照されているプロトタイプ PR と比較し、特に順序と _unknown_handler のカバレッジを確認します。完了の条件は、リファクタリングによってトラバーサルの動作が維持され、列挙されたメカニズムが削除され、変更された動作に対するテストがあることです。

索引モデルが issue の本文から書いたものです。

説明

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:

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 handleFields method:
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

主要言語
Python
スター
1.5k
フォーク
205
平均マージ
1日 2時間
マージ済み PR(30日)
5

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

PyCQA/pyflakes のほかの issue

PyCQA/pyflakes の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。