diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index c82bf8e9..9b4fa6aa 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -67,17 +67,35 @@ } -class FlakesChecker(pyflakes.checker.Checker): +class FlakesChecker: """Subclass the Pyflakes checker to conform with the flake8 API.""" - with_doctest = False + _with_doctest = False + _builtins = None def __init__(self, tree: ast.AST, filename: str) -> None: """Initialize the PyFlakes plugin with an AST tree and filename.""" - super().__init__( - tree, filename=filename, withDoctest=self.with_doctest, + self._checker = pyflakes.checker.Checker( + tree, + filename=filename, + builtins=self._builtins, + withDoctest=self._with_doctest, ) + def run(self) -> Generator[tuple[int, int, str, type[Any]]]: + """Run the plugin.""" + for message in self._checker.messages: + col = getattr(message, "col", 0) + yield ( + message.lineno, + col, + "{} {}".format( + FLAKE8_PYFLAKES_CODES.get(type(message).__name__, "F999"), + message.message % message.message_args, + ), + message.__class__, + ) + @classmethod def add_options(cls, parser: OptionManager) -> None: """Register options for PyFlakes on the Flake8 OptionManager.""" @@ -98,20 +116,5 @@ def add_options(cls, parser: OptionManager) -> None: @classmethod def parse_options(cls, options: argparse.Namespace) -> None: """Parse option values from Flake8's OptionManager.""" - if options.builtins: - cls.builtIns = cls.builtIns.union(options.builtins) - cls.with_doctest = options.doctests - - def run(self) -> Generator[tuple[int, int, str, type[Any]]]: - """Run the plugin.""" - for message in self.messages: - col = getattr(message, "col", 0) - yield ( - message.lineno, - col, - "{} {}".format( - FLAKE8_PYFLAKES_CODES.get(type(message).__name__, "F999"), - message.message % message.message_args, - ), - message.__class__, - ) + cls._builtins = options.builtins + cls._with_doctest = options.doctests diff --git a/tests/integration/plugins/test_pyflakes.py b/tests/integration/plugins/test_pyflakes.py new file mode 100644 index 00000000..5dd6325c --- /dev/null +++ b/tests/integration/plugins/test_pyflakes.py @@ -0,0 +1,19 @@ +from __future__ import annotations + +from flake8.main.cli import main + + +def test_pyflakes_builtins(tmpdir, capsys): + t_py = tmpdir.join('t.py') + t_py.write('print(b)\n') + + expected = "t.py:1:7: F821 undefined name 'b'\n" + with tmpdir.as_cwd(): + ret = main(('t.py',)) + out, err = capsys.readouterr() + assert (ret, out, err) == (1, expected, '') + + with tmpdir.as_cwd(): + ret = main(('t.py', '--builtins', 'b')) + out, err = capsys.readouterr() + assert (ret, out, err) == (0, '', '')