Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions src/flake8/plugins/pyflakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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
19 changes: 19 additions & 0 deletions tests/integration/plugins/test_pyflakes.py
Original file line number Diff line number Diff line change
@@ -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, '', '')
Loading