From 84d56a8c25106b5e0a41cdf63b5de261f8da5c99 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 2 Aug 2022 01:01:58 +0000 Subject: [PATCH 001/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.37.2 → v2.37.3](https://github.com/asottile/pyupgrade/compare/v2.37.2...v2.37.3) - [github.com/PyCQA/flake8: 4.0.1 → 5.0.3](https://github.com/PyCQA/flake8/compare/4.0.1...5.0.3) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 016f736..07a863e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,7 +13,7 @@ repos: - id: reorder-python-imports args: [--application-directories, '.:src', --py36-plus] - repo: https://github.com/asottile/pyupgrade - rev: v2.37.2 + rev: v2.37.3 hooks: - id: pyupgrade args: [--py36-plus] @@ -23,7 +23,7 @@ repos: - id: black args: [--line-length=79] - repo: https://github.com/PyCQA/flake8 - rev: 4.0.1 + rev: 5.0.3 hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy From 7b8b374c9bc1a141ca7cf6670c8cee6708398490 Mon Sep 17 00:00:00 2001 From: Todd Levi Date: Mon, 1 Aug 2022 13:11:02 -0500 Subject: [PATCH 002/131] Clarify entry point naming Clarified what is and is not a valid entry point name for registering plugins. --- .../plugin-development/registering-plugins.rst | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/source/plugin-development/registering-plugins.rst b/docs/source/plugin-development/registering-plugins.rst index b9e87fb..065bdd8 100644 --- a/docs/source/plugin-development/registering-plugins.rst +++ b/docs/source/plugin-development/registering-plugins.rst @@ -112,11 +112,17 @@ look like:: X101 = flake8_example:ExamplePlugin +In the above case, the entry-point name and the error code produced by your +plugin are the same. + If your plugin reports several error codes that all start with ``X10``, then it would look like:: X10 = flake8_example:ExamplePlugin +In this casae as well as the following case, your entry-point name acts as +a prefix to the error codes produced by your plugin. + If all of your plugin's error codes start with ``X1`` then it would look like:: @@ -130,8 +136,12 @@ in the users environment. Selecting an entry point that is already used can cause plugins to be deactivated without warning! **Please Note:** Your entry point does not need to be exactly 4 characters -as of |Flake8| 3.0. *Consider using an entry point with 3 letters followed -by 3 numbers (i.e.* ``ABC123`` *).* +as of |Flake8| 3.0. Single letter entry point prefixes (such as the +'X' in the examples above) have caused issues in the past. As such, +please consider using a 2 or 3 character entry point prefix, +i.e., ``ABC`` is better than ``A`` but ``ABCD`` is invalid. +*A 3 letters entry point prefix followed by 3 numbers (i.e.* ``ABC123`` *) +is currently the longest allowed entry point name.* .. _Entry Points: From e94ee2b5f1801354b940cfe830b9160852915aec Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 3 Aug 2022 18:49:44 -0400 Subject: [PATCH 003/131] require sufficiently new importlib-metadata --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 0978085..5c7b2d3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,7 +42,7 @@ install_requires = mccabe>=0.7.0,<0.8.0 pycodestyle>=2.9.0,<2.10.0 pyflakes>=2.5.0,<2.6.0 - importlib-metadata<4.3;python_version<"3.8" + importlib-metadata>=1.1.0,<4.3;python_version<"3.8" python_requires = >=3.6.1 [options.packages.find] From 6027577d325b0dd8bf1e465ebd29b71b5f0d005b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 3 Aug 2022 19:19:37 -0400 Subject: [PATCH 004/131] Release 5.0.4 --- docs/source/release-notes/5.0.4.rst | 15 +++++++++++++++ docs/source/release-notes/index.rst | 1 + src/flake8/__init__.py | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 docs/source/release-notes/5.0.4.rst diff --git a/docs/source/release-notes/5.0.4.rst b/docs/source/release-notes/5.0.4.rst new file mode 100644 index 0000000..b5c4889 --- /dev/null +++ b/docs/source/release-notes/5.0.4.rst @@ -0,0 +1,15 @@ +5.0.4 -- 2022-08-03 +------------------- + +You can view the `5.0.4 milestone`_ on GitHub for more details. + +Bugs Fixed +~~~~~~~~~~ + +- Set a lower bound on ``importlib-metadata`` to prevent ``RecursionError`` + (See also :issue:`1650`, :pull:`1653`). + + +.. all links +.. _5.0.4 milestone: + https://github.com/PyCQA/flake8/milestone/46 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index aaeb4c0..d511f30 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -9,6 +9,7 @@ with the newest releases first. ================== .. toctree:: + 5.0.4 5.0.3 5.0.2 5.0.1 diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index 26a8d5b..ea571c9 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ from typing import Type LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "5.0.3" +__version__ = "5.0.4" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From e94fb1094070ccf4b4fa44468cf5dde08bda68c7 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 31 Jul 2022 19:38:54 -0400 Subject: [PATCH 005/131] require python>=3.7 --- .github/workflows/main.yml | 5 +- .pre-commit-config.yaml | 8 +- bin/gen-pycodestyle-plugin | 14 ++-- docs/source/conf.py | 2 + example-plugin/setup.py | 2 + .../src/flake8_example_plugin/__init__.py | 2 + .../flake8_example_plugin/off_by_default.py | 1 + .../flake8_example_plugin/on_by_default.py | 1 + setup.cfg | 3 +- setup.py | 2 + src/flake8/__init__.py | 8 +- src/flake8/__main__.py | 2 + src/flake8/_compat.py | 2 + src/flake8/api/__init__.py | 1 + src/flake8/api/legacy.py | 21 +++-- src/flake8/checker.py | 31 ++++---- src/flake8/defaults.py | 2 + src/flake8/discover_files.py | 2 + src/flake8/exceptions.py | 1 + src/flake8/formatting/__init__.py | 1 + src/flake8/formatting/_windows_color.py | 2 + src/flake8/formatting/base.py | 19 +++-- src/flake8/formatting/default.py | 15 ++-- src/flake8/main/__init__.py | 1 + src/flake8/main/application.py | 33 ++++---- src/flake8/main/cli.py | 5 +- src/flake8/main/debug.py | 5 +- src/flake8/main/options.py | 2 + src/flake8/options/__init__.py | 1 + src/flake8/options/aggregator.py | 5 +- src/flake8/options/config.py | 18 ++--- src/flake8/options/manager.py | 76 +++++++++---------- src/flake8/plugins/__init__.py | 1 + src/flake8/plugins/finder.py | 51 ++++++------- src/flake8/plugins/pycodestyle.py | 7 +- src/flake8/plugins/pyflakes.py | 13 ++-- src/flake8/plugins/reporter.py | 5 +- src/flake8/processor.py | 38 +++++----- src/flake8/statistics.py | 21 +++-- src/flake8/style_guide.py | 62 +++++++-------- src/flake8/utils.py | 32 ++++---- src/flake8/violation.py | 11 ++- tests/__init__.py | 1 + tests/conftest.py | 2 + tests/integration/subdir/aplugin.py | 1 + tests/integration/test_aggregator.py | 2 + tests/integration/test_api_legacy.py | 2 + tests/integration/test_checker.py | 2 + tests/integration/test_main.py | 2 + tests/integration/test_plugins.py | 2 + tests/unit/conftest.py | 2 + tests/unit/plugins/finder_test.py | 2 + tests/unit/plugins/pycodestyle_test.py | 2 + tests/unit/plugins/reporter_test.py | 2 + tests/unit/test_application.py | 2 + tests/unit/test_base_formatter.py | 2 + tests/unit/test_checker_manager.py | 2 + tests/unit/test_debug.py | 2 + tests/unit/test_decision_engine.py | 2 + tests/unit/test_discover_files.py | 2 + tests/unit/test_exceptions.py | 2 + tests/unit/test_file_checker.py | 2 + tests/unit/test_file_processor.py | 2 + tests/unit/test_filenameonly_formatter.py | 2 + tests/unit/test_legacy_api.py | 2 + tests/unit/test_main_options.py | 2 + tests/unit/test_nothing_formatter.py | 2 + tests/unit/test_option.py | 2 + tests/unit/test_option_manager.py | 2 + tests/unit/test_options_config.py | 2 + tests/unit/test_pyflakes_codes.py | 2 + tests/unit/test_statistics.py | 2 + tests/unit/test_style_guide.py | 2 + tests/unit/test_utils.py | 2 + tests/unit/test_violation.py | 2 + tox.ini | 2 +- 76 files changed, 337 insertions(+), 263 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c9b32d7..868b5ea 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,9 +15,6 @@ jobs: - os: ubuntu-latest python: pypy-3.7 toxenv: py - - os: ubuntu-latest - python: 3.6 - toxenv: py - os: ubuntu-latest python: 3.7 toxenv: py @@ -32,7 +29,7 @@ jobs: toxenv: py # windows - os: windows-latest - python: 3.6 + python: 3.7 toxenv: py # misc - os: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 07a863e..faecf55 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,12 +11,16 @@ repos: rev: v3.8.2 hooks: - id: reorder-python-imports - args: [--application-directories, '.:src', --py36-plus] + args: [ + --application-directories, '.:src', + --py37-plus, + --add-import, 'from __future__ import annotations', + ] - repo: https://github.com/asottile/pyupgrade rev: v2.37.3 hooks: - id: pyupgrade - args: [--py36-plus] + args: [--py37-plus] - repo: https://github.com/psf/black rev: 22.6.0 hooks: diff --git a/bin/gen-pycodestyle-plugin b/bin/gen-pycodestyle-plugin index 3540a9a..8bc2efc 100755 --- a/bin/gen-pycodestyle-plugin +++ b/bin/gen-pycodestyle-plugin @@ -1,11 +1,12 @@ #!/usr/bin/env python3 +from __future__ import annotations + import inspect import os.path from typing import Any from typing import Callable from typing import Generator from typing import NamedTuple -from typing import Tuple import pycodestyle @@ -20,7 +21,7 @@ def _too_long(s: str) -> str: class Call(NamedTuple): name: str is_generator: bool - params: Tuple[str, ...] + params: tuple[str, ...] def to_src(self) -> str: params_s = ", ".join(self.params) @@ -35,7 +36,7 @@ class Call(NamedTuple): return "\n".join(lines) @classmethod - def from_func(cls, func: Callable[..., Any]) -> "Call": + def from_func(cls, func: Callable[..., Any]) -> Call: spec = inspect.getfullargspec(func) params = tuple(spec.args) return cls(func.__name__, inspect.isgeneratorfunction(func), params) @@ -55,9 +56,10 @@ def lines() -> Generator[str, None, None]: yield f'"""Generated using ./bin/{os.path.basename(__file__)}."""' yield "# fmt: off" + yield "from __future__ import annotations" + yield "" yield "from typing import Any" yield "from typing import Generator" - yield "from typing import Tuple" yield "" imports = sorted(call.name for call in logical + physical) for name in imports: @@ -69,7 +71,7 @@ def lines() -> Generator[str, None, None]: logical_params = {param for call in logical for param in call.params} for param in sorted(logical_params): yield f" {param}: Any," - yield ") -> Generator[Tuple[int, str], None, None]:" + yield ") -> Generator[tuple[int, str], None, None]:" yield ' """Run pycodestyle logical checks."""' for call in sorted(logical): yield call.to_src() @@ -80,7 +82,7 @@ def lines() -> Generator[str, None, None]: physical_params = {param for call in physical for param in call.params} for param in sorted(physical_params): yield f" {param}: Any," - yield ") -> Generator[Tuple[int, str], None, None]:" + yield ") -> Generator[tuple[int, str], None, None]:" yield ' """Run pycodestyle physical checks."""' for call in sorted(physical): yield call.to_src() diff --git a/docs/source/conf.py b/docs/source/conf.py index e0406c2..a2b4af3 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -14,6 +14,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # sys.path.insert(0, os.path.abspath('.')) +from __future__ import annotations + import flake8 # -- General configuration ------------------------------------------------ diff --git a/example-plugin/setup.py b/example-plugin/setup.py index 70d56fa..c0720bd 100644 --- a/example-plugin/setup.py +++ b/example-plugin/setup.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import setuptools setuptools.setup( diff --git a/example-plugin/src/flake8_example_plugin/__init__.py b/example-plugin/src/flake8_example_plugin/__init__.py index 3f6f163..47851da 100644 --- a/example-plugin/src/flake8_example_plugin/__init__.py +++ b/example-plugin/src/flake8_example_plugin/__init__.py @@ -1,4 +1,6 @@ """Module for an example Flake8 plugin.""" +from __future__ import annotations + from .off_by_default import ExampleTwo from .on_by_default import ExampleOne diff --git a/example-plugin/src/flake8_example_plugin/off_by_default.py b/example-plugin/src/flake8_example_plugin/off_by_default.py index 54737cb..d140ca1 100644 --- a/example-plugin/src/flake8_example_plugin/off_by_default.py +++ b/example-plugin/src/flake8_example_plugin/off_by_default.py @@ -1,4 +1,5 @@ """Our first example plugin.""" +from __future__ import annotations class ExampleTwo: diff --git a/example-plugin/src/flake8_example_plugin/on_by_default.py b/example-plugin/src/flake8_example_plugin/on_by_default.py index a3e5332..d2da126 100644 --- a/example-plugin/src/flake8_example_plugin/on_by_default.py +++ b/example-plugin/src/flake8_example_plugin/on_by_default.py @@ -1,4 +1,5 @@ """Our first example plugin.""" +from __future__ import annotations class ExampleOne: diff --git a/setup.cfg b/setup.cfg index 5c7b2d3..5e20adf 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,7 +20,6 @@ classifiers = Programming Language :: Python Programming Language :: Python :: 3 Programming Language :: Python :: 3 :: Only - Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 @@ -43,7 +42,7 @@ install_requires = pycodestyle>=2.9.0,<2.10.0 pyflakes>=2.5.0,<2.6.0 importlib-metadata>=1.1.0,<4.3;python_version<"3.8" -python_requires = >=3.6.1 +python_requires = >=3.7 [options.packages.find] where = src diff --git a/setup.py b/setup.py index 3822d9e..253a22e 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,6 @@ """Packaging logic for Flake8.""" +from __future__ import annotations + import os import sys diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index ea571c9..43f3bec 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -9,10 +9,10 @@ This module .. autofunction:: flake8.configure_logging """ +from __future__ import annotations + import logging import sys -from typing import Optional -from typing import Type LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) @@ -35,7 +35,7 @@ LOG_FORMAT = ( def configure_logging( verbosity: int, - filename: Optional[str] = None, + filename: str | None = None, logformat: str = LOG_FORMAT, ) -> None: """Configure logging for flake8. @@ -56,7 +56,7 @@ def configure_logging( if not filename or filename in ("stderr", "stdout"): fileobj = getattr(sys, filename or "stderr") - handler_cls: Type[logging.Handler] = logging.StreamHandler + handler_cls: type[logging.Handler] = logging.StreamHandler else: fileobj = filename handler_cls = logging.FileHandler diff --git a/src/flake8/__main__.py b/src/flake8/__main__.py index de240dc..8f7e7c9 100644 --- a/src/flake8/__main__.py +++ b/src/flake8/__main__.py @@ -1,4 +1,6 @@ """Module allowing for ``python -m flake8 ...``.""" +from __future__ import annotations + from flake8.main.cli import main if __name__ == "__main__": diff --git a/src/flake8/_compat.py b/src/flake8/_compat.py index 81da7be..91770bc 100644 --- a/src/flake8/_compat.py +++ b/src/flake8/_compat.py @@ -1,4 +1,6 @@ """Expose backports in a single place.""" +from __future__ import annotations + import sys if sys.version_info >= (3, 8): # pragma: no cover (PY38+) diff --git a/src/flake8/api/__init__.py b/src/flake8/api/__init__.py index c2eefbe..c5f9711 100644 --- a/src/flake8/api/__init__.py +++ b/src/flake8/api/__init__.py @@ -3,3 +3,4 @@ This is the only submodule in Flake8 with a guaranteed stable API. All other submodules are considered internal only and are subject to change. """ +from __future__ import annotations diff --git a/src/flake8/api/legacy.py b/src/flake8/api/legacy.py index 5881aa9..9635756 100644 --- a/src/flake8/api/legacy.py +++ b/src/flake8/api/legacy.py @@ -3,13 +3,12 @@ Previously, users would import :func:`get_style_guide` from ``flake8.engine``. In 3.0 we no longer have an "engine" module but we maintain the API from it. """ +from __future__ import annotations + import argparse import logging import os.path from typing import Any -from typing import List -from typing import Optional -from typing import Type import flake8 from flake8.discover_files import expand_paths @@ -53,7 +52,7 @@ class Report: """Return the total number of errors.""" return self._application.result_count - def get_statistics(self, violation: str) -> List[str]: + def get_statistics(self, violation: str) -> list[str]: """Get the list of occurrences of a violation. :returns: @@ -97,12 +96,12 @@ class StyleGuide: return self._application.options @property - def paths(self) -> List[str]: + def paths(self) -> list[str]: """Return the extra arguments passed as paths.""" assert self._application.options is not None return self._application.options.filenames - def check_files(self, paths: Optional[List[str]] = None) -> Report: + def check_files(self, paths: list[str] | None = None) -> Report: """Run collected checks on the files provided. This will check the files passed in and return a :class:`Report` @@ -119,7 +118,7 @@ class StyleGuide: self._application.report_errors() return Report(self._application) - def excluded(self, filename: str, parent: Optional[str] = None) -> bool: + def excluded(self, filename: str, parent: str | None = None) -> bool: """Determine if a file is excluded. :param filename: @@ -148,7 +147,7 @@ class StyleGuide: def init_report( self, - reporter: Optional[Type[formatter.BaseFormatter]] = None, + reporter: type[formatter.BaseFormatter] | None = None, ) -> None: """Set up a formatter for this run of Flake8.""" if reporter is None: @@ -170,9 +169,9 @@ class StyleGuide: def input_file( self, filename: str, - lines: Optional[Any] = None, - expected: Optional[Any] = None, - line_offset: Optional[Any] = 0, + lines: Any | None = None, + expected: Any | None = None, + line_offset: Any | None = 0, ) -> Report: """Run collected checks on a single file. diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 00cff39..d73349f 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -1,4 +1,6 @@ """Checker Manager and Checker classes.""" +from __future__ import annotations + import argparse import collections import errno @@ -8,7 +10,6 @@ import multiprocessing.pool import signal import tokenize from typing import Any -from typing import Dict from typing import List from typing import Optional from typing import Tuple @@ -71,8 +72,8 @@ class Manager: self.options = style_guide.options self.plugins = plugins self.jobs = self._job_count() - self._all_checkers: List[FileChecker] = [] - self.checkers: List[FileChecker] = [] + self._all_checkers: list[FileChecker] = [] + self.checkers: list[FileChecker] = [] self.statistics = { "files": 0, "logical lines": 0, @@ -152,7 +153,7 @@ class Manager: ) return reported_results_count - def make_checkers(self, paths: Optional[List[str]] = None) -> None: + def make_checkers(self, paths: list[str] | None = None) -> None: """Create checkers for each file.""" if paths is None: paths = self.options.filenames @@ -174,7 +175,7 @@ class Manager: self.checkers = [c for c in self._all_checkers if c.should_process] LOG.info("Checking %d files", len(self.checkers)) - def report(self) -> Tuple[int, int]: + def report(self) -> tuple[int, int]: """Report all of the errors found in the managed file checkers. This iterates over each of the checkers and reports the errors sorted @@ -195,8 +196,8 @@ class Manager: def run_parallel(self) -> None: """Run the checkers in parallel.""" # fmt: off - final_results: Dict[str, List[Tuple[str, int, int, str, Optional[str]]]] = collections.defaultdict(list) # noqa: E501 - final_statistics: Dict[str, Dict[str, int]] = collections.defaultdict(dict) # noqa: E501 + final_results: dict[str, list[tuple[str, int, int, str, str | None]]] = collections.defaultdict(list) # noqa: E501 + final_statistics: dict[str, dict[str, int]] = collections.defaultdict(dict) # noqa: E501 # fmt: on pool = _try_initialize_processpool(self.jobs) @@ -254,7 +255,7 @@ class Manager: LOG.warning("Flake8 was interrupted by the user") raise exceptions.EarlyQuit("Early quit while running checks") - def start(self, paths: Optional[List[str]] = None) -> None: + def start(self, paths: list[str] | None = None) -> None: """Start checking files. :param paths: @@ -301,7 +302,7 @@ class FileChecker: """Provide helpful debugging representation.""" return f"FileChecker for {self.filename}" - def _make_processor(self) -> Optional[processor.FileProcessor]: + def _make_processor(self) -> processor.FileProcessor | None: try: return processor.FileProcessor(self.filename, self.options) except OSError as e: @@ -316,7 +317,7 @@ class FileChecker: def report( self, - error_code: Optional[str], + error_code: str | None, line_number: int, column: int, text: str, @@ -361,7 +362,7 @@ class FileChecker: ) @staticmethod - def _extract_syntax_information(exception: Exception) -> Tuple[int, int]: + def _extract_syntax_information(exception: Exception) -> tuple[int, int]: if ( len(exception.args) > 1 and exception.args[1] @@ -524,7 +525,7 @@ class FileChecker: self.run_physical_checks(file_processor.lines[-1]) self.run_logical_checks() - def run_checks(self) -> Tuple[str, Results, Dict[str, int]]: + def run_checks(self) -> tuple[str, Results, dict[str, int]]: """Run checks against the file.""" assert self.processor is not None try: @@ -592,7 +593,7 @@ def _pool_init() -> None: def _try_initialize_processpool( job_count: int, -) -> Optional[multiprocessing.pool.Pool]: +) -> multiprocessing.pool.Pool | None: """Return a new process pool instance if we are able to create one.""" try: return multiprocessing.Pool(job_count, _pool_init) @@ -617,13 +618,13 @@ def calculate_pool_chunksize(num_checkers: int, num_jobs: int) -> int: return max(num_checkers // (num_jobs * 2), 1) -def _run_checks(checker: FileChecker) -> Tuple[str, Results, Dict[str, int]]: +def _run_checks(checker: FileChecker) -> tuple[str, Results, dict[str, int]]: return checker.run_checks() def find_offset( offset: int, mapping: processor._LogicalMapping -) -> Tuple[int, int]: +) -> tuple[int, int]: """Find the offset tuple for a single offset.""" if isinstance(offset, tuple): return offset diff --git a/src/flake8/defaults.py b/src/flake8/defaults.py index a1c04fc..4ba0048 100644 --- a/src/flake8/defaults.py +++ b/src/flake8/defaults.py @@ -1,4 +1,6 @@ """Constants that define defaults.""" +from __future__ import annotations + import re EXCLUDE = ( diff --git a/src/flake8/discover_files.py b/src/flake8/discover_files.py index 8c21064..b8592c8 100644 --- a/src/flake8/discover_files.py +++ b/src/flake8/discover_files.py @@ -1,4 +1,6 @@ """Functions related to discovering paths.""" +from __future__ import annotations + import logging import os.path from typing import Callable diff --git a/src/flake8/exceptions.py b/src/flake8/exceptions.py index 8e13cd8..18646e7 100644 --- a/src/flake8/exceptions.py +++ b/src/flake8/exceptions.py @@ -1,4 +1,5 @@ """Exception classes for all of Flake8.""" +from __future__ import annotations class Flake8Exception(Exception): diff --git a/src/flake8/formatting/__init__.py b/src/flake8/formatting/__init__.py index bf44801..732d0b6 100644 --- a/src/flake8/formatting/__init__.py +++ b/src/flake8/formatting/__init__.py @@ -1 +1,2 @@ """Submodule containing the default formatters for Flake8.""" +from __future__ import annotations diff --git a/src/flake8/formatting/_windows_color.py b/src/flake8/formatting/_windows_color.py index 1d2c73f..a06fdb9 100644 --- a/src/flake8/formatting/_windows_color.py +++ b/src/flake8/formatting/_windows_color.py @@ -2,6 +2,8 @@ See: https://github.com/pre-commit/pre-commit/blob/cb40e96/pre_commit/color.py """ +from __future__ import annotations + import sys if sys.platform == "win32": # pragma: no cover (windows) diff --git a/src/flake8/formatting/base.py b/src/flake8/formatting/base.py index 78d10e9..d986d65 100644 --- a/src/flake8/formatting/base.py +++ b/src/flake8/formatting/base.py @@ -1,11 +1,10 @@ """The base class and interface for all formatting plugins.""" +from __future__ import annotations + import argparse import os import sys from typing import IO -from typing import List -from typing import Optional -from typing import Tuple from flake8.formatting import _windows_color from flake8.statistics import Statistics @@ -46,7 +45,7 @@ class BaseFormatter: """ self.options = options self.filename = options.output_file - self.output_fd: Optional[IO[str]] = None + self.output_fd: IO[str] | None = None self.newline = "\n" self.color = options.color == "always" or ( options.color == "auto" @@ -84,7 +83,7 @@ class BaseFormatter: os.makedirs(dirname, exist_ok=True) self.output_fd = open(self.filename, "a") - def handle(self, error: "Violation") -> None: + def handle(self, error: Violation) -> None: """Handle an error reported by Flake8. This defaults to calling :meth:`format`, :meth:`show_source`, and @@ -99,7 +98,7 @@ class BaseFormatter: source = self.show_source(error) self.write(line, source) - def format(self, error: "Violation") -> Optional[str]: + def format(self, error: Violation) -> str | None: """Format an error reported by Flake8. This method **must** be implemented by subclasses. @@ -114,7 +113,7 @@ class BaseFormatter: "Subclass of BaseFormatter did not implement" " format." ) - def show_statistics(self, statistics: "Statistics") -> None: + def show_statistics(self, statistics: Statistics) -> None: """Format and print the statistics.""" for error_code in statistics.error_codes(): stats_for_error_code = statistics.statistics_for(error_code) @@ -123,7 +122,7 @@ class BaseFormatter: count += sum(stat.count for stat in stats_for_error_code) self._write(f"{count:<5} {error_code} {statistic.message}") - def show_benchmarks(self, benchmarks: List[Tuple[str, float]]) -> None: + def show_benchmarks(self, benchmarks: list[tuple[str, float]]) -> None: """Format and print the benchmarks.""" # NOTE(sigmavirus24): The format strings are a little confusing, even # to me, so here's a quick explanation: @@ -144,7 +143,7 @@ class BaseFormatter: benchmark = float_format(statistic=statistic, value=value) self._write(benchmark) - def show_source(self, error: "Violation") -> Optional[str]: + def show_source(self, error: Violation) -> str | None: """Show the physical line generating the error. This also adds an indicator for the particular part of the line that @@ -178,7 +177,7 @@ class BaseFormatter: if self.output_fd is None or self.options.tee: sys.stdout.buffer.write(output.encode() + self.newline.encode()) - def write(self, line: Optional[str], source: Optional[str]) -> None: + def write(self, line: str | None, source: str | None) -> None: """Write the line either to the output file or stdout. This handles deciding whether to write to a file or print to standard diff --git a/src/flake8/formatting/default.py b/src/flake8/formatting/default.py index f43dc42..b5d08ff 100644 --- a/src/flake8/formatting/default.py +++ b/src/flake8/formatting/default.py @@ -1,6 +1,5 @@ """Default formatting class for Flake8.""" -from typing import Optional -from typing import Set +from __future__ import annotations from flake8.formatting import base from flake8.violation import Violation @@ -38,7 +37,7 @@ class SimpleFormatter(base.BaseFormatter): error_format: str - def format(self, error: "Violation") -> Optional[str]: + def format(self, error: Violation) -> str | None: """Format and write error out. If an output filename is specified, write formatted errors to that @@ -86,12 +85,12 @@ class FilenameOnly(SimpleFormatter): def after_init(self) -> None: """Initialize our set of filenames.""" - self.filenames_already_printed: Set[str] = set() + self.filenames_already_printed: set[str] = set() - def show_source(self, error: "Violation") -> Optional[str]: + def show_source(self, error: Violation) -> str | None: """Do not include the source code.""" - def format(self, error: "Violation") -> Optional[str]: + def format(self, error: Violation) -> str | None: """Ensure we only print each error once.""" if error.filename not in self.filenames_already_printed: self.filenames_already_printed.add(error.filename) @@ -103,8 +102,8 @@ class FilenameOnly(SimpleFormatter): class Nothing(base.BaseFormatter): """Print absolutely nothing.""" - def format(self, error: "Violation") -> Optional[str]: + def format(self, error: Violation) -> str | None: """Do nothing.""" - def show_source(self, error: "Violation") -> Optional[str]: + def show_source(self, error: Violation) -> str | None: """Do not print the source.""" diff --git a/src/flake8/main/__init__.py b/src/flake8/main/__init__.py index d3aa1de..85bcff4 100644 --- a/src/flake8/main/__init__.py +++ b/src/flake8/main/__init__.py @@ -1 +1,2 @@ """Module containing the logic for the Flake8 entry-points.""" +from __future__ import annotations diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index 15c2477..13ece4e 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -1,15 +1,12 @@ """Module containing the application logic for Flake8.""" +from __future__ import annotations + import argparse import configparser import json import logging import time -from typing import Dict -from typing import List -from typing import Optional from typing import Sequence -from typing import Set -from typing import Tuple import flake8 from flake8 import checker @@ -38,27 +35,27 @@ class Application: #: The timestamp when the Application instance was instantiated. self.start_time = time.time() #: The timestamp when the Application finished reported errors. - self.end_time: Optional[float] = None + self.end_time: float | None = None #: The prelimary argument parser for handling options required for #: obtaining and parsing the configuration file. self.prelim_arg_parser = options.stage1_arg_parser() #: The instance of :class:`flake8.options.manager.OptionManager` used #: to parse and handle the options and arguments passed by the user - self.option_manager: Optional[manager.OptionManager] = None + self.option_manager: manager.OptionManager | None = None - self.plugins: Optional[finder.Plugins] = None + self.plugins: finder.Plugins | None = None #: The user-selected formatter from :attr:`formatting_plugins` - self.formatter: Optional[BaseFormatter] = None + self.formatter: BaseFormatter | None = None #: The :class:`flake8.style_guide.StyleGuideManager` built from the #: user's options - self.guide: Optional[style_guide.StyleGuideManager] = None + self.guide: style_guide.StyleGuideManager | None = None #: The :class:`flake8.checker.Manager` that will handle running all of #: the checks selected by the user. - self.file_checker_manager: Optional[checker.Manager] = None + self.file_checker_manager: checker.Manager | None = None #: The user-supplied options parsed into an instance of #: :class:`argparse.Namespace` - self.options: Optional[argparse.Namespace] = None + self.options: argparse.Namespace | None = None #: The number of errors, warnings, and other messages after running #: flake8 and taking into account ignored errors and lines. self.result_count = 0 @@ -70,11 +67,11 @@ class Application: self.catastrophic_failure = False #: The parsed diff information - self.parsed_diff: Dict[str, Set[int]] = {} + self.parsed_diff: dict[str, set[int]] = {} def parse_preliminary_options( self, argv: Sequence[str] - ) -> Tuple[argparse.Namespace, List[str]]: + ) -> tuple[argparse.Namespace, list[str]]: """Get preliminary options from the CLI, pre-plugin-loading. We need to know the values of a few standard options so that we can @@ -111,8 +108,8 @@ class Application: cfg: configparser.RawConfigParser, cfg_dir: str, *, - enable_extensions: Optional[str], - require_plugins: Optional[str], + enable_extensions: str | None, + require_plugins: str | None, ) -> None: """Find and load the plugins for this application. @@ -143,7 +140,7 @@ class Application: self, cfg: configparser.RawConfigParser, cfg_dir: str, - argv: List[str], + argv: list[str], ) -> None: """Parse configuration files and the CLI options.""" assert self.option_manager is not None @@ -218,7 +215,7 @@ class Application: assert self.options is not None assert self.file_checker_manager is not None if self.options.diff: - files: Optional[List[str]] = sorted(self.parsed_diff) + files: list[str] | None = sorted(self.parsed_diff) if not files: return else: diff --git a/src/flake8/main/cli.py b/src/flake8/main/cli.py index b4bb202..01a67ac 100644 --- a/src/flake8/main/cli.py +++ b/src/flake8/main/cli.py @@ -1,12 +1,13 @@ """Command-line implementation of flake8.""" +from __future__ import annotations + import sys -from typing import Optional from typing import Sequence from flake8.main import application -def main(argv: Optional[Sequence[str]] = None) -> int: +def main(argv: Sequence[str] | None = None) -> int: """Execute the main bit of the application. This handles the creation of an instance of :class:`Application`, runs it, diff --git a/src/flake8/main/debug.py b/src/flake8/main/debug.py index 03671bc..c3a8b0b 100644 --- a/src/flake8/main/debug.py +++ b/src/flake8/main/debug.py @@ -1,12 +1,13 @@ """Module containing the logic for our debugging logic.""" +from __future__ import annotations + import platform from typing import Any -from typing import Dict from flake8.plugins.finder import Plugins -def information(version: str, plugins: Plugins) -> Dict[str, Any]: +def information(version: str, plugins: Plugins) -> dict[str, Any]: """Generate the information to be printed for the bug report.""" versions = sorted( { diff --git a/src/flake8/main/options.py b/src/flake8/main/options.py index 4c9dfb8..d603232 100644 --- a/src/flake8/main/options.py +++ b/src/flake8/main/options.py @@ -1,4 +1,6 @@ """Contains the logic for all of the default options for Flake8.""" +from __future__ import annotations + import argparse from flake8 import defaults diff --git a/src/flake8/options/__init__.py b/src/flake8/options/__init__.py index cc20daa..3578223 100644 --- a/src/flake8/options/__init__.py +++ b/src/flake8/options/__init__.py @@ -10,3 +10,4 @@ to aggregate configuration into one object used by plugins and Flake8. """ +from __future__ import annotations diff --git a/src/flake8/options/aggregator.py b/src/flake8/options/aggregator.py index 580def6..af8e744 100644 --- a/src/flake8/options/aggregator.py +++ b/src/flake8/options/aggregator.py @@ -3,10 +3,11 @@ This holds the logic that uses the collected and merged config files and applies the user-specified command-line configuration on top of it. """ +from __future__ import annotations + import argparse import configparser import logging -from typing import Optional from typing import Sequence from flake8.options import config @@ -19,7 +20,7 @@ def aggregate_options( manager: OptionManager, cfg: configparser.RawConfigParser, cfg_dir: str, - argv: Optional[Sequence[str]], + argv: Sequence[str] | None, ) -> argparse.Namespace: """Aggregate and merge CLI and config file options.""" # Get defaults from the option parser diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index daf8529..e158737 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -1,12 +1,10 @@ """Config handling logic for Flake8.""" +from __future__ import annotations + import configparser import logging import os.path from typing import Any -from typing import Dict -from typing import List -from typing import Optional -from typing import Tuple from flake8 import exceptions from flake8.options.manager import OptionManager @@ -14,13 +12,13 @@ from flake8.options.manager import OptionManager LOG = logging.getLogger(__name__) -def _stat_key(s: str) -> Tuple[int, int]: +def _stat_key(s: str) -> tuple[int, int]: # same as what's used by samefile / samestat st = os.stat(s) return st.st_ino, st.st_dev -def _find_config_file(path: str) -> Optional[str]: +def _find_config_file(path: str) -> str | None: # on windows if the homedir isn't detected this returns back `~` home = os.path.expanduser("~") try: @@ -55,11 +53,11 @@ def _find_config_file(path: str) -> Optional[str]: def load_config( - config: Optional[str], - extra: List[str], + config: str | None, + extra: list[str], *, isolated: bool = False, -) -> Tuple[configparser.RawConfigParser, str]: +) -> tuple[configparser.RawConfigParser, str]: """Load the configuration given the user options. - in ``isolated`` mode, return an empty configuration @@ -97,7 +95,7 @@ def parse_config( option_manager: OptionManager, cfg: configparser.RawConfigParser, cfg_dir: str, -) -> Dict[str, Any]: +) -> dict[str, Any]: """Parse and normalize the typed configuration options.""" if "flake8" not in cfg: return {} diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index ff5a229..e333c9e 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -1,18 +1,14 @@ """Option handling and Option management logic.""" +from __future__ import annotations + import argparse import enum import functools import logging from typing import Any from typing import Callable -from typing import Dict -from typing import List from typing import Mapping -from typing import Optional from typing import Sequence -from typing import Tuple -from typing import Type -from typing import Union from flake8 import utils from flake8.plugins.finder import Plugins @@ -24,7 +20,7 @@ LOG = logging.getLogger(__name__) _ARG = enum.Enum("_ARG", "NO") -_optparse_callable_map: Dict[str, Union[Type[Any], _ARG]] = { +_optparse_callable_map: dict[str, type[Any] | _ARG] = { "int": int, "long": int, "string": str, @@ -44,7 +40,7 @@ class _CallbackAction(argparse.Action): *args: Any, callback: Callable[..., Any], callback_args: Sequence[Any] = (), - callback_kwargs: Optional[Dict[str, Any]] = None, + callback_kwargs: dict[str, Any] | None = None, **kwargs: Any, ) -> None: self._callback = callback @@ -56,8 +52,8 @@ class _CallbackAction(argparse.Action): self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, - values: Optional[Union[Sequence[str], str]], - option_string: Optional[str] = None, + values: Sequence[str] | str | None, + option_string: str | None = None, ) -> None: if not values: values = None @@ -78,8 +74,8 @@ def _flake8_normalize( *args: str, comma_separated_list: bool = False, normalize_paths: bool = False, -) -> Union[str, List[str]]: - ret: Union[str, List[str]] = value +) -> str | list[str]: + ret: str | list[str] = value if comma_separated_list and isinstance(ret, str): ret = utils.parse_comma_separated_list(value) @@ -97,24 +93,24 @@ class Option: def __init__( self, - short_option_name: Union[str, _ARG] = _ARG.NO, - long_option_name: Union[str, _ARG] = _ARG.NO, + short_option_name: str | _ARG = _ARG.NO, + long_option_name: str | _ARG = _ARG.NO, # Options below here are taken from the optparse.Option class - action: Union[str, Type[argparse.Action], _ARG] = _ARG.NO, - default: Union[Any, _ARG] = _ARG.NO, - type: Union[str, Callable[..., Any], _ARG] = _ARG.NO, - dest: Union[str, _ARG] = _ARG.NO, - nargs: Union[int, str, _ARG] = _ARG.NO, - const: Union[Any, _ARG] = _ARG.NO, - choices: Union[Sequence[Any], _ARG] = _ARG.NO, - help: Union[str, _ARG] = _ARG.NO, - metavar: Union[str, _ARG] = _ARG.NO, + action: str | type[argparse.Action] | _ARG = _ARG.NO, + default: Any | _ARG = _ARG.NO, + type: str | Callable[..., Any] | _ARG = _ARG.NO, + dest: str | _ARG = _ARG.NO, + nargs: int | str | _ARG = _ARG.NO, + const: Any | _ARG = _ARG.NO, + choices: Sequence[Any] | _ARG = _ARG.NO, + help: str | _ARG = _ARG.NO, + metavar: str | _ARG = _ARG.NO, # deprecated optparse-only options - callback: Union[Callable[..., Any], _ARG] = _ARG.NO, - callback_args: Union[Sequence[Any], _ARG] = _ARG.NO, - callback_kwargs: Union[Mapping[str, Any], _ARG] = _ARG.NO, + callback: Callable[..., Any] | _ARG = _ARG.NO, + callback_args: Sequence[Any] | _ARG = _ARG.NO, + callback_kwargs: Mapping[str, Any] | _ARG = _ARG.NO, # Options below are taken from argparse.ArgumentParser.add_argument - required: Union[bool, _ARG] = _ARG.NO, + required: bool | _ARG = _ARG.NO, # Options below here are specific to Flake8 parse_from_config: bool = False, comma_separated_list: bool = False, @@ -247,7 +243,7 @@ class Option: self.help = help self.metavar = metavar self.required = required - self.option_kwargs: Dict[str, Union[Any, _ARG]] = { + self.option_kwargs: dict[str, Any | _ARG] = { "action": self.action, "default": self.default, "type": self.type, @@ -268,7 +264,7 @@ class Option: self.comma_separated_list = comma_separated_list self.normalize_paths = normalize_paths - self.config_name: Optional[str] = None + self.config_name: str | None = None if parse_from_config: if long_option_name is _ARG.NO: raise ValueError( @@ -280,7 +276,7 @@ class Option: self._opt = None @property - def filtered_option_kwargs(self) -> Dict[str, Any]: + def filtered_option_kwargs(self) -> dict[str, Any]: """Return any actually-specified arguments.""" return { k: v for k, v in self.option_kwargs.items() if v is not _ARG.NO @@ -307,7 +303,7 @@ class Option: return value - def to_argparse(self) -> Tuple[List[str], Dict[str, Any]]: + def to_argparse(self) -> tuple[list[str], dict[str, Any]]: """Convert a Flake8 Option to argparse ``add_argument`` arguments.""" return self.option_args, self.filtered_option_kwargs @@ -320,7 +316,7 @@ class OptionManager: *, version: str, plugin_versions: str, - parents: List[argparse.ArgumentParser], + parents: list[argparse.ArgumentParser], ) -> None: """Initialize an instance of an OptionManager. @@ -350,17 +346,17 @@ class OptionManager: ) self.parser.add_argument("filenames", nargs="*", metavar="filename") - self.config_options_dict: Dict[str, Option] = {} - self.options: List[Option] = [] - self.extended_default_ignore: List[str] = [] - self.extended_default_select: List[str] = [] + self.config_options_dict: dict[str, Option] = {} + self.options: list[Option] = [] + self.extended_default_ignore: list[str] = [] + self.extended_default_select: list[str] = [] - self._current_group: Optional[argparse._ArgumentGroup] = None + self._current_group: argparse._ArgumentGroup | None = None # TODO: maybe make this a free function to reduce api surface area def register_plugins(self, plugins: Plugins) -> None: """Register the plugin options (if needed).""" - groups: Dict[str, argparse._ArgumentGroup] = {} + groups: dict[str, argparse._ArgumentGroup] = {} def _set_group(name: str) -> None: try: @@ -428,8 +424,8 @@ class OptionManager: def parse_args( self, - args: Optional[Sequence[str]] = None, - values: Optional[argparse.Namespace] = None, + args: Sequence[str] | None = None, + values: argparse.Namespace | None = None, ) -> argparse.Namespace: """Proxy to calling the OptionParser's parse_args method.""" if values: diff --git a/src/flake8/plugins/__init__.py b/src/flake8/plugins/__init__.py index fda6a44..b540313 100644 --- a/src/flake8/plugins/__init__.py +++ b/src/flake8/plugins/__init__.py @@ -1 +1,2 @@ """Submodule of built-in plugins and plugin managers.""" +from __future__ import annotations diff --git a/src/flake8/plugins/finder.py b/src/flake8/plugins/finder.py index 9e9e3af..c051488 100644 --- a/src/flake8/plugins/finder.py +++ b/src/flake8/plugins/finder.py @@ -1,4 +1,6 @@ """Functions related to finding and loading plugins.""" +from __future__ import annotations + import configparser import inspect import itertools @@ -6,14 +8,9 @@ import logging import re import sys from typing import Any -from typing import Dict -from typing import FrozenSet from typing import Generator from typing import Iterable -from typing import List from typing import NamedTuple -from typing import Optional -from typing import Tuple from flake8 import utils from flake8._compat import importlib_metadata @@ -45,7 +42,7 @@ class LoadedPlugin(NamedTuple): plugin: Plugin obj: Any - parameters: Dict[str, bool] + parameters: dict[str, bool] @property def entry_name(self) -> str: @@ -61,17 +58,17 @@ class LoadedPlugin(NamedTuple): class Checkers(NamedTuple): """Classified plugins needed for checking.""" - tree: List[LoadedPlugin] - logical_line: List[LoadedPlugin] - physical_line: List[LoadedPlugin] + tree: list[LoadedPlugin] + logical_line: list[LoadedPlugin] + physical_line: list[LoadedPlugin] class Plugins(NamedTuple): """Classified plugins.""" checkers: Checkers - reporters: Dict[str, LoadedPlugin] - disabled: List[LoadedPlugin] + reporters: dict[str, LoadedPlugin] + disabled: list[LoadedPlugin] def all_plugins(self) -> Generator[LoadedPlugin, None, None]: """Return an iterator over all :class:`LoadedPlugin`s.""" @@ -96,12 +93,12 @@ class Plugins(NamedTuple): class PluginOptions(NamedTuple): """Options related to plugin loading.""" - local_plugin_paths: Tuple[str, ...] - enable_extensions: FrozenSet[str] - require_plugins: FrozenSet[str] + local_plugin_paths: tuple[str, ...] + enable_extensions: frozenset[str] + require_plugins: frozenset[str] @classmethod - def blank(cls) -> "PluginOptions": + def blank(cls) -> PluginOptions: """Make a blank PluginOptions, mostly used for tests.""" return cls( local_plugin_paths=(), @@ -113,8 +110,8 @@ class PluginOptions(NamedTuple): def _parse_option( cfg: configparser.RawConfigParser, cfg_opt_name: str, - opt: Optional[str], -) -> List[str]: + opt: str | None, +) -> list[str]: # specified on commandline: use that if opt is not None: return utils.parse_comma_separated_list(opt) @@ -133,8 +130,8 @@ def parse_plugin_options( cfg: configparser.RawConfigParser, cfg_dir: str, *, - enable_extensions: Optional[str], - require_plugins: Optional[str], + enable_extensions: str | None, + require_plugins: str | None, ) -> PluginOptions: """Parse plugin loading related options.""" paths_s = cfg.get("flake8:local-plugins", "paths", fallback="").strip() @@ -231,8 +228,8 @@ def _find_local_plugins( def _check_required_plugins( - plugins: List[Plugin], - expected: FrozenSet[str], + plugins: list[Plugin], + expected: frozenset[str], ) -> None: plugin_names = { utils.normalize_pypi_name(plugin.package) for plugin in plugins @@ -252,7 +249,7 @@ def _check_required_plugins( def find_plugins( cfg: configparser.RawConfigParser, opts: PluginOptions, -) -> List[Plugin]: +) -> list[Plugin]: """Discovers all plugins (but does not load them).""" ret = [*_find_importlib_plugins(), *_find_local_plugins(cfg)] @@ -264,7 +261,7 @@ def find_plugins( return ret -def _parameters_for(func: Any) -> Dict[str, bool]: +def _parameters_for(func: Any) -> dict[str, bool]: """Return the parameters for the plugin. This will inspect the plugin and return either the function parameters @@ -305,15 +302,15 @@ def _load_plugin(plugin: Plugin) -> LoadedPlugin: def _import_plugins( - plugins: List[Plugin], + plugins: list[Plugin], opts: PluginOptions, -) -> List[LoadedPlugin]: +) -> list[LoadedPlugin]: sys.path.extend(opts.local_plugin_paths) return [_load_plugin(p) for p in plugins] def _classify_plugins( - plugins: List[LoadedPlugin], + plugins: list[LoadedPlugin], opts: PluginOptions, ) -> Plugins: tree = [] @@ -358,7 +355,7 @@ def _classify_plugins( def load_plugins( - plugins: List[Plugin], + plugins: list[Plugin], opts: PluginOptions, ) -> Plugins: """Load and classify all flake8 plugins. diff --git a/src/flake8/plugins/pycodestyle.py b/src/flake8/plugins/pycodestyle.py index 753af23..4b0d67f 100644 --- a/src/flake8/plugins/pycodestyle.py +++ b/src/flake8/plugins/pycodestyle.py @@ -1,8 +1,9 @@ """Generated using ./bin/gen-pycodestyle-plugin.""" # fmt: off +from __future__ import annotations + from typing import Any from typing import Generator -from typing import Tuple from pycodestyle import ambiguous_identifier as _ambiguous_identifier from pycodestyle import bare_except as _bare_except @@ -60,7 +61,7 @@ def pycodestyle_logical( previous_unindented_logical_line: Any, tokens: Any, verbose: Any, -) -> Generator[Tuple[int, str], None, None]: +) -> Generator[tuple[int, str], None, None]: """Run pycodestyle logical checks.""" yield from _ambiguous_identifier(logical_line, tokens) yield from _bare_except(logical_line, noqa) @@ -104,7 +105,7 @@ def pycodestyle_physical( noqa: Any, physical_line: Any, total_lines: Any, -) -> Generator[Tuple[int, str], None, None]: +) -> Generator[tuple[int, str], None, None]: """Run pycodestyle physical checks.""" ret = _maximum_line_length(physical_line, max_line_length, multiline, line_number, noqa) # noqa: E501 if ret is not None: diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index 54eaeca..7b99fd4 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -1,13 +1,12 @@ """Plugin built-in to Flake8 to treat pyflakes as a plugin.""" +from __future__ import annotations + import argparse import ast import os import tokenize from typing import Any from typing import Generator -from typing import List -from typing import Tuple -from typing import Type import pyflakes.checker @@ -68,13 +67,13 @@ class FlakesChecker(pyflakes.checker.Checker): """Subclass the Pyflakes checker to conform with the flake8 API.""" with_doctest = False - include_in_doctest: List[str] = [] - exclude_from_doctest: List[str] = [] + include_in_doctest: list[str] = [] + exclude_from_doctest: list[str] = [] def __init__( self, tree: ast.AST, - file_tokens: List[tokenize.TokenInfo], + file_tokens: list[tokenize.TokenInfo], filename: str, ) -> None: """Initialize the PyFlakes plugin with an AST tree and filename.""" @@ -180,7 +179,7 @@ class FlakesChecker(pyflakes.checker.Checker): f"both for doctesting." ) - def run(self) -> Generator[Tuple[int, int, str, Type[Any]], None, None]: + def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]: """Run the plugin.""" for message in self.messages: col = getattr(message, "col", 0) diff --git a/src/flake8/plugins/reporter.py b/src/flake8/plugins/reporter.py index 5bbbd81..f63b20c 100644 --- a/src/flake8/plugins/reporter.py +++ b/src/flake8/plugins/reporter.py @@ -1,7 +1,8 @@ """Functions for construcing the requested report plugin.""" +from __future__ import annotations + import argparse import logging -from typing import Dict from flake8.formatting.base import BaseFormatter from flake8.plugins.finder import LoadedPlugin @@ -10,7 +11,7 @@ LOG = logging.getLogger(__name__) def make( - reporters: Dict[str, LoadedPlugin], + reporters: dict[str, LoadedPlugin], options: argparse.Namespace, ) -> BaseFormatter: """Make the formatter from the requested user options. diff --git a/src/flake8/processor.py b/src/flake8/processor.py index fa9bd2f..644192d 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -1,14 +1,14 @@ """Module containing our file processor that tokenizes a file for checks.""" +from __future__ import annotations + import argparse import ast import contextlib import logging import tokenize from typing import Any -from typing import Dict from typing import Generator from typing import List -from typing import Optional from typing import Tuple from flake8 import defaults @@ -61,7 +61,7 @@ class FileProcessor: self, filename: str, options: argparse.Namespace, - lines: Optional[List[str]] = None, + lines: list[str] | None = None, ) -> None: """Initialice our file processor. @@ -78,13 +78,13 @@ class FileProcessor: #: Number of blank lines self.blank_lines = 0 #: Checker states for each plugin? - self._checker_states: Dict[str, Dict[Any, Any]] = {} + self._checker_states: dict[str, dict[Any, Any]] = {} #: Current checker state - self.checker_state: Dict[Any, Any] = {} + self.checker_state: dict[Any, Any] = {} #: User provided option for hang closing self.hang_closing = options.hang_closing #: Character used for indentation - self.indent_char: Optional[str] = None + self.indent_char: str | None = None #: Current level of indentation self.indent_level = 0 #: Number of spaces used for indentation @@ -106,19 +106,19 @@ class FileProcessor: #: Previous unindented (i.e. top-level) logical line self.previous_unindented_logical_line = "" #: Current set of tokens - self.tokens: List[tokenize.TokenInfo] = [] + self.tokens: list[tokenize.TokenInfo] = [] #: Total number of lines in the file self.total_lines = len(self.lines) #: Verbosity level of Flake8 self.verbose = options.verbose #: Statistics dictionary self.statistics = {"logical lines": 0} - self._file_tokens: Optional[List[tokenize.TokenInfo]] = None + self._file_tokens: list[tokenize.TokenInfo] | None = None # map from line number to the line we'll search for `noqa` in - self._noqa_line_mapping: Optional[Dict[int, str]] = None + self._noqa_line_mapping: dict[int, str] | None = None @property - def file_tokens(self) -> List[tokenize.TokenInfo]: + def file_tokens(self) -> list[tokenize.TokenInfo]: """Return the complete set of tokens for a file.""" if self._file_tokens is None: line_iter = iter(self.lines) @@ -217,7 +217,7 @@ class FileProcessor: """Build an abstract syntax tree from the list of lines.""" return ast.parse("".join(self.lines)) - def build_logical_line(self) -> Tuple[str, str, _LogicalMapping]: + def build_logical_line(self) -> tuple[str, str, _LogicalMapping]: """Build a logical line from the current tokens list.""" comments, logical, mapping_list = self.build_logical_line_tokens() joined_comments = "".join(comments) @@ -240,9 +240,9 @@ class FileProcessor: def keyword_arguments_for( self, - parameters: Dict[str, bool], - arguments: Dict[str, Any], - ) -> Dict[str, Any]: + parameters: dict[str, bool], + arguments: dict[str, Any], + ) -> dict[str, Any]: """Generate the keyword arguments for a list of parameters.""" ret = {} for param, required in parameters.items(): @@ -269,12 +269,12 @@ class FileProcessor: self.tokens.append(token) yield token - def _noqa_line_range(self, min_line: int, max_line: int) -> Dict[int, str]: + def _noqa_line_range(self, min_line: int, max_line: int) -> dict[int, str]: line_range = range(min_line, max_line + 1) joined = "".join(self.lines[min_line - 1 : max_line]) return dict.fromkeys(line_range, joined) - def noqa_line_for(self, line_number: int) -> Optional[str]: + def noqa_line_for(self, line_number: int) -> str | None: """Retrieve the line which will be used to determine noqa.""" if self._noqa_line_mapping is None: try: @@ -324,7 +324,7 @@ class FileProcessor: self.indent_char = line[0] return line - def read_lines(self) -> List[str]: + def read_lines(self) -> list[str]: """Read the lines for this file checker.""" if self.filename is None or self.filename == "-": self.filename = self.options.stdin_display_name or "stdin" @@ -333,7 +333,7 @@ class FileProcessor: lines = self.read_lines_from_filename() return lines - def read_lines_from_filename(self) -> List[str]: + def read_lines_from_filename(self) -> list[str]: """Read the lines for a file.""" try: with tokenize.open(self.filename) as fd: @@ -344,7 +344,7 @@ class FileProcessor: with open(self.filename, encoding="latin-1") as fd: return fd.readlines() - def read_lines_from_stdin(self) -> List[str]: + def read_lines_from_stdin(self) -> list[str]: """Read the lines from standard in.""" return utils.stdin_get_lines() diff --git a/src/flake8/statistics.py b/src/flake8/statistics.py index ae89be1..a33e6a6 100644 --- a/src/flake8/statistics.py +++ b/src/flake8/statistics.py @@ -1,9 +1,8 @@ """Statistic collection logic for Flake8.""" -from typing import Dict +from __future__ import annotations + from typing import Generator -from typing import List from typing import NamedTuple -from typing import Optional from flake8.violation import Violation @@ -13,9 +12,9 @@ class Statistics: def __init__(self) -> None: """Initialize the underlying dictionary for our statistics.""" - self._store: Dict[Key, "Statistic"] = {} + self._store: dict[Key, Statistic] = {} - def error_codes(self) -> List[str]: + def error_codes(self) -> list[str]: """Return all unique error codes stored. :returns: @@ -23,7 +22,7 @@ class Statistics: """ return sorted({key.code for key in self._store}) - def record(self, error: "Violation") -> None: + def record(self, error: Violation) -> None: """Add the fact that the error was seen in the file. :param error: @@ -36,8 +35,8 @@ class Statistics: self._store[key].increment() def statistics_for( - self, prefix: str, filename: Optional[str] = None - ) -> Generator["Statistic", None, None]: + self, prefix: str, filename: str | None = None + ) -> Generator[Statistic, None, None]: """Generate statistics for the prefix and filename. If you have a :class:`Statistics` object that has recorded errors, @@ -79,11 +78,11 @@ class Key(NamedTuple): code: str @classmethod - def create_from(cls, error: "Violation") -> "Key": + def create_from(cls, error: Violation) -> Key: """Create a Key from :class:`flake8.violation.Violation`.""" return cls(filename=error.filename, code=error.code) - def matches(self, prefix: str, filename: Optional[str]) -> bool: + def matches(self, prefix: str, filename: str | None) -> bool: """Determine if this key matches some constraints. :param prefix: @@ -118,7 +117,7 @@ class Statistic: self.count = count @classmethod - def create_from(cls, error: "Violation") -> "Statistic": + def create_from(cls, error: Violation) -> Statistic: """Create a Statistic from a :class:`flake8.violation.Violation`.""" return cls( error_code=error.code, diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index b7115a0..2fee0f3 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -1,4 +1,6 @@ """Implementation of the StyleGuide used by Flake8.""" +from __future__ import annotations + import argparse import contextlib import copy @@ -6,14 +8,8 @@ import enum import functools import itertools import logging -from typing import Dict from typing import Generator -from typing import List -from typing import Optional from typing import Sequence -from typing import Set -from typing import Tuple -from typing import Union from flake8 import defaults from flake8 import statistics @@ -49,20 +45,20 @@ class Decision(enum.Enum): def _explicitly_chosen( *, - option: Optional[List[str]], - extend: Optional[List[str]], -) -> Tuple[str, ...]: + option: list[str] | None, + extend: list[str] | None, +) -> tuple[str, ...]: ret = [*(option or []), *(extend or [])] return tuple(sorted(ret, reverse=True)) def _select_ignore( *, - option: Optional[List[str]], - default: Tuple[str, ...], - extended_default: List[str], - extend: Optional[List[str]], -) -> Tuple[str, ...]: + option: list[str] | None, + default: tuple[str, ...], + extended_default: list[str], + extend: list[str] | None, +) -> tuple[str, ...]: # option was explicitly set, ignore the default and extended default if option is not None: ret = [*option, *(extend or [])] @@ -80,7 +76,7 @@ class DecisionEngine: def __init__(self, options: argparse.Namespace) -> None: """Initialize the engine.""" - self.cache: Dict[str, Decision] = {} + self.cache: dict[str, Decision] = {} self.selected_explicitly = _explicitly_chosen( option=options.select, @@ -104,7 +100,7 @@ class DecisionEngine: extend=options.extend_ignore, ) - def was_selected(self, code: str) -> Union[Selected, Ignored]: + def was_selected(self, code: str) -> Selected | Ignored: """Determine if the code has been selected by the user. :param code: The code for the check that has been run. @@ -122,7 +118,7 @@ class DecisionEngine: else: return Ignored.Implicitly - def was_ignored(self, code: str) -> Union[Selected, Ignored]: + def was_ignored(self, code: str) -> Selected | Ignored: """Determine if the code has been ignored by the user. :param code: @@ -211,7 +207,7 @@ class StyleGuideManager: self, options: argparse.Namespace, formatter: base_formatter.BaseFormatter, - decider: Optional[DecisionEngine] = None, + decider: DecisionEngine | None = None, ) -> None: """Initialize our StyleGuide. @@ -221,7 +217,7 @@ class StyleGuideManager: self.formatter = formatter self.stats = statistics.Statistics() self.decider = decider or DecisionEngine(options) - self.style_guides: List[StyleGuide] = [] + self.style_guides: list[StyleGuide] = [] self.default_style_guide = StyleGuide( options, formatter, self.stats, decider=decider ) @@ -238,7 +234,7 @@ class StyleGuideManager: def populate_style_guides_with( self, options: argparse.Namespace - ) -> Generator["StyleGuide", None, None]: + ) -> Generator[StyleGuide, None, None]: """Generate style guides from the per-file-ignores option. :param options: @@ -252,7 +248,7 @@ class StyleGuideManager: filename=filename, extend_ignore_with=violations ) - def _style_guide_for(self, filename: str) -> "StyleGuide": + def _style_guide_for(self, filename: str) -> StyleGuide: """Find the StyleGuide for the filename in particular.""" return max( (g for g in self.style_guides if g.applies_to(filename)), @@ -262,7 +258,7 @@ class StyleGuideManager: @contextlib.contextmanager def processing_file( self, filename: str - ) -> Generator["StyleGuide", None, None]: + ) -> Generator[StyleGuide, None, None]: """Record the fact that we're processing the file's results.""" guide = self.style_guide_for(filename) with guide.processing_file(filename): @@ -275,7 +271,7 @@ class StyleGuideManager: line_number: int, column_number: int, text: str, - physical_line: Optional[str] = None, + physical_line: str | None = None, ) -> int: """Handle an error reported by a check. @@ -302,7 +298,7 @@ class StyleGuideManager: code, filename, line_number, column_number, text, physical_line ) - def add_diff_ranges(self, diffinfo: Dict[str, Set[int]]) -> None: + def add_diff_ranges(self, diffinfo: dict[str, set[int]]) -> None: """Update the StyleGuides to filter out information not in the diff. This provides information to the underlying StyleGuides so that only @@ -323,8 +319,8 @@ class StyleGuide: options: argparse.Namespace, formatter: base_formatter.BaseFormatter, stats: statistics.Statistics, - filename: Optional[str] = None, - decider: Optional[DecisionEngine] = None, + filename: str | None = None, + decider: DecisionEngine | None = None, ): """Initialize our StyleGuide. @@ -337,7 +333,7 @@ class StyleGuide: self.filename = filename if self.filename: self.filename = utils.normalize_path(self.filename) - self._parsed_diff: Dict[str, Set[int]] = {} + self._parsed_diff: dict[str, set[int]] = {} def __repr__(self) -> str: """Make it easier to debug which StyleGuide we're using.""" @@ -345,9 +341,9 @@ class StyleGuide: def copy( self, - filename: Optional[str] = None, - extend_ignore_with: Optional[Sequence[str]] = None, - ) -> "StyleGuide": + filename: str | None = None, + extend_ignore_with: Sequence[str] | None = None, + ) -> StyleGuide: """Create a copy of this style guide with different values.""" filename = filename or self.filename options = copy.deepcopy(self.options) @@ -360,7 +356,7 @@ class StyleGuide: @contextlib.contextmanager def processing_file( self, filename: str - ) -> Generator["StyleGuide", None, None]: + ) -> Generator[StyleGuide, None, None]: """Record the fact that we're processing the file's results.""" self.formatter.beginning(filename) yield self @@ -405,7 +401,7 @@ class StyleGuide: line_number: int, column_number: int, text: str, - physical_line: Optional[str] = None, + physical_line: str | None = None, ) -> int: """Handle an error reported by a check. @@ -451,7 +447,7 @@ class StyleGuide: return 1 return 0 - def add_diff_ranges(self, diffinfo: Dict[str, Set[int]]) -> None: + def add_diff_ranges(self, diffinfo: dict[str, set[int]]) -> None: """Update the StyleGuide to filter out information not in the diff. This provides information to the StyleGuide so that only the errors diff --git a/src/flake8/utils.py b/src/flake8/utils.py index cc47ffc..60555a9 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -1,4 +1,6 @@ """Utility methods for flake8.""" +from __future__ import annotations + import collections import fnmatch as _fnmatch import functools @@ -10,15 +12,9 @@ import re import sys import textwrap import tokenize -from typing import Dict -from typing import List from typing import NamedTuple -from typing import Optional from typing import Pattern from typing import Sequence -from typing import Set -from typing import Tuple -from typing import Union from flake8 import exceptions @@ -30,7 +26,7 @@ NORMALIZE_PACKAGE_NAME_RE = re.compile(r"[-_.]+") def parse_comma_separated_list( value: str, regexp: Pattern[str] = COMMA_SEPARATED_LIST_RE -) -> List[str]: +) -> list[str]: """Parse a comma-separated list. :param value: @@ -64,7 +60,7 @@ _FILE_LIST_TOKEN_TYPES = [ ] -def _tokenize_files_to_codes_mapping(value: str) -> List[_Token]: +def _tokenize_files_to_codes_mapping(value: str) -> list[_Token]: tokens = [] i = 0 while i < len(value): @@ -82,8 +78,8 @@ def _tokenize_files_to_codes_mapping(value: str) -> List[_Token]: def parse_files_to_codes_mapping( # noqa: C901 - value_: Union[Sequence[str], str] -) -> List[Tuple[str, List[str]]]: + value_: Sequence[str] | str, +) -> list[tuple[str, list[str]]]: """Parse a files-to-codes mapping. A files-to-codes mapping a sequence of values specified as @@ -97,15 +93,15 @@ def parse_files_to_codes_mapping( # noqa: C901 else: value = value_ - ret: List[Tuple[str, List[str]]] = [] + ret: list[tuple[str, list[str]]] = [] if not value.strip(): return ret class State: seen_sep = True seen_colon = False - filenames: List[str] = [] - codes: List[str] = [] + filenames: list[str] = [] + codes: list[str] = [] def _reset() -> None: if State.codes: @@ -157,7 +153,7 @@ def parse_files_to_codes_mapping( # noqa: C901 def normalize_paths( paths: Sequence[str], parent: str = os.curdir -) -> List[str]: +) -> list[str]: """Normalize a list of paths relative to a parent directory. :returns: @@ -201,12 +197,12 @@ def stdin_get_value() -> str: return stdin_value.decode("utf-8") -def stdin_get_lines() -> List[str]: +def stdin_get_lines() -> list[str]: """Return lines of stdin split according to file splitting.""" return list(io.StringIO(stdin_get_value())) -def parse_unified_diff(diff: Optional[str] = None) -> Dict[str, Set[int]]: +def parse_unified_diff(diff: str | None = None) -> dict[str, set[int]]: """Parse the unified diff passed on stdin. :returns: @@ -218,7 +214,7 @@ def parse_unified_diff(diff: Optional[str] = None) -> Dict[str, Set[int]]: number_of_rows = None current_path = None - parsed_paths: Dict[str, Set[int]] = collections.defaultdict(set) + parsed_paths: dict[str, set[int]] = collections.defaultdict(set) for line in diff.splitlines(): if number_of_rows: if not line or line[0] != "-": @@ -271,7 +267,7 @@ def parse_unified_diff(diff: Optional[str] = None) -> Dict[str, Set[int]]: return parsed_paths -def is_using_stdin(paths: List[str]) -> bool: +def is_using_stdin(paths: list[str]) -> bool: """Determine if we're going to read from stdin. :param paths: diff --git a/src/flake8/violation.py b/src/flake8/violation.py index d2d2578..45834b2 100644 --- a/src/flake8/violation.py +++ b/src/flake8/violation.py @@ -1,12 +1,11 @@ """Contains the Violation error class used internally.""" +from __future__ import annotations + import functools import linecache import logging -from typing import Dict from typing import Match from typing import NamedTuple -from typing import Optional -from typing import Set from flake8 import defaults from flake8 import utils @@ -16,7 +15,7 @@ LOG = logging.getLogger(__name__) @functools.lru_cache(maxsize=512) -def _find_noqa(physical_line: str) -> Optional[Match[str]]: +def _find_noqa(physical_line: str) -> Match[str] | None: return defaults.NOQA_INLINE_REGEXP.search(physical_line) @@ -28,7 +27,7 @@ class Violation(NamedTuple): line_number: int column_number: int text: str - physical_line: Optional[str] + physical_line: str | None def is_inline_ignored(self, disable_noqa: bool) -> bool: """Determine if a comment has been added to ignore this line. @@ -69,7 +68,7 @@ class Violation(NamedTuple): ) return False - def is_in(self, diff: Dict[str, Set[int]]) -> bool: + def is_in(self, diff: dict[str, set[int]]) -> bool: """Determine if the violation is included in a diff's line ranges. This function relies on the parsed data added via diff --git a/tests/__init__.py b/tests/__init__.py index f7ac891..ee1f2a0 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1 +1,2 @@ """This is here because mypy doesn't understand PEP 420.""" +from __future__ import annotations diff --git a/tests/conftest.py b/tests/conftest.py index 0f48309..ac413fb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,6 @@ """Test configuration for py.test.""" +from __future__ import annotations + import sys import flake8 diff --git a/tests/integration/subdir/aplugin.py b/tests/integration/subdir/aplugin.py index 801f2c0..97b06a9 100644 --- a/tests/integration/subdir/aplugin.py +++ b/tests/integration/subdir/aplugin.py @@ -1,4 +1,5 @@ """Module that is off sys.path by default, for testing local-plugin-paths.""" +from __future__ import annotations class ExtensionTestPlugin2: diff --git a/tests/integration/test_aggregator.py b/tests/integration/test_aggregator.py index d35266f..a5b39d7 100644 --- a/tests/integration/test_aggregator.py +++ b/tests/integration/test_aggregator.py @@ -1,4 +1,6 @@ """Test aggregation of config files and command-line options.""" +from __future__ import annotations + import os import pytest diff --git a/tests/integration/test_api_legacy.py b/tests/integration/test_api_legacy.py index efb0fc9..b386bd5 100644 --- a/tests/integration/test_api_legacy.py +++ b/tests/integration/test_api_legacy.py @@ -1,4 +1,6 @@ """Integration tests for the legacy api.""" +from __future__ import annotations + from flake8.api import legacy diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py index 9223ec4..ab9eb27 100644 --- a/tests/integration/test_checker.py +++ b/tests/integration/test_checker.py @@ -1,4 +1,6 @@ """Integration tests for the checker submodule.""" +from __future__ import annotations + import sys from unittest import mock diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index fe254b7..e711fb3 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -1,4 +1,6 @@ """Integration tests for the main entrypoint of flake8.""" +from __future__ import annotations + import json import os import sys diff --git a/tests/integration/test_plugins.py b/tests/integration/test_plugins.py index 4323d05..0b4424a 100644 --- a/tests/integration/test_plugins.py +++ b/tests/integration/test_plugins.py @@ -1,4 +1,6 @@ """Integration tests for plugin loading.""" +from __future__ import annotations + import pytest from flake8.main.cli import main diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 904366e..0f8386a 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -1,4 +1,6 @@ """Shared fixtures between unit tests.""" +from __future__ import annotations + import argparse import pytest diff --git a/tests/unit/plugins/finder_test.py b/tests/unit/plugins/finder_test.py index 63f8156..d526fd1 100644 --- a/tests/unit/plugins/finder_test.py +++ b/tests/unit/plugins/finder_test.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import configparser import sys from unittest import mock diff --git a/tests/unit/plugins/pycodestyle_test.py b/tests/unit/plugins/pycodestyle_test.py index 703970f..1b00d9d 100644 --- a/tests/unit/plugins/pycodestyle_test.py +++ b/tests/unit/plugins/pycodestyle_test.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import importlib.machinery import importlib.util import os.path diff --git a/tests/unit/plugins/reporter_test.py b/tests/unit/plugins/reporter_test.py index 4b46cc4..07b0dfa 100644 --- a/tests/unit/plugins/reporter_test.py +++ b/tests/unit/plugins/reporter_test.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import argparse import pytest diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index 508f83b..04147ec 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -1,4 +1,6 @@ """Tests for the Application class.""" +from __future__ import annotations + import argparse import pytest diff --git a/tests/unit/test_base_formatter.py b/tests/unit/test_base_formatter.py index 7830eb4..5b57335 100644 --- a/tests/unit/test_base_formatter.py +++ b/tests/unit/test_base_formatter.py @@ -1,4 +1,6 @@ """Tests for the BaseFormatter object.""" +from __future__ import annotations + import argparse import sys from unittest import mock diff --git a/tests/unit/test_checker_manager.py b/tests/unit/test_checker_manager.py index c6114f6..32bc9a4 100644 --- a/tests/unit/test_checker_manager.py +++ b/tests/unit/test_checker_manager.py @@ -1,4 +1,6 @@ """Tests for the Manager object for FileCheckers.""" +from __future__ import annotations + import errno import multiprocessing from unittest import mock diff --git a/tests/unit/test_debug.py b/tests/unit/test_debug.py index 1fc93ef..6d8806c 100644 --- a/tests/unit/test_debug.py +++ b/tests/unit/test_debug.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from unittest import mock from flake8._compat import importlib_metadata diff --git a/tests/unit/test_decision_engine.py b/tests/unit/test_decision_engine.py index 59c372a..27ced3f 100644 --- a/tests/unit/test_decision_engine.py +++ b/tests/unit/test_decision_engine.py @@ -1,4 +1,6 @@ """Tests for the flake8.style_guide.DecisionEngine class.""" +from __future__ import annotations + import argparse import pytest diff --git a/tests/unit/test_discover_files.py b/tests/unit/test_discover_files.py index f2dfa13..edf047d 100644 --- a/tests/unit/test_discover_files.py +++ b/tests/unit/test_discover_files.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import os.path import pytest diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 96d0244..99b298b 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -1,4 +1,6 @@ """Tests for the flake8.exceptions module.""" +from __future__ import annotations + import pickle import pytest diff --git a/tests/unit/test_file_checker.py b/tests/unit/test_file_checker.py index 3fe2e51..368b5f6 100644 --- a/tests/unit/test_file_checker.py +++ b/tests/unit/test_file_checker.py @@ -1,4 +1,6 @@ """Unit tests for the FileChecker class.""" +from __future__ import annotations + import argparse from unittest import mock diff --git a/tests/unit/test_file_processor.py b/tests/unit/test_file_processor.py index e8ebbc1..bd693b3 100644 --- a/tests/unit/test_file_processor.py +++ b/tests/unit/test_file_processor.py @@ -1,4 +1,6 @@ """Tests for the FileProcessor class.""" +from __future__ import annotations + import ast import tokenize from unittest import mock diff --git a/tests/unit/test_filenameonly_formatter.py b/tests/unit/test_filenameonly_formatter.py index e92d4bb..77f75b9 100644 --- a/tests/unit/test_filenameonly_formatter.py +++ b/tests/unit/test_filenameonly_formatter.py @@ -1,4 +1,6 @@ """Tests for the FilenameOnly formatter object.""" +from __future__ import annotations + import argparse from flake8.formatting import default diff --git a/tests/unit/test_legacy_api.py b/tests/unit/test_legacy_api.py index 0e5b535..844bd5a 100644 --- a/tests/unit/test_legacy_api.py +++ b/tests/unit/test_legacy_api.py @@ -1,4 +1,6 @@ """Tests for Flake8's legacy API.""" +from __future__ import annotations + import argparse import configparser import os.path diff --git a/tests/unit/test_main_options.py b/tests/unit/test_main_options.py index aea2071..7c1feba 100644 --- a/tests/unit/test_main_options.py +++ b/tests/unit/test_main_options.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from flake8.main import options diff --git a/tests/unit/test_nothing_formatter.py b/tests/unit/test_nothing_formatter.py index eb4b862..76929fd 100644 --- a/tests/unit/test_nothing_formatter.py +++ b/tests/unit/test_nothing_formatter.py @@ -1,4 +1,6 @@ """Tests for the Nothing formatter obbject.""" +from __future__ import annotations + import argparse from flake8.formatting import default diff --git a/tests/unit/test_option.py b/tests/unit/test_option.py index d576c49..4b3070d 100644 --- a/tests/unit/test_option.py +++ b/tests/unit/test_option.py @@ -1,4 +1,6 @@ """Unit tests for flake8.options.manager.Option.""" +from __future__ import annotations + import functools from unittest import mock diff --git a/tests/unit/test_option_manager.py b/tests/unit/test_option_manager.py index 0a56076..d5b88c3 100644 --- a/tests/unit/test_option_manager.py +++ b/tests/unit/test_option_manager.py @@ -1,4 +1,6 @@ """Unit tests for flake.options.manager.OptionManager.""" +from __future__ import annotations + import argparse import os from unittest import mock diff --git a/tests/unit/test_options_config.py b/tests/unit/test_options_config.py index 4ad6acd..0890ea9 100644 --- a/tests/unit/test_options_config.py +++ b/tests/unit/test_options_config.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import configparser import os.path from unittest import mock diff --git a/tests/unit/test_pyflakes_codes.py b/tests/unit/test_pyflakes_codes.py index c251721..4626e3d 100644 --- a/tests/unit/test_pyflakes_codes.py +++ b/tests/unit/test_pyflakes_codes.py @@ -1,4 +1,6 @@ """Tests of pyflakes monkey patches.""" +from __future__ import annotations + import ast import pyflakes diff --git a/tests/unit/test_statistics.py b/tests/unit/test_statistics.py index 03f3189..261f360 100644 --- a/tests/unit/test_statistics.py +++ b/tests/unit/test_statistics.py @@ -1,4 +1,6 @@ """Tests for the statistics module in Flake8.""" +from __future__ import annotations + import pytest from flake8 import statistics as stats diff --git a/tests/unit/test_style_guide.py b/tests/unit/test_style_guide.py index e4aaff2..94fcb26 100644 --- a/tests/unit/test_style_guide.py +++ b/tests/unit/test_style_guide.py @@ -1,4 +1,6 @@ """Tests for the flake8.style_guide.StyleGuide class.""" +from __future__ import annotations + import argparse from unittest import mock diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index fefe662..98a5a5d 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,4 +1,6 @@ """Tests for flake8's utils module.""" +from __future__ import annotations + import io import logging import os diff --git a/tests/unit/test_violation.py b/tests/unit/test_violation.py index 6b47691..a4a43da 100644 --- a/tests/unit/test_violation.py +++ b/tests/unit/test_violation.py @@ -1,4 +1,6 @@ """Tests for the flake8.violation.Violation class.""" +from __future__ import annotations + from unittest import mock import pytest diff --git a/tox.ini b/tox.ini index c0763c1..df5a75c 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion=2.3.1 -envlist = py36,py37,py38,flake8,linters,docs +envlist = py37,py38,flake8,linters,docs [testenv] deps = From 3642af2dcad88107dac7dc8bda03b5ada36d58fd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 9 Aug 2022 00:38:58 +0000 Subject: [PATCH 006/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/PyCQA/flake8: 5.0.3 → 5.0.4](https://github.com/PyCQA/flake8/compare/5.0.3...5.0.4) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 07a863e..0ca677a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,7 +23,7 @@ repos: - id: black args: [--line-length=79] - repo: https://github.com/PyCQA/flake8 - rev: 5.0.3 + rev: 5.0.4 hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy From 68455f0e8f03a33b31216b547a1b15358d145e5a Mon Sep 17 00:00:00 2001 From: Dragos Velicanu Date: Wed, 10 Aug 2022 08:32:56 -0700 Subject: [PATCH 007/131] fix documentation of color configuruation --- docs/source/user/options.rst | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/docs/source/user/options.rst b/docs/source/user/options.rst index ab333c8..d9f1221 100644 --- a/docs/source/user/options.rst +++ b/docs/source/user/options.rst @@ -193,7 +193,7 @@ Options and their Descriptions Possible options are ``auto``, ``always``, and ``never``. - This **can** be specified in config files. + This **can not** be specified in config files. When color is enabled, the following substitutions are enabled: @@ -208,12 +208,6 @@ Options and their Descriptions - ``%(white)s`` - ``%(reset)s`` - Example config file usage: - - .. code-block:: ini - - color = never - .. option:: --count From 2ee3c56d9340df57516ec1214a930114ee6a6405 Mon Sep 17 00:00:00 2001 From: Kai Mueller Date: Sun, 4 Sep 2022 15:33:18 +0000 Subject: [PATCH 008/131] Fix typo casae->case --- docs/source/plugin-development/registering-plugins.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/plugin-development/registering-plugins.rst b/docs/source/plugin-development/registering-plugins.rst index 065bdd8..63e279e 100644 --- a/docs/source/plugin-development/registering-plugins.rst +++ b/docs/source/plugin-development/registering-plugins.rst @@ -120,7 +120,7 @@ it would look like:: X10 = flake8_example:ExamplePlugin -In this casae as well as the following case, your entry-point name acts as +In this case as well as the following case, your entry-point name acts as a prefix to the error codes produced by your plugin. If all of your plugin's error codes start with ``X1`` then it would look From e299674866aee0946057fbed75711c501c3e686e Mon Sep 17 00:00:00 2001 From: Kai Mueller Date: Sun, 4 Sep 2022 15:55:29 +0000 Subject: [PATCH 009/131] Document off-by-default feature Add a short section to the plugin development. See #1676 --- docs/source/plugin-development/registering-plugins.rst | 6 ++++++ docs/source/user/options.rst | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/source/plugin-development/registering-plugins.rst b/docs/source/plugin-development/registering-plugins.rst index 63e279e..ca74008 100644 --- a/docs/source/plugin-development/registering-plugins.rst +++ b/docs/source/plugin-development/registering-plugins.rst @@ -143,6 +143,12 @@ i.e., ``ABC`` is better than ``A`` but ``ABCD`` is invalid. *A 3 letters entry point prefix followed by 3 numbers (i.e.* ``ABC123`` *) is currently the longest allowed entry point name.* +.. _off-by-default: + +If your plugin is intended to be opt-in, it can set the attribute +``off_by_default = True``. Users of your plugin will then need to utilize +:ref:`enable-extensions` with your plugin's entry +point. .. _Entry Points: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points diff --git a/docs/source/user/options.rst b/docs/source/user/options.rst index d9f1221..170c1e7 100644 --- a/docs/source/user/options.rst +++ b/docs/source/user/options.rst @@ -794,11 +794,13 @@ Options and their Descriptions flake8-typing-extensions +.. _option-enable-extensions: + .. option:: --enable-extensions= :ref:`Go back to index ` - Enable off-by-default extensions. + Enable :ref:`off-by-default` extensions. Plugins to |Flake8| have the option of registering themselves as off-by-default. These plugins will not be loaded unless enabled by this From 46b404aa1f016bd3311eadaa03458827eb439ac2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 Sep 2022 01:17:10 +0000 Subject: [PATCH 010/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 22.6.0 → 22.8.0](https://github.com/psf/black/compare/22.6.0...22.8.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3ce2935..88b235d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,7 +22,7 @@ repos: - id: pyupgrade args: [--py37-plus] - repo: https://github.com/psf/black - rev: 22.6.0 + rev: 22.8.0 hooks: - id: black args: [--line-length=79] From a3e31c2f44449fe4392edb1ea485c458f9b539fe Mon Sep 17 00:00:00 2001 From: wookie184 Date: Tue, 6 Sep 2022 18:29:02 +0100 Subject: [PATCH 011/131] Prefer extend-ignore over ignore in general examples --- docs/source/index.rst | 4 ++-- docs/source/user/configuration.rst | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/source/index.rst b/docs/source/index.rst index 51b0189..f4c5938 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -60,11 +60,11 @@ If you only want to see the instances of a specific warning or error, you can flake8 --select E123,W503 path/to/code/ -Alternatively, if you want to *ignore* only one specific warning or error: +Alternatively, if you want to add a specific warning or error to *ignore*: .. code:: - flake8 --ignore E24,W504 path/to/code/ + flake8 --extend-ignore E203,W234 path/to/code/ Please read our user guide for more information about how to use and configure |Flake8|. diff --git a/docs/source/user/configuration.rst b/docs/source/user/configuration.rst index eca2667..70fc22b 100644 --- a/docs/source/user/configuration.rst +++ b/docs/source/user/configuration.rst @@ -90,7 +90,7 @@ Let's actually look at |Flake8|'s own configuration section: .. code-block:: ini [flake8] - ignore = D203 + extend-ignore = E203 exclude = .git,__pycache__,docs/source/conf.py,old,build,dist max-complexity = 10 @@ -98,7 +98,7 @@ This is equivalent to: .. prompt:: bash - flake8 --ignore D203 \ + flake8 --extend-ignore E203 \ --exclude .git,__pycache__,docs/source/conf.py,old,build,dist \ --max-complexity 10 @@ -107,7 +107,7 @@ In our case, if we wanted to, we could also do .. code-block:: ini [flake8] - ignore = D203 + extend-ignore = E203 exclude = .git, __pycache__, @@ -122,7 +122,7 @@ This allows us to add comments for why we're excluding items, e.g. .. code-block:: ini [flake8] - ignore = D203 + extend-ignore = E203 exclude = # No need to traverse our git directory .git, @@ -190,7 +190,7 @@ look at a portion of a project's Flake8 configuration in their ``tox.ini``: # H404: multi line docstring should start without a leading new line # H405: multi line docstring summary not separated with an empty line # H501: Do not use self.__dict__ for string formatting - ignore = H101,H202,H233,H301,H306,H401,H403,H404,H405,H501 + extend-ignore = H101,H202,H233,H301,H306,H401,H403,H404,H405,H501 They use the comments to describe the check but they could also write this as: @@ -198,7 +198,7 @@ They use the comments to describe the check but they could also write this as: [flake8] # it's not a bug that we aren't using all of hacking - ignore = + extend-ignore = # H101: Use TODO(NAME) H101, # H202: assertRaises Exception too broad From 8c7c38bad9a238f0bec9bc9ec58e87e7cc22d5e7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Sep 2022 00:54:40 +0000 Subject: [PATCH 012/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.37.3 → v2.38.0](https://github.com/asottile/pyupgrade/compare/v2.37.3...v2.38.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 88b235d..f38f352 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v2.37.3 + rev: v2.38.0 hooks: - id: pyupgrade args: [--py37-plus] From 045923237e8bcfcf109ce0621485ba06182ccba0 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Thu, 22 Sep 2022 20:28:48 -0400 Subject: [PATCH 013/131] fix documentation for the --count option It erroneously claimed that it set the application exit code to 1 if the count was greater than 1. However, this is false, because the --count option doesn't modify the error code at any time. If the count was greater than 1, then the exit code was already 1, even in the absence of --count, unless --exit-zero was used. This documentation bug resulted in people reading the `flake8 --help` output and believing that --count is mandatory in order to ensure that flake8 produces errors in automated processes (such as CI scripts) when flake8 violations are detected. --- src/flake8/main/options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flake8/main/options.py b/src/flake8/main/options.py index d603232..9b374ab 100644 --- a/src/flake8/main/options.py +++ b/src/flake8/main/options.py @@ -159,8 +159,8 @@ def register_default_options(option_manager: OptionManager) -> None: "--count", action="store_true", parse_from_config=True, - help="Print total number of errors to standard output and " - "set the exit code to 1 if total is not empty.", + help="Print total number of errors to standard output after " + "all other output.", ) add_option( From eafb4d72540c371cb3b9f268e25bc7c2b8b3c4ae Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 22 Sep 2022 21:04:10 -0400 Subject: [PATCH 014/131] remove example help text from docs to avoid confusion --- docs/source/user/invocation.rst | 66 +-------------------------------- 1 file changed, 1 insertion(+), 65 deletions(-) diff --git a/docs/source/user/invocation.rst b/docs/source/user/invocation.rst index 590bcf8..0049ec9 100644 --- a/docs/source/user/invocation.rst +++ b/docs/source/user/invocation.rst @@ -86,69 +86,5 @@ And you should see something like: Options: --version show program's version number and exit -h, --help show this help message and exit - -v, --verbose Print more information about what is happening in - flake8. This option is repeatable and will increase - verbosity each time it is repeated. - -q, --quiet Report only file names, or nothing. This option is - repeatable. - --count Print total number of errors and warnings to standard - error and set the exit code to 1 if total is not - empty. - --diff Report changes only within line number ranges in the - unified diff provided on standard in by the user. - --exclude=patterns Comma-separated list of files or directories to - exclude.(Default: - .svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.nox,.eggs, - *.egg) - --filename=patterns Only check for filenames matching the patterns in this - comma-separated list. (Default: *.py) - --format=format Format errors according to the chosen formatter. - --hang-closing Hang closing bracket instead of matching indentation - of opening bracket's line. - --ignore=errors Comma-separated list of errors and warnings to ignore - (or skip). For example, ``--ignore=E4,E51,W234``. - (Default: E121,E123,E126,E226,E24,E704) - --extend-ignore=errors - Comma-separated list of errors and warnings to add to - the list of ignored ones. For example, ``--extend- - ignore=E4,E51,W234``. - --max-line-length=n Maximum allowed line length for the entirety of this - run. (Default: 79) - --select=errors Comma-separated list of errors and warnings to enable. - For example, ``--select=E4,E51,W234``. (Default: ) - --extend-select errors - Comma-separated list of errors and warnings to add to - the list of selected ones. For example, ``--extend- - select=E4,E51,W234``. - --disable-noqa Disable the effect of "# noqa". This will report - errors on lines with "# noqa" at the end. - --show-source Show the source generate each error or warning. - --statistics Count errors and warnings. - --enabled-extensions=ENABLED_EXTENSIONS - Enable plugins and extensions that are otherwise - disabled by default - --exit-zero Exit with status code "0" even if there are errors. - -j JOBS, --jobs=JOBS Number of subprocesses to use to run checks in - parallel. This is ignored on Windows. The default, - "auto", will auto-detect the number of processors - available to use. (Default: auto) - --output-file=OUTPUT_FILE - Redirect report to a file. - --tee Write to stdout and output-file. - --append-config=APPEND_CONFIG - Provide extra config files to parse in addition to the - files found by Flake8 by default. These files are the - last ones read and so they take the highest precedence - when multiple files provide the same option. - --config=CONFIG Path to the config file that will be the authoritative - config source. This will cause Flake8 to ignore all - other configuration files. - --isolated Ignore all configuration files. - --builtins=BUILTINS define more built-ins, comma separated - --doctests check syntax of the doctests - --include-in-doctest=INCLUDE_IN_DOCTEST - Run doctests only on these files - --exclude-from-doctest=EXCLUDE_FROM_DOCTEST - Skip these files when running doctests - Installed plugins: pyflakes: 1.0.0, pep8: 1.7.0 + ... From d59c42769c3907f38adfeab0581be80d043c290b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 27 Sep 2022 01:09:19 +0000 Subject: [PATCH 015/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/reorder_python_imports: v3.8.2 → v3.8.3](https://github.com/asottile/reorder_python_imports/compare/v3.8.2...v3.8.3) - [github.com/asottile/pyupgrade: v2.38.0 → v2.38.2](https://github.com/asottile/pyupgrade/compare/v2.38.0...v2.38.2) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f38f352..1902447 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/reorder_python_imports - rev: v3.8.2 + rev: v3.8.3 hooks: - id: reorder-python-imports args: [ @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v2.38.0 + rev: v2.38.2 hooks: - id: pyupgrade args: [--py37-plus] From 4bba8798ea427ff3ad472b990784144fb4b1e661 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 3 Oct 2022 12:25:05 -0400 Subject: [PATCH 016/131] Add link to GitHub Sponsors for primary maintainer --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..eb54a96 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: asottile From b029e85b44eabe48c8e22d0bced180472fd3697b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Oct 2022 01:05:39 +0000 Subject: [PATCH 017/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v0.971 → v0.981](https://github.com/pre-commit/mirrors-mypy/compare/v0.971...v0.981) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1902447..5ae2954 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.971 + rev: v0.981 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From df938795956da742246c930c463dff504676993a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Oct 2022 02:26:58 +0000 Subject: [PATCH 018/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/reorder_python_imports: v3.8.3 → v3.8.4](https://github.com/asottile/reorder_python_imports/compare/v3.8.3...v3.8.4) - [github.com/asottile/pyupgrade: v2.38.2 → v3.1.0](https://github.com/asottile/pyupgrade/compare/v2.38.2...v3.1.0) - [github.com/psf/black: 22.8.0 → 22.10.0](https://github.com/psf/black/compare/22.8.0...22.10.0) - [github.com/pre-commit/mirrors-mypy: v0.981 → v0.982](https://github.com/pre-commit/mirrors-mypy/compare/v0.981...v0.982) --- .pre-commit-config.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5ae2954..7e05447 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/reorder_python_imports - rev: v3.8.3 + rev: v3.8.4 hooks: - id: reorder-python-imports args: [ @@ -17,12 +17,12 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v2.38.2 + rev: v3.1.0 hooks: - id: pyupgrade args: [--py37-plus] - repo: https://github.com/psf/black - rev: 22.8.0 + rev: 22.10.0 hooks: - id: black args: [--line-length=79] @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.981 + rev: v0.982 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 48b29191303b271a162b3acbdfba3e5cf310b50c Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Thu, 28 Jul 2022 22:06:58 +0200 Subject: [PATCH 019/131] Display list of available formatters with help for --format --- src/flake8/main/application.py | 1 + src/flake8/main/options.py | 10 +++++++++- src/flake8/options/manager.py | 2 ++ tests/integration/test_aggregator.py | 1 + tests/integration/test_main.py | 10 ++++++++++ tests/integration/test_plugins.py | 1 + tests/unit/test_option_manager.py | 10 ++++++++-- tests/unit/test_options_config.py | 4 +++- 8 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index 13ece4e..5178abb 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -132,6 +132,7 @@ class Application: version=flake8.__version__, plugin_versions=self.plugins.versions_str(), parents=[self.prelim_arg_parser], + formatter_names=list(self.plugins.reporters), ) options.register_default_options(self.option_manager) self.option_manager.register_plugins(self.plugins) diff --git a/src/flake8/main/options.py b/src/flake8/main/options.py index 9b374ab..86a6cf8 100644 --- a/src/flake8/main/options.py +++ b/src/flake8/main/options.py @@ -220,7 +220,15 @@ def register_default_options(option_manager: OptionManager) -> None: metavar="format", default="default", parse_from_config=True, - help="Format errors according to the chosen formatter.", + help=( + f"Format errors according to the chosen formatter " + f"({', '.join(sorted(option_manager.formatter_names))}) " + f"or a format string containing %%-style " + f"mapping keys (code, col, path, row, text). " + f"For example, " + f"``--format=pylint`` or ``--format='%%(path)s %%(code)s'``. " + f"(Default: %(default)s)" + ), ) add_option( diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index e333c9e..7c40cb9 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -317,6 +317,7 @@ class OptionManager: version: str, plugin_versions: str, parents: list[argparse.ArgumentParser], + formatter_names: list[str], ) -> None: """Initialize an instance of an OptionManager. @@ -330,6 +331,7 @@ class OptionManager: A list of ArgumentParser objects whose arguments should also be included. """ + self.formatter_names = formatter_names self.parser = argparse.ArgumentParser( prog="flake8", usage="%(prog)s [options] file file ...", diff --git a/tests/integration/test_aggregator.py b/tests/integration/test_aggregator.py index a5b39d7..006ac5f 100644 --- a/tests/integration/test_aggregator.py +++ b/tests/integration/test_aggregator.py @@ -18,6 +18,7 @@ def optmanager(): version="3.0.0", plugin_versions="", parents=[], + formatter_names=[], ) options.register_default_options(option_manager) return option_manager diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index e711fb3..dfa0e0b 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -406,3 +406,13 @@ The specified config file does not exist: missing.cfg out, err = capsys.readouterr() assert out == expected assert err == "" + + +def test_format_option_help(capsys): + """Test that help displays list of available formatters.""" + with pytest.raises(SystemExit): + cli.main(["--help"]) + + out, err = capsys.readouterr() + assert "(default, pylint, quiet-filename, quiet-nothing)" in out + assert err == "" diff --git a/tests/integration/test_plugins.py b/tests/integration/test_plugins.py index 0b4424a..edba048 100644 --- a/tests/integration/test_plugins.py +++ b/tests/integration/test_plugins.py @@ -100,6 +100,7 @@ def test_local_plugin_can_add_option(local_config): version="123", plugin_versions="", parents=[stage1_parser], + formatter_names=[], ) register_default_options(option_manager) option_manager.register_plugins(loaded_plugins) diff --git a/tests/unit/test_option_manager.py b/tests/unit/test_option_manager.py index d5b88c3..3d3ddc1 100644 --- a/tests/unit/test_option_manager.py +++ b/tests/unit/test_option_manager.py @@ -17,7 +17,10 @@ TEST_VERSION = "3.0.0b1" def optmanager(): """Generate a simple OptionManager with default test arguments.""" return manager.OptionManager( - version=TEST_VERSION, plugin_versions="", parents=[] + version=TEST_VERSION, + plugin_versions="", + parents=[], + formatter_names=[], ) @@ -34,7 +37,10 @@ def test_option_manager_including_parent_options(): # WHEN optmanager = manager.OptionManager( - version=TEST_VERSION, plugin_versions="", parents=[parent_parser] + version=TEST_VERSION, + plugin_versions="", + parents=[parent_parser], + formatter_names=[], ) options = optmanager.parse_args(["--parent", "foo"]) diff --git a/tests/unit/test_options_config.py b/tests/unit/test_options_config.py index 0890ea9..8c8f0cb 100644 --- a/tests/unit/test_options_config.py +++ b/tests/unit/test_options_config.py @@ -168,7 +168,9 @@ def test_load_extra_config_utf8(tmpdir): @pytest.fixture def opt_manager(): - ret = OptionManager(version="123", plugin_versions="", parents=[]) + ret = OptionManager( + version="123", plugin_versions="", parents=[], formatter_names=[] + ) register_default_options(ret) return ret From 9b929478d19e9e9ae0235963a83485dfa0d937ca Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 18 Oct 2022 01:25:12 +0000 Subject: [PATCH 020/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/reorder_python_imports: v3.8.4 → v3.8.5](https://github.com/asottile/reorder_python_imports/compare/v3.8.4...v3.8.5) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7e05447..567bc7a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/reorder_python_imports - rev: v3.8.4 + rev: v3.8.5 hooks: - id: reorder-python-imports args: [ From 3ec725734559d384d5cbf59177740ba5c2773afc Mon Sep 17 00:00:00 2001 From: Bastien Gerard Date: Mon, 24 Oct 2022 22:17:51 +0200 Subject: [PATCH 021/131] remove docstring of OptionManager as typing is sufficient --- src/flake8/options/manager.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index 7c40cb9..8b4ec19 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -319,18 +319,7 @@ class OptionManager: parents: list[argparse.ArgumentParser], formatter_names: list[str], ) -> None: - """Initialize an instance of an OptionManager. - - :param prog: - Name of the actual program (e.g., flake8). - :param version: - Version string for the program. - :param usage: - Basic usage string used by the OptionParser. - :param parents: - A list of ArgumentParser objects whose arguments should also be - included. - """ + """Initialize an instance of an OptionManager.""" self.formatter_names = formatter_names self.parser = argparse.ArgumentParser( prog="flake8", From fba6df88f92c1c5f29c68c501737758a66594d06 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 26 Oct 2022 20:37:24 -0700 Subject: [PATCH 022/131] remove --diff option --- docs/source/internal/utils.rst | 8 -- docs/source/user/options.rst | 23 ----- src/flake8/api/legacy.py | 1 - src/flake8/checker.py | 10 -- src/flake8/discover_files.py | 25 ++--- src/flake8/main/application.py | 23 +---- src/flake8/main/options.py | 8 -- src/flake8/style_guide.py | 27 +----- src/flake8/utils.py | 67 ------------- src/flake8/violation.py | 33 ------- tests/fixtures/diffs/multi_file_diff | 130 -------------------------- tests/fixtures/diffs/single_file_diff | 27 ------ tests/fixtures/diffs/two_file_diff | 45 --------- tests/integration/test_main.py | 36 ------- tests/unit/test_checker_manager.py | 7 +- tests/unit/test_discover_files.py | 10 -- tests/unit/test_utils.py | 38 -------- tests/unit/test_violation.py | 19 ---- 18 files changed, 11 insertions(+), 526 deletions(-) delete mode 100644 tests/fixtures/diffs/multi_file_diff delete mode 100644 tests/fixtures/diffs/single_file_diff delete mode 100644 tests/fixtures/diffs/two_file_diff diff --git a/docs/source/internal/utils.rst b/docs/source/internal/utils.rst index c745917..dc53378 100644 --- a/docs/source/internal/utils.rst +++ b/docs/source/internal/utils.rst @@ -66,11 +66,3 @@ The standard library's :func:`fnmatch.fnmatch` is excellent at deciding if a filename matches a single pattern. In our use case, however, we typically have a list of patterns and want to know if the filename matches any of them. This function abstracts that logic away with a little extra logic. - -.. autofunction:: flake8.utils.parse_unified_diff - -To handle usage of :option:`flake8 --diff`, |Flake8| needs to be able -to parse the name of the files in the diff as well as the ranges indicated the -sections that have been changed. This function either accepts the diff as an -argument or reads the diff from standard-in. It then returns a dictionary with -filenames as the keys and sets of line numbers as the value. diff --git a/docs/source/user/options.rst b/docs/source/user/options.rst index 170c1e7..f9e8b86 100644 --- a/docs/source/user/options.rst +++ b/docs/source/user/options.rst @@ -44,8 +44,6 @@ Index of Options - :option:`flake8 --count` -- :option:`flake8 --diff` - - :option:`flake8 --exclude` - :option:`flake8 --filename` @@ -230,27 +228,6 @@ Options and their Descriptions count = True -.. option:: --diff - - :ref:`Go back to index ` - - .. warning:: - - Due to hiding potential errors, this option is deprecated and will be - removed in a future version. - - Use the unified diff provided on standard in to only check the modified - files and report errors included in the diff. - - Command-line example: - - .. prompt:: bash - - git diff -u | flake8 --diff - - This **can not** be specified in config files. - - .. option:: --exclude= :ref:`Go back to index ` diff --git a/src/flake8/api/legacy.py b/src/flake8/api/legacy.py index 9635756..ba4339e 100644 --- a/src/flake8/api/legacy.py +++ b/src/flake8/api/legacy.py @@ -136,7 +136,6 @@ class StyleGuide: stdin_display_name=self.options.stdin_display_name, filename_patterns=self.options.filename, exclude=self.options.exclude, - is_running_from_diff=self.options.diff, ) ) return not paths diff --git a/src/flake8/checker.py b/src/flake8/checker.py index d73349f..18b1ff6 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -97,8 +97,6 @@ class Manager: # implementation issues # - the user provided stdin and that's not something we can handle # well - # - we're processing a diff, which again does not work well with - # multiprocessing and which really shouldn't require multiprocessing # - the user provided some awful input # class state is only preserved when using the `fork` strategy. @@ -116,13 +114,6 @@ class Manager: ) return 0 - if self.options.diff: - LOG.warning( - "The --diff option was specified with --jobs but " - "they are not compatible. Ignoring --jobs arguments." - ) - return 0 - jobs = self.options.jobs # If the value is "auto", we want to let the multiprocessing library @@ -169,7 +160,6 @@ class Manager: stdin_display_name=self.options.stdin_display_name, filename_patterns=self.options.filename, exclude=self.exclude, - is_running_from_diff=self.options.diff, ) ] self.checkers = [c for c in self._all_checkers if c.should_process] diff --git a/src/flake8/discover_files.py b/src/flake8/discover_files.py index b8592c8..580d5fd 100644 --- a/src/flake8/discover_files.py +++ b/src/flake8/discover_files.py @@ -55,7 +55,6 @@ def expand_paths( stdin_display_name: str, filename_patterns: Sequence[str], exclude: Sequence[str], - is_running_from_diff: bool, ) -> Generator[str, None, None]: """Expand out ``paths`` from commandline to the lintable files.""" if not paths: @@ -75,24 +74,16 @@ def expand_paths( logger=LOG, ) - def is_included(arg: str, fname: str) -> bool: - # while running from a diff, the arguments aren't _explicitly_ - # listed so we still filter them - if is_running_from_diff: - return utils.fnmatch(fname, filename_patterns) - else: - return ( - # always lint `-` - fname == "-" - # always lint explicitly passed (even if not matching filter) - or arg == fname - # otherwise, check the file against filtered patterns - or utils.fnmatch(fname, filename_patterns) - ) - return ( filename for path in paths for filename in _filenames_from(path, predicate=is_excluded) - if is_included(path, filename) + if ( + # always lint `-` + filename == "-" + # always lint explicitly passed (even if not matching filter) + or path == filename + # otherwise, check the file against filtered patterns + or utils.fnmatch(filename, filename_patterns) + ) ) diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index 5178abb..c76fb86 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -13,7 +13,6 @@ from flake8 import checker from flake8 import defaults from flake8 import exceptions from flake8 import style_guide -from flake8 import utils from flake8.formatting.base import BaseFormatter from flake8.main import debug from flake8.main import options @@ -66,9 +65,6 @@ class Application: #: with a non-zero status code self.catastrophic_failure = False - #: The parsed diff information - self.parsed_diff: dict[str, set[int]] = {} - def parse_preliminary_options( self, argv: Sequence[str] ) -> tuple[argparse.Namespace, list[str]]: @@ -158,13 +154,6 @@ class Application: print(json.dumps(info, indent=2, sort_keys=True)) raise SystemExit(0) - if self.options.diff: - LOG.warning( - "the --diff option is deprecated and will be removed in a " - "future version." - ) - self.parsed_diff = utils.parse_unified_diff() - for loaded in self.plugins.all_plugins(): parse_options = getattr(loaded.obj, "parse_options", None) if parse_options is None: @@ -194,9 +183,6 @@ class Application: self.options, self.formatter ) - if self.options.diff: - self.guide.add_diff_ranges(self.parsed_diff) - def make_file_checker_manager(self) -> None: """Initialize our FileChecker Manager.""" assert self.guide is not None @@ -213,16 +199,9 @@ class Application: :class:`~flake8.checker.Manger` instance run the checks it is managing. """ - assert self.options is not None assert self.file_checker_manager is not None - if self.options.diff: - files: list[str] | None = sorted(self.parsed_diff) - if not files: - return - else: - files = None - self.file_checker_manager.start(files) + self.file_checker_manager.start() try: self.file_checker_manager.run() except exceptions.PluginExecutionFailed as plugin_failed: diff --git a/src/flake8/main/options.py b/src/flake8/main/options.py index 86a6cf8..891bae9 100644 --- a/src/flake8/main/options.py +++ b/src/flake8/main/options.py @@ -114,7 +114,6 @@ def register_default_options(option_manager: OptionManager) -> None: - ``-q``/``--quiet`` - ``--color`` - ``--count`` - - ``--diff`` - ``--exclude`` - ``--extend-exclude`` - ``--filename`` @@ -163,13 +162,6 @@ def register_default_options(option_manager: OptionManager) -> None: "all other output.", ) - add_option( - "--diff", - action="store_true", - help="(DEPRECATED) Report changes only within line number ranges in " - "the unified diff provided on standard in by the user.", - ) - add_option( "--exclude", metavar="patterns", diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index 2fee0f3..94698f8 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -298,18 +298,6 @@ class StyleGuideManager: code, filename, line_number, column_number, text, physical_line ) - def add_diff_ranges(self, diffinfo: dict[str, set[int]]) -> None: - """Update the StyleGuides to filter out information not in the diff. - - This provides information to the underlying StyleGuides so that only - the errors in the line number ranges are reported. - - :param diffinfo: - Dictionary mapping filenames to sets of line number ranges. - """ - for guide in self.style_guides: - guide.add_diff_ranges(diffinfo) - class StyleGuide: """Manage a Flake8 user's style guide.""" @@ -333,7 +321,6 @@ class StyleGuide: self.filename = filename if self.filename: self.filename = utils.normalize_path(self.filename) - self._parsed_diff: dict[str, set[int]] = {} def __repr__(self) -> str: """Make it easier to debug which StyleGuide we're using.""" @@ -440,20 +427,8 @@ class StyleGuide: self.should_report_error(error.code) is Decision.Selected ) is_not_inline_ignored = error.is_inline_ignored(disable_noqa) is False - is_included_in_diff = error.is_in(self._parsed_diff) - if error_is_selected and is_not_inline_ignored and is_included_in_diff: + if error_is_selected and is_not_inline_ignored: self.formatter.handle(error) self.stats.record(error) return 1 return 0 - - def add_diff_ranges(self, diffinfo: dict[str, set[int]]) -> None: - """Update the StyleGuide to filter out information not in the diff. - - This provides information to the StyleGuide so that only the errors - in the line number ranges are reported. - - :param diffinfo: - Dictionary mapping filenames to sets of line number ranges. - """ - self._parsed_diff = diffinfo diff --git a/src/flake8/utils.py b/src/flake8/utils.py index 60555a9..afc3896 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -1,7 +1,6 @@ """Utility methods for flake8.""" from __future__ import annotations -import collections import fnmatch as _fnmatch import functools import io @@ -18,7 +17,6 @@ from typing import Sequence from flake8 import exceptions -DIFF_HUNK_REGEXP = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$") COMMA_SEPARATED_LIST_RE = re.compile(r"[,\s]") LOCAL_PLUGIN_LIST_RE = re.compile(r"[,\t\n\r\f\v]") NORMALIZE_PACKAGE_NAME_RE = re.compile(r"[-_.]+") @@ -202,71 +200,6 @@ def stdin_get_lines() -> list[str]: return list(io.StringIO(stdin_get_value())) -def parse_unified_diff(diff: str | None = None) -> dict[str, set[int]]: - """Parse the unified diff passed on stdin. - - :returns: - dictionary mapping file names to sets of line numbers - """ - # Allow us to not have to patch out stdin_get_value - if diff is None: - diff = stdin_get_value() - - number_of_rows = None - current_path = None - parsed_paths: dict[str, set[int]] = collections.defaultdict(set) - for line in diff.splitlines(): - if number_of_rows: - if not line or line[0] != "-": - number_of_rows -= 1 - # We're in the part of the diff that has lines starting with +, -, - # and ' ' to show context and the changes made. We skip these - # because the information we care about is the filename and the - # range within it. - # When number_of_rows reaches 0, we will once again start - # searching for filenames and ranges. - continue - - # NOTE(sigmavirus24): Diffs that we support look roughly like: - # diff a/file.py b/file.py - # ... - # --- a/file.py - # +++ b/file.py - # Below we're looking for that last line. Every diff tool that - # gives us this output may have additional information after - # ``b/file.py`` which it will separate with a \t, e.g., - # +++ b/file.py\t100644 - # Which is an example that has the new file permissions/mode. - # In this case we only care about the file name. - if line[:3] == "+++": - current_path = line[4:].split("\t", 1)[0] - # NOTE(sigmavirus24): This check is for diff output from git. - if current_path[:2] == "b/": - current_path = current_path[2:] - # We don't need to do anything else. We have set up our local - # ``current_path`` variable. We can skip the rest of this loop. - # The next line we will see will give us the hung information - # which is in the next section of logic. - continue - - hunk_match = DIFF_HUNK_REGEXP.match(line) - # NOTE(sigmavirus24): pep8/pycodestyle check for: - # line[:3] == '@@ ' - # But the DIFF_HUNK_REGEXP enforces that the line start with that - # So we can more simply check for a match instead of slicing and - # comparing. - if hunk_match: - (row, number_of_rows) = ( - 1 if not group else int(group) for group in hunk_match.groups() - ) - assert current_path is not None - parsed_paths[current_path].update(range(row, row + number_of_rows)) - - # We have now parsed our diff into a dictionary that looks like: - # {'file.py': set(range(10, 16), range(18, 20)), ...} - return parsed_paths - - def is_using_stdin(paths: list[str]) -> bool: """Determine if we're going to read from stdin. diff --git a/src/flake8/violation.py b/src/flake8/violation.py index 45834b2..96161d4 100644 --- a/src/flake8/violation.py +++ b/src/flake8/violation.py @@ -67,36 +67,3 @@ class Violation(NamedTuple): "%r is not ignored inline with ``# noqa: %s``", self, codes_str ) return False - - def is_in(self, diff: dict[str, set[int]]) -> bool: - """Determine if the violation is included in a diff's line ranges. - - This function relies on the parsed data added via - :meth:`~StyleGuide.add_diff_ranges`. If that has not been called and - we are not evaluating files in a diff, then this will always return - True. If there are diff ranges, then this will return True if the - line number in the error falls inside one of the ranges for the file - (and assuming the file is part of the diff data). If there are diff - ranges, this will return False if the file is not part of the diff - data or the line number of the error is not in any of the ranges of - the diff. - - :returns: - True if there is no diff or if the error is in the diff's line - number ranges. False if the error's line number falls outside - the diff's line number ranges. - """ - if not diff: - return True - - # NOTE(sigmavirus24): The parsed diff will be a defaultdict with - # a set as the default value (if we have received it from - # flake8.utils.parse_unified_diff). In that case ranges below - # could be an empty set (which is False-y) or if someone else - # is using this API, it could be None. If we could guarantee one - # or the other, we would check for it more explicitly. - line_numbers = diff.get(self.filename) - if not line_numbers: - return False - - return self.line_number in line_numbers diff --git a/tests/fixtures/diffs/multi_file_diff b/tests/fixtures/diffs/multi_file_diff deleted file mode 100644 index de86209..0000000 --- a/tests/fixtures/diffs/multi_file_diff +++ /dev/null @@ -1,130 +0,0 @@ -diff --git a/flake8/utils.py b/flake8/utils.py -index f6ce384..7cd12b0 100644 ---- a/flake8/utils.py -+++ b/flake8/utils.py -@@ -75,8 +75,8 @@ def stdin_get_value(): - return cached_value.getvalue() - - --def parse_unified_diff(): -- # type: () -> List[str] -+def parse_unified_diff(diff=None): -+ # type: (str) -> List[str] - """Parse the unified diff passed on stdin. - - :returns: -@@ -84,7 +84,10 @@ def parse_unified_diff(): - :rtype: - dict - """ -- diff = stdin_get_value() -+ # Allow us to not have to patch out stdin_get_value -+ if diff is None: -+ diff = stdin_get_value() -+ - number_of_rows = None - current_path = None - parsed_paths = collections.defaultdict(set) -diff --git a/tests/fixtures/diffs/single_file_diff b/tests/fixtures/diffs/single_file_diff -new file mode 100644 -index 0000000..77ca534 ---- /dev/null -+++ b/tests/fixtures/diffs/single_file_diff -@@ -0,0 +1,27 @@ -+diff --git a/flake8/utils.py b/flake8/utils.py -+index f6ce384..7cd12b0 100644 -+--- a/flake8/utils.py -++++ b/flake8/utils.py -+@@ -75,8 +75,8 @@ def stdin_get_value(): -+ return cached_value.getvalue() -+ -+ -+-def parse_unified_diff(): -+- # type: () -> List[str] -++def parse_unified_diff(diff=None): -++ # type: (str) -> List[str] -+ """Parse the unified diff passed on stdin. -+ -+ :returns: -+@@ -84,7 +84,10 @@ def parse_unified_diff(): -+ :rtype: -+ dict -+ """ -+- diff = stdin_get_value() -++ # Allow us to not have to patch out stdin_get_value -++ if diff is None: -++ diff = stdin_get_value() -++ -+ number_of_rows = None -+ current_path = None -+ parsed_paths = collections.defaultdict(set) -diff --git a/tests/fixtures/diffs/two_file_diff b/tests/fixtures/diffs/two_file_diff -new file mode 100644 -index 0000000..5bd35cd ---- /dev/null -+++ b/tests/fixtures/diffs/two_file_diff -@@ -0,0 +1,45 @@ -+diff --git a/flake8/utils.py b/flake8/utils.py -+index f6ce384..7cd12b0 100644 -+--- a/flake8/utils.py -++++ b/flake8/utils.py -+@@ -75,8 +75,8 @@ def stdin_get_value(): -+ return cached_value.getvalue() -+ -+ -+-def parse_unified_diff(): -+- # type: () -> List[str] -++def parse_unified_diff(diff=None): -++ # type: (str) -> List[str] -+ """Parse the unified diff passed on stdin. -+ -+ :returns: -+@@ -84,7 +84,10 @@ def parse_unified_diff(): -+ :rtype: -+ dict -+ """ -+- diff = stdin_get_value() -++ # Allow us to not have to patch out stdin_get_value -++ if diff is None: -++ diff = stdin_get_value() -++ -+ number_of_rows = None -+ current_path = None -+ parsed_paths = collections.defaultdict(set) -+diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py -+index d69d939..21482ce 100644 -+--- a/tests/unit/test_utils.py -++++ b/tests/unit/test_utils.py -+@@ -115,3 +115,13 @@ def test_parameters_for_function_plugin(): -+ plugin = plugin_manager.Plugin('plugin-name', object()) -+ plugin._plugin = fake_plugin -+ assert utils.parameters_for(plugin) == ['physical_line', 'self', 'tree'] -++ -++ -++def read_diff_file(filename): -++ """Read the diff file in its entirety.""" -++ with open(filename, 'r') as fd: -++ content = fd.read() -++ return content -++ -++ -++SINGLE_FILE_DIFF = read_diff_file('tests/fixtures/diffs/single_file_diff') -diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py -index d69d939..1461369 100644 ---- a/tests/unit/test_utils.py -+++ b/tests/unit/test_utils.py -@@ -115,3 +115,14 @@ def test_parameters_for_function_plugin(): - plugin = plugin_manager.Plugin('plugin-name', object()) - plugin._plugin = fake_plugin - assert utils.parameters_for(plugin) == ['physical_line', 'self', 'tree'] -+ -+ -+def read_diff_file(filename): -+ """Read the diff file in its entirety.""" -+ with open(filename, 'r') as fd: -+ content = fd.read() -+ return content -+ -+ -+SINGLE_FILE_DIFF = read_diff_file('tests/fixtures/diffs/single_file_diff') -+TWO_FILE_DIFF = read_diff_file('tests/fixtures/diffs/two_file_diff') diff --git a/tests/fixtures/diffs/single_file_diff b/tests/fixtures/diffs/single_file_diff deleted file mode 100644 index 77ca534..0000000 --- a/tests/fixtures/diffs/single_file_diff +++ /dev/null @@ -1,27 +0,0 @@ -diff --git a/flake8/utils.py b/flake8/utils.py -index f6ce384..7cd12b0 100644 ---- a/flake8/utils.py -+++ b/flake8/utils.py -@@ -75,8 +75,8 @@ def stdin_get_value(): - return cached_value.getvalue() - - --def parse_unified_diff(): -- # type: () -> List[str] -+def parse_unified_diff(diff=None): -+ # type: (str) -> List[str] - """Parse the unified diff passed on stdin. - - :returns: -@@ -84,7 +84,10 @@ def parse_unified_diff(): - :rtype: - dict - """ -- diff = stdin_get_value() -+ # Allow us to not have to patch out stdin_get_value -+ if diff is None: -+ diff = stdin_get_value() -+ - number_of_rows = None - current_path = None - parsed_paths = collections.defaultdict(set) diff --git a/tests/fixtures/diffs/two_file_diff b/tests/fixtures/diffs/two_file_diff deleted file mode 100644 index 5bd35cd..0000000 --- a/tests/fixtures/diffs/two_file_diff +++ /dev/null @@ -1,45 +0,0 @@ -diff --git a/flake8/utils.py b/flake8/utils.py -index f6ce384..7cd12b0 100644 ---- a/flake8/utils.py -+++ b/flake8/utils.py -@@ -75,8 +75,8 @@ def stdin_get_value(): - return cached_value.getvalue() - - --def parse_unified_diff(): -- # type: () -> List[str] -+def parse_unified_diff(diff=None): -+ # type: (str) -> List[str] - """Parse the unified diff passed on stdin. - - :returns: -@@ -84,7 +84,10 @@ def parse_unified_diff(): - :rtype: - dict - """ -- diff = stdin_get_value() -+ # Allow us to not have to patch out stdin_get_value -+ if diff is None: -+ diff = stdin_get_value() -+ - number_of_rows = None - current_path = None - parsed_paths = collections.defaultdict(set) -diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py -index d69d939..21482ce 100644 ---- a/tests/unit/test_utils.py -+++ b/tests/unit/test_utils.py -@@ -115,3 +115,13 @@ def test_parameters_for_function_plugin(): - plugin = plugin_manager.Plugin('plugin-name', object()) - plugin._plugin = fake_plugin - assert utils.parameters_for(plugin) == ['physical_line', 'self', 'tree'] -+ -+ -+def read_diff_file(filename): -+ """Read the diff file in its entirety.""" -+ with open(filename, 'r') as fd: -+ content = fd.read() -+ return content -+ -+ -+SINGLE_FILE_DIFF = read_diff_file('tests/fixtures/diffs/single_file_diff') diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index dfa0e0b..db60f6b 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -13,42 +13,6 @@ from flake8.main import cli from flake8.options import config -def test_diff_option(tmpdir, capsys): - """Ensure that `flake8 --diff` works.""" - t_py_contents = """\ -import os -import sys # unused but not part of diff - -print('(to avoid trailing whitespace in test)') -print('(to avoid trailing whitespace in test)') -print(os.path.join('foo', 'bar')) - -y # part of the diff and an error -""" - - diff = """\ -diff --git a/t.py b/t.py -index d64ac39..7d943de 100644 ---- a/t.py -+++ b/t.py -@@ -4,3 +4,5 @@ import sys # unused but not part of diff - print('(to avoid trailing whitespace in test)') - print('(to avoid trailing whitespace in test)') - print(os.path.join('foo', 'bar')) -+ -+y # part of the diff and an error -""" - - with mock.patch.object(utils, "stdin_get_value", return_value=diff): - with tmpdir.as_cwd(): - tmpdir.join("t.py").write(t_py_contents) - assert cli.main(["--diff"]) == 1 - - out, err = capsys.readouterr() - assert out == "t.py:8:1: F821 undefined name 'y'\n" - assert err == "" - - def test_form_feed_line_split(tmpdir, capsys): """Test that form feed is treated the same for stdin.""" src = "x=1\n\f\ny=1\n" diff --git a/tests/unit/test_checker_manager.py b/tests/unit/test_checker_manager.py index 32bc9a4..b75bf6d 100644 --- a/tests/unit/test_checker_manager.py +++ b/tests/unit/test_checker_manager.py @@ -14,12 +14,7 @@ from flake8.plugins import finder def style_guide_mock(): """Create a mock StyleGuide object.""" - return mock.MagicMock( - **{ - "options.diff": False, - "options.jobs": JobsArgument("4"), - } - ) + return mock.MagicMock(**{"options.jobs": JobsArgument("4")}) def _parallel_checker_manager(): diff --git a/tests/unit/test_discover_files.py b/tests/unit/test_discover_files.py index edf047d..ca945c2 100644 --- a/tests/unit/test_discover_files.py +++ b/tests/unit/test_discover_files.py @@ -125,7 +125,6 @@ def _expand_paths( stdin_display_name="stdin", filename_patterns=("*.py",), exclude=(), - is_running_from_diff=False, ): return set( expand_paths( @@ -133,7 +132,6 @@ def _expand_paths( stdin_display_name=stdin_display_name, filename_patterns=filename_patterns, exclude=exclude, - is_running_from_diff=is_running_from_diff, ) ) @@ -166,11 +164,3 @@ def test_alternate_stdin_name_is_filtered(): def test_filename_included_even_if_not_matching_include(tmp_path): some_file = str(tmp_path.joinpath("some/file")) assert _expand_paths(paths=(some_file,)) == {some_file} - - -def test_diff_filenames_filtered_by_patterns(tmp_path): - f1 = str(tmp_path.joinpath("f1")) - f2 = str(tmp_path.joinpath("f2.py")) - - ret = _expand_paths(paths=(f1, f2), is_running_from_diff=True) - assert ret == {f2} diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 98a5a5d..82eef63 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -183,44 +183,6 @@ def test_fnmatch(filename, patterns, expected): assert utils.fnmatch(filename, patterns) is expected -def read_diff_file(filename): - """Read the diff file in its entirety.""" - with open(filename) as fd: - content = fd.read() - return content - - -SINGLE_FILE_DIFF = read_diff_file("tests/fixtures/diffs/single_file_diff") -SINGLE_FILE_INFO = { - "flake8/utils.py": set(range(75, 83)).union(set(range(84, 94))), -} -TWO_FILE_DIFF = read_diff_file("tests/fixtures/diffs/two_file_diff") -TWO_FILE_INFO = { - "flake8/utils.py": set(range(75, 83)).union(set(range(84, 94))), - "tests/unit/test_utils.py": set(range(115, 128)), -} -MULTI_FILE_DIFF = read_diff_file("tests/fixtures/diffs/multi_file_diff") -MULTI_FILE_INFO = { - "flake8/utils.py": set(range(75, 83)).union(set(range(84, 94))), - "tests/unit/test_utils.py": set(range(115, 129)), - "tests/fixtures/diffs/single_file_diff": set(range(1, 28)), - "tests/fixtures/diffs/two_file_diff": set(range(1, 46)), -} - - -@pytest.mark.parametrize( - "diff, parsed_diff", - [ - (SINGLE_FILE_DIFF, SINGLE_FILE_INFO), - (TWO_FILE_DIFF, TWO_FILE_INFO), - (MULTI_FILE_DIFF, MULTI_FILE_INFO), - ], -) -def test_parse_unified_diff(diff, parsed_diff): - """Verify that what we parse from a diff matches expectations.""" - assert utils.parse_unified_diff(diff) == parsed_diff - - def test_stdin_get_value_crlf(): """Ensure that stdin is normalized from crlf to lf.""" stdin = io.TextIOWrapper(io.BytesIO(b"1\r\n2\r\n"), "UTF-8") diff --git a/tests/unit/test_violation.py b/tests/unit/test_violation.py index a4a43da..1b4852b 100644 --- a/tests/unit/test_violation.py +++ b/tests/unit/test_violation.py @@ -51,22 +51,3 @@ def test_disable_is_inline_ignored(): assert error.is_inline_ignored(True) is False assert getline.called is False - - -@pytest.mark.parametrize( - "violation_file,violation_line,diff,expected", - [ - ("file.py", 10, {}, True), - ("file.py", 1, {"file.py": range(1, 2)}, True), - ("file.py", 10, {"file.py": range(1, 2)}, False), - ("file.py", 1, {"other.py": range(1, 2)}, False), - ("file.py", 10, {"other.py": range(1, 2)}, False), - ], -) -def test_violation_is_in_diff(violation_file, violation_line, diff, expected): - """Verify that we find violations within a diff.""" - violation = Violation( - "E001", violation_file, violation_line, 1, "warning", "line" - ) - - assert violation.is_in(diff) is expected From 1745fd4a88f463764b38e7f05b31ec2a64e0b7a7 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 26 Oct 2022 20:56:54 -0700 Subject: [PATCH 023/131] replace some unnecessary itertools.chain with *splat --- src/flake8/checker.py | 5 +---- src/flake8/style_guide.py | 11 ++++------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 18b1ff6..522d650 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -4,7 +4,6 @@ from __future__ import annotations import argparse import collections import errno -import itertools import logging import multiprocessing.pool import signal @@ -80,9 +79,7 @@ class Manager: "physical lines": 0, "tokens": 0, } - self.exclude = tuple( - itertools.chain(self.options.exclude, self.options.extend_exclude) - ) + self.exclude = (*self.options.exclude, *self.options.extend_exclude) def _process_statistics(self) -> None: for checker in self.checkers: diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index 94698f8..8431650 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -6,7 +6,6 @@ import contextlib import copy import enum import functools -import itertools import logging from typing import Generator from typing import Sequence @@ -221,12 +220,10 @@ class StyleGuideManager: self.default_style_guide = StyleGuide( options, formatter, self.stats, decider=decider ) - self.style_guides = list( - itertools.chain( - [self.default_style_guide], - self.populate_style_guides_with(options), - ) - ) + self.style_guides = [ + self.default_style_guide, + *self.populate_style_guides_with(options), + ] self.style_guide_for = functools.lru_cache(maxsize=None)( self._style_guide_for From 8b1e7c696fa17dd23f025c1657fb97784ca859b4 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 26 Oct 2022 21:27:18 -0700 Subject: [PATCH 024/131] remove impossible None check --- src/flake8/processor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 644192d..77f3d5a 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -326,7 +326,7 @@ class FileProcessor: def read_lines(self) -> list[str]: """Read the lines for this file checker.""" - if self.filename is None or self.filename == "-": + if self.filename == "-": self.filename = self.options.stdin_display_name or "stdin" lines = self.read_lines_from_stdin() else: From 0d667a73299971f1cf8ff549c519fffb282b1faf Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 26 Oct 2022 20:17:35 -0700 Subject: [PATCH 025/131] enable multiprocessing on other platforms --- src/flake8/api/legacy.py | 25 +---- src/flake8/checker.py | 171 +++++++++++++---------------- src/flake8/main/application.py | 133 ++-------------------- src/flake8/options/parse_args.py | 70 ++++++++++++ tests/integration/test_checker.py | 17 +-- tests/unit/test_checker_manager.py | 31 ++---- tests/unit/test_legacy_api.py | 45 -------- 7 files changed, 175 insertions(+), 317 deletions(-) create mode 100644 src/flake8/options/parse_args.py diff --git a/src/flake8/api/legacy.py b/src/flake8/api/legacy.py index ba4339e..446df29 100644 --- a/src/flake8/api/legacy.py +++ b/src/flake8/api/legacy.py @@ -10,11 +10,10 @@ import logging import os.path from typing import Any -import flake8 from flake8.discover_files import expand_paths from flake8.formatting import base as formatter from flake8.main import application as app -from flake8.options import config +from flake8.options.parse_args import parse_args LOG = logging.getLogger(__name__) @@ -163,7 +162,7 @@ class StyleGuide: # Stop cringing... I know it's gross. self._application.make_guide() self._application.file_checker_manager = None - self._application.make_file_checker_manager() + self._application.make_file_checker_manager([]) def input_file( self, @@ -200,23 +199,7 @@ def get_style_guide(**kwargs: Any) -> StyleGuide: An initialized StyleGuide """ application = app.Application() - prelim_opts, remaining_args = application.parse_preliminary_options([]) - flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file) - - cfg, cfg_dir = config.load_config( - config=prelim_opts.config, - extra=prelim_opts.append_config, - isolated=prelim_opts.isolated, - ) - - application.find_plugins( - cfg, - cfg_dir, - enable_extensions=prelim_opts.enable_extensions, - require_plugins=prelim_opts.require_plugins, - ) - application.register_plugin_options() - application.parse_configuration_and_cli(cfg, cfg_dir, remaining_args) + application.plugins, application.options = parse_args([]) # We basically want application.initialize to be called but with these # options set instead before we make our formatter, notifier, internal # style guide and file checker manager. @@ -229,5 +212,5 @@ def get_style_guide(**kwargs: Any) -> StyleGuide: LOG.error('Could not update option "%s"', key) application.make_formatter() application.make_guide() - application.make_file_checker_manager() + application.make_file_checker_manager([]) return StyleGuide(application) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 522d650..2d7bcd9 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -2,15 +2,17 @@ from __future__ import annotations import argparse -import collections +import contextlib import errno import logging import multiprocessing.pool import signal import tokenize from typing import Any +from typing import Generator from typing import List from typing import Optional +from typing import Sequence from typing import Tuple from flake8 import defaults @@ -18,6 +20,7 @@ from flake8 import exceptions from flake8 import processor from flake8 import utils from flake8.discover_files import expand_paths +from flake8.options.parse_args import parse_args from flake8.plugins.finder import Checkers from flake8.plugins.finder import LoadedPlugin from flake8.style_guide import StyleGuideManager @@ -41,6 +44,41 @@ SERIAL_RETRY_ERRNOS = { # noise in diffs. } +_mp_plugins: Checkers +_mp_options: argparse.Namespace + + +@contextlib.contextmanager +def _mp_prefork( + plugins: Checkers, options: argparse.Namespace +) -> Generator[None, None, None]: + # we can save significant startup work w/ `fork` multiprocessing + global _mp_plugins, _mp_options + _mp_plugins, _mp_options = plugins, options + try: + yield + finally: + del _mp_plugins, _mp_options + + +def _mp_init(argv: Sequence[str]) -> None: + global _mp_plugins, _mp_options + + # Ensure correct signaling of ^C using multiprocessing.Pool. + signal.signal(signal.SIGINT, signal.SIG_IGN) + + try: + _mp_plugins, _mp_options # for `fork` this'll already be set + except NameError: + plugins, options = parse_args(argv) + _mp_plugins, _mp_options = plugins.checkers, options + + +def _mp_run(filename: str) -> tuple[str, Results, dict[str, int]]: + return FileChecker( + filename=filename, plugins=_mp_plugins, options=_mp_options + ).run_checks() + class Manager: """Manage the parallelism and checker instances for each plugin and file. @@ -65,14 +103,13 @@ class Manager: self, style_guide: StyleGuideManager, plugins: Checkers, + argv: Sequence[str], ) -> None: """Initialize our Manager instance.""" self.style_guide = style_guide self.options = style_guide.options self.plugins = plugins self.jobs = self._job_count() - self._all_checkers: list[FileChecker] = [] - self.checkers: list[FileChecker] = [] self.statistics = { "files": 0, "logical lines": 0, @@ -80,30 +117,22 @@ class Manager: "tokens": 0, } self.exclude = (*self.options.exclude, *self.options.extend_exclude) + self.argv = argv + self.results: list[tuple[str, Results, dict[str, int]]] = [] def _process_statistics(self) -> None: - for checker in self.checkers: + for _, _, statistics in self.results: for statistic in defaults.STATISTIC_NAMES: - self.statistics[statistic] += checker.statistics[statistic] - self.statistics["files"] += len(self.checkers) + self.statistics[statistic] += statistics[statistic] + self.statistics["files"] += len(self.filenames) def _job_count(self) -> int: # First we walk through all of our error cases: # - multiprocessing library is not present - # - we're running on windows in which case we know we have significant - # implementation issues # - the user provided stdin and that's not something we can handle # well # - the user provided some awful input - # class state is only preserved when using the `fork` strategy. - if multiprocessing.get_start_method() != "fork": - LOG.warning( - "The multiprocessing module is not available. " - "Ignoring --jobs arguments." - ) - return 0 - if utils.is_using_stdin(self.options.filenames): LOG.warning( "The --jobs option is not compatible with supplying " @@ -141,27 +170,6 @@ class Manager: ) return reported_results_count - def make_checkers(self, paths: list[str] | None = None) -> None: - """Create checkers for each file.""" - if paths is None: - paths = self.options.filenames - - self._all_checkers = [ - FileChecker( - filename=filename, - plugins=self.plugins, - options=self.options, - ) - for filename in expand_paths( - paths=paths, - stdin_display_name=self.options.stdin_display_name, - filename_patterns=self.options.filename, - exclude=self.exclude, - ) - ] - self.checkers = [c for c in self._all_checkers if c.should_process] - LOG.info("Checking %d files", len(self.checkers)) - def report(self) -> tuple[int, int]: """Report all of the errors found in the managed file checkers. @@ -172,9 +180,8 @@ class Manager: A tuple of the total results found and the results reported. """ results_reported = results_found = 0 - for checker in self._all_checkers: - results = sorted(checker.results, key=lambda tup: (tup[1], tup[2])) - filename = checker.display_name + for filename, results, _ in self.results: + results.sort(key=lambda tup: (tup[1], tup[2])) with self.style_guide.processing_file(filename): results_reported += self._handle_results(filename, results) results_found += len(results) @@ -182,12 +189,8 @@ class Manager: def run_parallel(self) -> None: """Run the checkers in parallel.""" - # fmt: off - final_results: dict[str, list[tuple[str, int, int, str, str | None]]] = collections.defaultdict(list) # noqa: E501 - final_statistics: dict[str, dict[str, int]] = collections.defaultdict(dict) # noqa: E501 - # fmt: on - - pool = _try_initialize_processpool(self.jobs) + with _mp_prefork(self.plugins, self.options): + pool = _try_initialize_processpool(self.jobs, self.argv) if pool is None: self.run_serial() @@ -195,17 +198,7 @@ class Manager: pool_closed = False try: - pool_map = pool.imap_unordered( - _run_checks, - self.checkers, - chunksize=calculate_pool_chunksize( - len(self.checkers), self.jobs - ), - ) - for ret in pool_map: - filename, results, statistics = ret - final_results[filename] = results - final_statistics[filename] = statistics + self.results = list(pool.imap_unordered(_mp_run, self.filenames)) pool.close() pool.join() pool_closed = True @@ -214,15 +207,16 @@ class Manager: pool.terminate() pool.join() - for checker in self.checkers: - filename = checker.display_name - checker.results = final_results[filename] - checker.statistics = final_statistics[filename] - def run_serial(self) -> None: """Run the checkers in serial.""" - for checker in self.checkers: - checker.run_checks() + self.results = [ + FileChecker( + filename=filename, + plugins=self.plugins, + options=self.options, + ).run_checks() + for filename in self.filenames + ] def run(self) -> None: """Run all the checkers. @@ -234,7 +228,7 @@ class Manager: :issue:`117`) this also implements fallback to serial processing. """ try: - if self.jobs > 1 and len(self.checkers) > 1: + if self.jobs > 1 and len(self.filenames) > 1: self.run_parallel() else: self.run_serial() @@ -242,7 +236,7 @@ class Manager: LOG.warning("Flake8 was interrupted by the user") raise exceptions.EarlyQuit("Early quit while running checks") - def start(self, paths: list[str] | None = None) -> None: + def start(self) -> None: """Start checking files. :param paths: @@ -250,7 +244,14 @@ class Manager: :meth:`~Manager.make_checkers`. """ LOG.info("Making checkers") - self.make_checkers(paths) + self.filenames = tuple( + expand_paths( + paths=self.options.filenames, + stdin_display_name=self.options.stdin_display_name, + filename_patterns=self.options.filename, + exclude=self.exclude, + ) + ) def stop(self) -> None: """Stop checking files.""" @@ -325,7 +326,7 @@ class FileChecker: def run_check(self, plugin: LoadedPlugin, **arguments: Any) -> Any: """Run the check in a single plugin.""" - assert self.processor is not None + assert self.processor is not None, self.filename try: params = self.processor.keyword_arguments_for( plugin.parameters, arguments @@ -409,7 +410,7 @@ class FileChecker: def run_ast_checks(self) -> None: """Run all checks expecting an abstract syntax tree.""" - assert self.processor is not None + assert self.processor is not None, self.filename ast = self.processor.build_ast() for plugin in self.plugins.tree: @@ -514,7 +515,9 @@ class FileChecker: def run_checks(self) -> tuple[str, Results, dict[str, int]]: """Run checks against the file.""" - assert self.processor is not None + if self.processor is None or not self.should_process: + return self.display_name, self.results, self.statistics + try: self.run_ast_checks() self.process_tokens() @@ -522,11 +525,11 @@ class FileChecker: code = "E902" if isinstance(e, tokenize.TokenError) else "E999" row, column = self._extract_syntax_information(e) self.report(code, row, column, f"{type(e).__name__}: {e.args[0]}") - return self.filename, self.results, self.statistics + return self.display_name, self.results, self.statistics logical_lines = self.processor.statistics["logical lines"] self.statistics["logical lines"] = logical_lines - return self.filename, self.results, self.statistics + return self.display_name, self.results, self.statistics def handle_newline(self, token_type: int) -> None: """Handle the logic when encountering a newline token.""" @@ -573,17 +576,13 @@ class FileChecker: self.run_physical_checks(line) -def _pool_init() -> None: - """Ensure correct signaling of ^C using multiprocessing.Pool.""" - signal.signal(signal.SIGINT, signal.SIG_IGN) - - def _try_initialize_processpool( job_count: int, + argv: Sequence[str], ) -> multiprocessing.pool.Pool | None: """Return a new process pool instance if we are able to create one.""" try: - return multiprocessing.Pool(job_count, _pool_init) + return multiprocessing.Pool(job_count, _mp_init, initargs=(argv,)) except OSError as err: if err.errno not in SERIAL_RETRY_ERRNOS: raise @@ -593,22 +592,6 @@ def _try_initialize_processpool( return None -def calculate_pool_chunksize(num_checkers: int, num_jobs: int) -> int: - """Determine the chunksize for the multiprocessing Pool. - - - For chunksize, see: https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool.imap # noqa - - This formula, while not perfect, aims to give each worker two batches of - work. - - See: https://github.com/pycqa/flake8/issues/829#note_18878876 - - See: https://github.com/pycqa/flake8/issues/197 - """ - return max(num_checkers // (num_jobs * 2), 1) - - -def _run_checks(checker: FileChecker) -> tuple[str, Results, dict[str, int]]: - return checker.run_checks() - - def find_offset( offset: int, mapping: processor._LogicalMapping ) -> tuple[int, int]: diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index c76fb86..b6bfae3 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -2,7 +2,6 @@ from __future__ import annotations import argparse -import configparser import json import logging import time @@ -15,10 +14,7 @@ from flake8 import exceptions from flake8 import style_guide from flake8.formatting.base import BaseFormatter from flake8.main import debug -from flake8.main import options -from flake8.options import aggregator -from flake8.options import config -from flake8.options import manager +from flake8.options.parse_args import parse_args from flake8.plugins import finder from flake8.plugins import reporter @@ -35,12 +31,6 @@ class Application: self.start_time = time.time() #: The timestamp when the Application finished reported errors. self.end_time: float | None = None - #: The prelimary argument parser for handling options required for - #: obtaining and parsing the configuration file. - self.prelim_arg_parser = options.stage1_arg_parser() - #: The instance of :class:`flake8.options.manager.OptionManager` used - #: to parse and handle the options and arguments passed by the user - self.option_manager: manager.OptionManager | None = None self.plugins: finder.Plugins | None = None #: The user-selected formatter from :attr:`formatting_plugins` @@ -65,30 +55,6 @@ class Application: #: with a non-zero status code self.catastrophic_failure = False - def parse_preliminary_options( - self, argv: Sequence[str] - ) -> tuple[argparse.Namespace, list[str]]: - """Get preliminary options from the CLI, pre-plugin-loading. - - We need to know the values of a few standard options so that we can - locate configuration files and configure logging. - - Since plugins aren't loaded yet, there may be some as-yet-unknown - options; we ignore those for now, they'll be parsed later when we do - real option parsing. - - :param argv: - Command-line arguments passed in directly. - :returns: - Populated namespace and list of remaining argument strings. - """ - args, rest = self.prelim_arg_parser.parse_known_args(argv) - # XXX (ericvw): Special case "forwarding" the output file option so - # that it can be reparsed again for the BaseFormatter.filename. - if args.output_file: - rest.extend(("--output-file", args.output_file)) - return args, rest - def exit_code(self) -> int: """Return the program exit code.""" if self.catastrophic_failure: @@ -99,76 +65,6 @@ class Application: else: return int(self.result_count > 0) - def find_plugins( - self, - cfg: configparser.RawConfigParser, - cfg_dir: str, - *, - enable_extensions: str | None, - require_plugins: str | None, - ) -> None: - """Find and load the plugins for this application. - - Set :attr:`plugins` based on loaded plugins. - """ - opts = finder.parse_plugin_options( - cfg, - cfg_dir, - enable_extensions=enable_extensions, - require_plugins=require_plugins, - ) - raw = finder.find_plugins(cfg, opts) - self.plugins = finder.load_plugins(raw, opts) - - def register_plugin_options(self) -> None: - """Register options provided by plugins to our option manager.""" - assert self.plugins is not None - - self.option_manager = manager.OptionManager( - version=flake8.__version__, - plugin_versions=self.plugins.versions_str(), - parents=[self.prelim_arg_parser], - formatter_names=list(self.plugins.reporters), - ) - options.register_default_options(self.option_manager) - self.option_manager.register_plugins(self.plugins) - - def parse_configuration_and_cli( - self, - cfg: configparser.RawConfigParser, - cfg_dir: str, - argv: list[str], - ) -> None: - """Parse configuration files and the CLI options.""" - assert self.option_manager is not None - assert self.plugins is not None - self.options = aggregator.aggregate_options( - self.option_manager, - cfg, - cfg_dir, - argv, - ) - - if self.options.bug_report: - info = debug.information(flake8.__version__, self.plugins) - print(json.dumps(info, indent=2, sort_keys=True)) - raise SystemExit(0) - - for loaded in self.plugins.all_plugins(): - parse_options = getattr(loaded.obj, "parse_options", None) - if parse_options is None: - continue - - # XXX: ideally we wouldn't have two forms of parse_options - try: - parse_options( - self.option_manager, - self.options, - self.options.filenames, - ) - except TypeError: - parse_options(self.options) - def make_formatter(self) -> None: """Initialize a formatter based on the parsed options.""" assert self.plugins is not None @@ -183,13 +79,14 @@ class Application: self.options, self.formatter ) - def make_file_checker_manager(self) -> None: + def make_file_checker_manager(self, argv: Sequence[str]) -> None: """Initialize our FileChecker Manager.""" assert self.guide is not None assert self.plugins is not None self.file_checker_manager = checker.Manager( style_guide=self.guide, plugins=self.plugins.checkers, + argv=argv, ) def run_checks(self) -> None: @@ -265,28 +162,16 @@ class Application: This finds the plugins, registers their options, and parses the command-line arguments. """ - # NOTE(sigmavirus24): When updating this, make sure you also update - # our legacy API calls to these same methods. - prelim_opts, remaining_args = self.parse_preliminary_options(argv) - flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file) + self.plugins, self.options = parse_args(argv) - cfg, cfg_dir = config.load_config( - config=prelim_opts.config, - extra=prelim_opts.append_config, - isolated=prelim_opts.isolated, - ) + if self.options.bug_report: + info = debug.information(flake8.__version__, self.plugins) + print(json.dumps(info, indent=2, sort_keys=True)) + raise SystemExit(0) - self.find_plugins( - cfg, - cfg_dir, - enable_extensions=prelim_opts.enable_extensions, - require_plugins=prelim_opts.require_plugins, - ) - self.register_plugin_options() - self.parse_configuration_and_cli(cfg, cfg_dir, remaining_args) self.make_formatter() self.make_guide() - self.make_file_checker_manager() + self.make_file_checker_manager(argv) def report(self) -> None: """Report errors, statistics, and benchmarks.""" diff --git a/src/flake8/options/parse_args.py b/src/flake8/options/parse_args.py new file mode 100644 index 0000000..e3f8795 --- /dev/null +++ b/src/flake8/options/parse_args.py @@ -0,0 +1,70 @@ +"""Procedure for parsing args, config, loading plugins.""" +from __future__ import annotations + +import argparse +from typing import Sequence + +import flake8 +from flake8.main import options +from flake8.options import aggregator +from flake8.options import config +from flake8.options import manager +from flake8.plugins import finder + + +def parse_args( + argv: Sequence[str], +) -> tuple[finder.Plugins, argparse.Namespace]: + """Procedure for parsing args, config, loading plugins.""" + prelim_parser = options.stage1_arg_parser() + + args0, rest = prelim_parser.parse_known_args(argv) + # XXX (ericvw): Special case "forwarding" the output file option so + # that it can be reparsed again for the BaseFormatter.filename. + if args0.output_file: + rest.extend(("--output-file", args0.output_file)) + + flake8.configure_logging(args0.verbose, args0.output_file) + + cfg, cfg_dir = config.load_config( + config=args0.config, + extra=args0.append_config, + isolated=args0.isolated, + ) + + plugin_opts = finder.parse_plugin_options( + cfg, + cfg_dir, + enable_extensions=args0.enable_extensions, + require_plugins=args0.require_plugins, + ) + raw_plugins = finder.find_plugins(cfg, plugin_opts) + plugins = finder.load_plugins(raw_plugins, plugin_opts) + + option_manager = manager.OptionManager( + version=flake8.__version__, + plugin_versions=plugins.versions_str(), + parents=[prelim_parser], + formatter_names=list(plugins.reporters), + ) + options.register_default_options(option_manager) + option_manager.register_plugins(plugins) + + opts = aggregator.aggregate_options(option_manager, cfg, cfg_dir, rest) + + for loaded in plugins.all_plugins(): + parse_options = getattr(loaded.obj, "parse_options", None) + if parse_options is None: + continue + + # XXX: ideally we wouldn't have two forms of parse_options + try: + parse_options( + option_manager, + opts, + opts.filenames, + ) + except TypeError: + parse_options(opts) + + return plugins, opts diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py index ab9eb27..13ec8c1 100644 --- a/tests/integration/test_checker.py +++ b/tests/integration/test_checker.py @@ -266,17 +266,12 @@ def test_report_order(results, expected_order): # tuples to create the expected result lists from the indexes expected_results = [results[index] for index in expected_order] - file_checker = mock.Mock(spec=["results", "display_name"]) - file_checker.results = results - file_checker.display_name = "placeholder" - style_guide = mock.MagicMock(spec=["options", "processing_file"]) # Create a placeholder manager without arguments or plugins # Just add one custom file checker which just provides the results - manager = checker.Manager(style_guide, finder.Checkers([], [], [])) - manager.checkers = manager._all_checkers = [file_checker] - + manager = checker.Manager(style_guide, finder.Checkers([], [], []), []) + manager.results = [("placeholder", results, {})] # _handle_results is the first place which gets the sorted result # Should something non-private be mocked instead? handler = mock.Mock(side_effect=count_side_effect) @@ -295,9 +290,9 @@ def test_acquire_when_multiprocessing_pool_can_initialize(): This simulates the behaviour on most common platforms. """ with mock.patch("multiprocessing.Pool") as pool: - result = checker._try_initialize_processpool(2) + result = checker._try_initialize_processpool(2, []) - pool.assert_called_once_with(2, checker._pool_init) + pool.assert_called_once_with(2, checker._mp_init, initargs=([],)) assert result is pool.return_value @@ -314,9 +309,9 @@ def test_acquire_when_multiprocessing_pool_can_not_initialize(): https://github.com/python/cpython/blob/4e02981de0952f54bf87967f8e10d169d6946b40/Lib/multiprocessing/synchronize.py#L30-L33 """ with mock.patch("multiprocessing.Pool", side_effect=ImportError) as pool: - result = checker._try_initialize_processpool(2) + result = checker._try_initialize_processpool(2, []) - pool.assert_called_once_with(2, checker._pool_init) + pool.assert_called_once_with(2, checker._mp_init, initargs=([],)) assert result is None diff --git a/tests/unit/test_checker_manager.py b/tests/unit/test_checker_manager.py index b75bf6d..68dd82a 100644 --- a/tests/unit/test_checker_manager.py +++ b/tests/unit/test_checker_manager.py @@ -20,9 +20,9 @@ def style_guide_mock(): def _parallel_checker_manager(): """Call Manager.run() and return the number of calls to `run_serial`.""" style_guide = style_guide_mock() - manager = checker.Manager(style_guide, finder.Checkers([], [], [])) - # multiple checkers is needed for parallel mode - manager.checkers = [mock.Mock(), mock.Mock()] + manager = checker.Manager(style_guide, finder.Checkers([], [], []), []) + # multiple files is needed for parallel mode + manager.filenames = ("file1", "file2") return manager @@ -36,8 +36,7 @@ def test_oserrors_cause_serial_fall_back(): assert serial.call_count == 1 -@mock.patch.object(multiprocessing, "get_start_method", return_value="fork") -def test_oserrors_are_reraised(_): +def test_oserrors_are_reraised(): """Verify that unexpected OSErrors will cause the Manager to reraise.""" err = OSError(errno.EAGAIN, "Ominous message") with mock.patch("_multiprocessing.SemLock", side_effect=err): @@ -48,14 +47,6 @@ def test_oserrors_are_reraised(_): assert serial.call_count == 0 -@mock.patch.object(multiprocessing, "get_start_method", return_value="spawn") -def test_multiprocessing_is_disabled(_): - """Verify not being able to import multiprocessing forces jobs to 0.""" - style_guide = style_guide_mock() - manager = checker.Manager(style_guide, finder.Checkers([], [], [])) - assert manager.jobs == 0 - - def test_multiprocessing_cpu_count_not_implemented(): """Verify that jobs is 0 if cpu_count is unavailable.""" style_guide = style_guide_mock() @@ -66,22 +57,18 @@ def test_multiprocessing_cpu_count_not_implemented(): "cpu_count", side_effect=NotImplementedError, ): - manager = checker.Manager(style_guide, finder.Checkers([], [], [])) + manager = checker.Manager(style_guide, finder.Checkers([], [], []), []) assert manager.jobs == 0 -@mock.patch.object(multiprocessing, "get_start_method", return_value="spawn") -def test_make_checkers(_): +def test_make_checkers(): """Verify that we create a list of FileChecker instances.""" style_guide = style_guide_mock() style_guide.options.filenames = ["file1", "file2"] - manager = checker.Manager(style_guide, finder.Checkers([], [], [])) + manager = checker.Manager(style_guide, finder.Checkers([], [], []), []) with mock.patch("flake8.utils.fnmatch", return_value=True): with mock.patch("flake8.processor.FileProcessor"): - manager.make_checkers(["file1", "file2"]) + manager.start() - assert manager._all_checkers - for file_checker in manager._all_checkers: - assert file_checker.filename in style_guide.options.filenames - assert not manager.checkers # the files don't exist + assert manager.filenames == ("file1", "file2") diff --git a/tests/unit/test_legacy_api.py b/tests/unit/test_legacy_api.py index 844bd5a..c6af630 100644 --- a/tests/unit/test_legacy_api.py +++ b/tests/unit/test_legacy_api.py @@ -1,57 +1,12 @@ """Tests for Flake8's legacy API.""" from __future__ import annotations -import argparse -import configparser -import os.path from unittest import mock import pytest from flake8.api import legacy as api from flake8.formatting import base as formatter -from flake8.options import config - - -def test_get_style_guide(): - """Verify the methods called on our internal Application.""" - prelim_opts = argparse.Namespace( - append_config=[], - config=None, - isolated=False, - output_file=None, - verbose=0, - enable_extensions=None, - require_plugins=None, - ) - mockedapp = mock.Mock() - mockedapp.parse_preliminary_options.return_value = (prelim_opts, []) - mockedapp.program = "flake8" - - cfg = configparser.RawConfigParser() - cfg_dir = os.getcwd() - - with mock.patch.object(config, "load_config", return_value=(cfg, cfg_dir)): - with mock.patch("flake8.main.application.Application") as application: - application.return_value = mockedapp - style_guide = api.get_style_guide() - - application.assert_called_once_with() - mockedapp.parse_preliminary_options.assert_called_once_with([]) - mockedapp.find_plugins.assert_called_once_with( - cfg, - cfg_dir, - enable_extensions=None, - require_plugins=None, - ) - mockedapp.register_plugin_options.assert_called_once_with() - mockedapp.parse_configuration_and_cli.assert_called_once_with( - cfg, cfg_dir, [] - ) - mockedapp.make_formatter.assert_called_once_with() - mockedapp.make_guide.assert_called_once_with() - mockedapp.make_file_checker_manager.assert_called_once_with() - assert isinstance(style_guide, api.StyleGuide) def test_styleguide_options(): From eb66669067b0ca0e2beaa26a0643d9df82c3da08 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 28 Oct 2022 20:12:25 -0400 Subject: [PATCH 026/131] add tidelift security policy --- .github/SECURITY.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .github/SECURITY.md diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 0000000..5885648 --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,5 @@ +## security contact information + +to report a security vulnerability, please use the +[Tidelift security contact](https://tidelift.com/security). +Tidelift will coordinate the fix and disclosure. From 449799975a309cb4ac5e5ce74ae11ec8f77c3a22 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 28 Oct 2022 20:18:22 -0400 Subject: [PATCH 027/131] add tidelift to FUNDING.yml --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index eb54a96..7267456 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1,2 @@ github: asottile +tidelift: pypi/pre-commit From 7a094fa826aed5f0d2182744261b71a2f39c7396 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 30 Oct 2022 15:11:56 -0400 Subject: [PATCH 028/131] ensure results are sorted for file traversal --- src/flake8/checker.py | 4 +++- tests/integration/test_main.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 2d7bcd9..2cae545 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -6,6 +6,7 @@ import contextlib import errno import logging import multiprocessing.pool +import operator import signal import tokenize from typing import Any @@ -180,8 +181,9 @@ class Manager: A tuple of the total results found and the results reported. """ results_reported = results_found = 0 + self.results.sort(key=operator.itemgetter(0)) for filename, results, _ in self.results: - results.sort(key=lambda tup: (tup[1], tup[2])) + results.sort(key=operator.itemgetter(1, 2)) with self.style_guide.processing_file(filename): results_reported += self._handle_results(filename, results) results_found += len(results) diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index db60f6b..96d1182 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -98,6 +98,26 @@ t.py:1:1: F401 'os' imported but unused assert err == "" +def test_errors_sorted(tmpdir, capsys): + with tmpdir.as_cwd(): + for c in "abcde": + tmpdir.join(f"{c}.py").write("import os\n") + assert cli.main(["./"]) == 1 + + # file traversal was done in inode-order before + # this uses a significant number of files such that it's unlikely to pass + expected = """\ +./a.py:1:1: F401 'os' imported but unused +./b.py:1:1: F401 'os' imported but unused +./c.py:1:1: F401 'os' imported but unused +./d.py:1:1: F401 'os' imported but unused +./e.py:1:1: F401 'os' imported but unused +""" + out, err = capsys.readouterr() + assert out == expected + assert err == "" + + def test_extend_exclude(tmpdir, capsys): """Ensure that `flake8 --extend-exclude` works.""" for d in ["project", "vendor", "legacy", ".git", ".tox", ".hg"]: From 71ac5d1e8af015f1cc3d53f1dbb0265d187fb31a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Nov 2022 01:55:24 +0000 Subject: [PATCH 029/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/reorder_python_imports: v3.8.5 → v3.9.0](https://github.com/asottile/reorder_python_imports/compare/v3.8.5...v3.9.0) - [github.com/asottile/pyupgrade: v3.1.0 → v3.2.0](https://github.com/asottile/pyupgrade/compare/v3.1.0...v3.2.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 567bc7a..1a154c0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/reorder_python_imports - rev: v3.8.5 + rev: v3.9.0 hooks: - id: reorder-python-imports args: [ @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.1.0 + rev: v3.2.0 hooks: - id: pyupgrade args: [--py37-plus] From 175c9ae3bcd505ce13b43ef2ec723f5eae3cbf26 Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Sun, 6 Nov 2022 16:49:02 +0100 Subject: [PATCH 030/131] Fix typos --- src/flake8/plugins/pyflakes.py | 4 ++-- src/flake8/plugins/reporter.py | 2 +- src/flake8/processor.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index 7b99fd4..cffea36 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -90,13 +90,13 @@ class FlakesChecker(pyflakes.checker.Checker): for exclude in self.exclude_from_doctest: if exclude != "" and filename.startswith(exclude): with_doctest = False - overlaped_by = [ + overlapped_by = [ include for include in included_by if include.startswith(exclude) ] - if overlaped_by: + if overlapped_by: with_doctest = True super().__init__( diff --git a/src/flake8/plugins/reporter.py b/src/flake8/plugins/reporter.py index f63b20c..a5749c0 100644 --- a/src/flake8/plugins/reporter.py +++ b/src/flake8/plugins/reporter.py @@ -1,4 +1,4 @@ -"""Functions for construcing the requested report plugin.""" +"""Functions for constructing the requested report plugin.""" from __future__ import annotations import argparse diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 77f3d5a..16fa16b 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -27,7 +27,7 @@ _Logical = Tuple[List[str], List[str], _LogicalMapping] class FileProcessor: - """Processes a file and holdes state. + """Processes a file and holds state. This processes a file by generating tokens, logical and physical lines, and AST trees. This also provides a way of passing state about the file @@ -63,7 +63,7 @@ class FileProcessor: options: argparse.Namespace, lines: list[str] | None = None, ) -> None: - """Initialice our file processor. + """Initialize our file processor. :param filename: Name of the file to process """ From 1346ddefd315223b8f476bb02381a944e14f2cbc Mon Sep 17 00:00:00 2001 From: Menno Liefstingh Date: Thu, 13 Oct 2022 01:15:35 +0200 Subject: [PATCH 031/131] Adds warning when invalid error codes are parsed for ignore or extend-ignore from config file --- src/flake8/defaults.py | 2 ++ src/flake8/options/config.py | 11 ++++++++++ src/flake8/plugins/finder.py | 8 +++---- tests/unit/plugins/finder_test.py | 31 -------------------------- tests/unit/test_defaults.py | 36 +++++++++++++++++++++++++++++++ tests/unit/test_options_config.py | 32 +++++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 36 deletions(-) create mode 100644 tests/unit/test_defaults.py diff --git a/src/flake8/defaults.py b/src/flake8/defaults.py index 4ba0048..e3edf32 100644 --- a/src/flake8/defaults.py +++ b/src/flake8/defaults.py @@ -43,3 +43,5 @@ NOQA_INLINE_REGEXP = re.compile( ) NOQA_FILE = re.compile(r"\s*# flake8[:=]\s*noqa", re.I) + +VALID_CODE_PREFIX = re.compile("^[A-Z]{1,3}[0-9]{0,3}$", re.ASCII) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index e158737..a9ced1b 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -7,6 +7,7 @@ import os.path from typing import Any from flake8 import exceptions +from flake8.defaults import VALID_CODE_PREFIX from flake8.options.manager import OptionManager LOG = logging.getLogger(__name__) @@ -120,6 +121,16 @@ def parse_config( LOG.debug('Option "%s" returned value: %r', option_name, value) final_value = option.normalize(value, cfg_dir) + + if option_name in {"ignore", "extend-ignore"}: + for error_code in final_value: + if not VALID_CODE_PREFIX.match(error_code): + raise ValueError( + f"Error code {error_code!r} " + f"supplied to {option_name!r} option " + f"does not match {VALID_CODE_PREFIX.pattern!r}" + ) + assert option.config_name is not None config_dict[option.config_name] = final_value diff --git a/src/flake8/plugins/finder.py b/src/flake8/plugins/finder.py index c051488..4a43ccb 100644 --- a/src/flake8/plugins/finder.py +++ b/src/flake8/plugins/finder.py @@ -5,7 +5,6 @@ import configparser import inspect import itertools import logging -import re import sys from typing import Any from typing import Generator @@ -14,13 +13,12 @@ from typing import NamedTuple from flake8 import utils from flake8._compat import importlib_metadata +from flake8.defaults import VALID_CODE_PREFIX from flake8.exceptions import ExecutionError from flake8.exceptions import FailedToLoadPlugin LOG = logging.getLogger(__name__) -VALID_CODE = re.compile("^[A-Z]{1,3}[0-9]{0,3}$", re.ASCII) - FLAKE8_GROUPS = frozenset(("flake8.extension", "flake8.report")) BANNED_PLUGINS = { @@ -337,10 +335,10 @@ def _classify_plugins( raise NotImplementedError(f"what plugin type? {loaded}") for loaded in itertools.chain(tree, logical_line, physical_line): - if not VALID_CODE.match(loaded.entry_name): + if not VALID_CODE_PREFIX.match(loaded.entry_name): raise ExecutionError( f"plugin code for `{loaded.display_name}` does not match " - f"{VALID_CODE.pattern}" + f"{VALID_CODE_PREFIX.pattern}" ) return Plugins( diff --git a/tests/unit/plugins/finder_test.py b/tests/unit/plugins/finder_test.py index d526fd1..cd5cf4a 100644 --- a/tests/unit/plugins/finder_test.py +++ b/tests/unit/plugins/finder_test.py @@ -31,37 +31,6 @@ def _loaded(plugin=None, obj=None, parameters=None): return finder.LoadedPlugin(plugin, obj, parameters) -@pytest.mark.parametrize( - "s", - ( - "E", - "E1", - "E123", - "ABC", - "ABC1", - "ABC123", - ), -) -def test_valid_plugin_prefixes(s): - assert finder.VALID_CODE.match(s) - - -@pytest.mark.parametrize( - "s", - ( - "", - "A1234", - "ABCD", - "abc", - "a-b", - "☃", - "A𝟗", - ), -) -def test_invalid_plugin_prefixes(s): - assert finder.VALID_CODE.match(s) is None - - def test_loaded_plugin_entry_name_vs_display_name(): loaded = _loaded(_plugin(package="package-name", ep=_ep(name="Q"))) assert loaded.entry_name == "Q" diff --git a/tests/unit/test_defaults.py b/tests/unit/test_defaults.py new file mode 100644 index 0000000..822b8f0 --- /dev/null +++ b/tests/unit/test_defaults.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +import pytest + +from flake8.defaults import VALID_CODE_PREFIX + + +@pytest.mark.parametrize( + "s", + ( + "E", + "E1", + "E123", + "ABC", + "ABC1", + "ABC123", + ), +) +def test_valid_plugin_prefixes(s): + assert VALID_CODE_PREFIX.match(s) + + +@pytest.mark.parametrize( + "s", + ( + "", + "A1234", + "ABCD", + "abc", + "a-b", + "☃", + "A𝟗", + ), +) +def test_invalid_plugin_prefixes(s): + assert VALID_CODE_PREFIX.match(s) is None diff --git a/tests/unit/test_options_config.py b/tests/unit/test_options_config.py index 8c8f0cb..43d8104 100644 --- a/tests/unit/test_options_config.py +++ b/tests/unit/test_options_config.py @@ -220,3 +220,35 @@ def test_parse_config_ignores_unknowns(tmp_path, opt_manager, caplog): def test_load_config_missing_file_raises_exception(capsys): with pytest.raises(exceptions.ExecutionError): config.load_config("foo.cfg", []) + + +def test_invalid_ignore_codes_raise_error(tmpdir, opt_manager): + tmpdir.join("setup.cfg").write("[flake8]\nignore = E203, //comment") + with tmpdir.as_cwd(): + cfg, _ = config.load_config("setup.cfg", [], isolated=False) + + with pytest.raises(ValueError) as excinfo: + config.parse_config(opt_manager, cfg, tmpdir) + + expected = ( + "Error code '//comment' supplied to 'ignore' option " + "does not match '^[A-Z]{1,3}[0-9]{0,3}$'" + ) + (msg,) = excinfo.value.args + assert msg == expected + + +def test_invalid_extend_ignore_codes_raise_error(tmpdir, opt_manager): + tmpdir.join("setup.cfg").write("[flake8]\nextend-ignore = E203, //comment") + with tmpdir.as_cwd(): + cfg, _ = config.load_config("setup.cfg", [], isolated=False) + + with pytest.raises(ValueError) as excinfo: + config.parse_config(opt_manager, cfg, tmpdir) + + expected = ( + "Error code '//comment' supplied to 'extend-ignore' option " + "does not match '^[A-Z]{1,3}[0-9]{0,3}$'" + ) + (msg,) = excinfo.value.args + assert msg == expected From 314b9f51616cd7d71e2fd8fbf39138ab7a86f075 Mon Sep 17 00:00:00 2001 From: lt94 Date: Sun, 6 Nov 2022 19:29:05 +0800 Subject: [PATCH 032/131] Raise exception if append-config does not exist --- src/flake8/options/config.py | 5 ++++- tests/unit/test_options_config.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index a9ced1b..b51949c 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -87,7 +87,10 @@ def load_config( # TODO: remove this and replace it with configuration modifying plugins # read the additional configs afterwards for filename in extra: - cfg.read(filename, encoding="UTF-8") + if not cfg.read(filename, encoding="UTF-8"): + raise exceptions.ExecutionError( + f"The specified config file does not exist: {filename}" + ) return cfg, cfg_dir diff --git a/tests/unit/test_options_config.py b/tests/unit/test_options_config.py index 43d8104..7de58f0 100644 --- a/tests/unit/test_options_config.py +++ b/tests/unit/test_options_config.py @@ -222,6 +222,11 @@ def test_load_config_missing_file_raises_exception(capsys): config.load_config("foo.cfg", []) +def test_load_config_missing_append_config_raise_exception(): + with pytest.raises(exceptions.ExecutionError): + config.load_config(None, ["dont_exist_config.cfg"], isolated=False) + + def test_invalid_ignore_codes_raise_error(tmpdir, opt_manager): tmpdir.join("setup.cfg").write("[flake8]\nignore = E203, //comment") with tmpdir.as_cwd(): From 82953c9ae3d29d8b0208d7a8e44305b338524366 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Nov 2022 01:55:50 +0000 Subject: [PATCH 033/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.2.0 → v3.2.2](https://github.com/asottile/pyupgrade/compare/v3.2.0...v3.2.2) - [github.com/pre-commit/mirrors-mypy: v0.982 → v0.990](https://github.com/pre-commit/mirrors-mypy/compare/v0.982...v0.990) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1a154c0..9e602e1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.2.0 + rev: v3.2.2 hooks: - id: pyupgrade args: [--py37-plus] @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.982 + rev: v0.990 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 88457a0894411d706b535843b6760734c47ae637 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 15 Nov 2022 18:52:52 -0500 Subject: [PATCH 034/131] remove optparse support --- src/flake8/options/manager.py | 112 ++---------------------------- tests/unit/test_option_manager.py | 91 ------------------------ 2 files changed, 4 insertions(+), 199 deletions(-) diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index 8b4ec19..4fd26b2 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -7,7 +7,6 @@ import functools import logging from typing import Any from typing import Callable -from typing import Mapping from typing import Sequence from flake8 import utils @@ -20,55 +19,6 @@ LOG = logging.getLogger(__name__) _ARG = enum.Enum("_ARG", "NO") -_optparse_callable_map: dict[str, type[Any] | _ARG] = { - "int": int, - "long": int, - "string": str, - "float": float, - "complex": complex, - "choice": _ARG.NO, - # optparse allows this but does not document it - "str": str, -} - - -class _CallbackAction(argparse.Action): - """Shim for optparse-style callback actions.""" - - def __init__( - self, - *args: Any, - callback: Callable[..., Any], - callback_args: Sequence[Any] = (), - callback_kwargs: dict[str, Any] | None = None, - **kwargs: Any, - ) -> None: - self._callback = callback - self._callback_args = callback_args - self._callback_kwargs = callback_kwargs or {} - super().__init__(*args, **kwargs) - - def __call__( - self, - parser: argparse.ArgumentParser, - namespace: argparse.Namespace, - values: Sequence[str] | str | None, - option_string: str | None = None, - ) -> None: - if not values: - values = None - elif isinstance(values, list) and len(values) > 1: - values = tuple(values) - self._callback( - self, - option_string, - values, - parser, - *self._callback_args, - **self._callback_kwargs, - ) - - def _flake8_normalize( value: str, *args: str, @@ -95,21 +45,16 @@ class Option: self, short_option_name: str | _ARG = _ARG.NO, long_option_name: str | _ARG = _ARG.NO, - # Options below here are taken from the optparse.Option class + # Options below are taken from argparse.ArgumentParser.add_argument action: str | type[argparse.Action] | _ARG = _ARG.NO, default: Any | _ARG = _ARG.NO, - type: str | Callable[..., Any] | _ARG = _ARG.NO, + type: Callable[..., Any] | _ARG = _ARG.NO, dest: str | _ARG = _ARG.NO, nargs: int | str | _ARG = _ARG.NO, const: Any | _ARG = _ARG.NO, choices: Sequence[Any] | _ARG = _ARG.NO, help: str | _ARG = _ARG.NO, metavar: str | _ARG = _ARG.NO, - # deprecated optparse-only options - callback: Callable[..., Any] | _ARG = _ARG.NO, - callback_args: Sequence[Any] | _ARG = _ARG.NO, - callback_kwargs: Mapping[str, Any] | _ARG = _ARG.NO, - # Options below are taken from argparse.ArgumentParser.add_argument required: bool | _ARG = _ARG.NO, # Options below here are specific to Flake8 parse_from_config: bool = False, @@ -150,21 +95,9 @@ class Option: :param type: A callable to normalize the type (as is the case in - :mod:`argparse`). Deprecated: you can also pass through type - strings such as ``'int'`` which are handled by :mod:`optparse`. + :mod:`argparse`). :param action: - Any action allowed by :mod:`argparse`. Deprecated: this also - understands the ``action='callback'`` action from :mod:`optparse`. - :param callback: - Callback used if the action is ``"callback"``. Deprecated: please - use ``action=`` instead. - :param callback_args: - Additional positional arguments to the callback callable. - Deprecated: please use ``action=`` instead (probably with - ``functools.partial``). - :param callback_kwargs: - Keyword arguments to the callback callable. Deprecated: please - use ``action=`` instead (probably with ``functools.partial``). + Any action allowed by :mod:`argparse`. The following parameters are for Flake8's option handling alone. @@ -184,37 +117,6 @@ class Option: ): short_option_name, long_option_name = _ARG.NO, short_option_name - # optparse -> argparse `%default` => `%(default)s` - if help is not _ARG.NO and "%default" in help: - LOG.warning( - "option %s: please update `help=` text to use %%(default)s " - "instead of %%default -- this will be an error in the future", - long_option_name, - ) - help = help.replace("%default", "%(default)s") - - # optparse -> argparse for `callback` - if action == "callback": - LOG.warning( - "option %s: please update from optparse `action='callback'` " - "to argparse action classes -- this will be an error in the " - "future", - long_option_name, - ) - action = _CallbackAction - if type is _ARG.NO: - nargs = 0 - - # optparse -> argparse for `type` - if isinstance(type, str): - LOG.warning( - "option %s: please update from optparse string `type=` to " - "argparse callable `type=` -- this will be an error in the " - "future", - long_option_name, - ) - type = _optparse_callable_map[type] - # flake8 special type normalization if comma_separated_list or normalize_paths: type = functools.partial( @@ -237,9 +139,6 @@ class Option: self.nargs = nargs self.const = const self.choices = choices - self.callback = callback - self.callback_args = callback_args - self.callback_kwargs = callback_kwargs self.help = help self.metavar = metavar self.required = required @@ -251,9 +150,6 @@ class Option: "nargs": self.nargs, "const": self.const, "choices": self.choices, - "callback": self.callback, - "callback_args": self.callback_args, - "callback_kwargs": self.callback_kwargs, "help": self.help, "metavar": self.metavar, "required": self.required, diff --git a/tests/unit/test_option_manager.py b/tests/unit/test_option_manager.py index 3d3ddc1..92266f3 100644 --- a/tests/unit/test_option_manager.py +++ b/tests/unit/test_option_manager.py @@ -3,7 +3,6 @@ from __future__ import annotations import argparse import os -from unittest import mock import pytest @@ -170,96 +169,6 @@ def test_extend_default_ignore(optmanager): assert optmanager.extended_default_ignore == ["T100", "T101", "T102"] -def test_optparse_normalize_callback_option_legacy(optmanager): - """Test the optparse shim for `callback=`.""" - callback_foo = mock.Mock() - optmanager.add_option( - "--foo", - action="callback", - callback=callback_foo, - callback_args=(1, 2), - callback_kwargs={"a": "b"}, - ) - callback_bar = mock.Mock() - optmanager.add_option( - "--bar", - action="callback", - type="string", - callback=callback_bar, - ) - callback_baz = mock.Mock() - optmanager.add_option( - "--baz", - action="callback", - type="string", - nargs=2, - callback=callback_baz, - ) - - optmanager.parse_args(["--foo", "--bar", "bararg", "--baz", "1", "2"]) - - callback_foo.assert_called_once_with( - mock.ANY, # the option / action instance - "--foo", - None, - mock.ANY, # the OptionParser / ArgumentParser - 1, - 2, - a="b", - ) - callback_bar.assert_called_once_with( - mock.ANY, # the option / action instance - "--bar", - "bararg", - mock.ANY, # the OptionParser / ArgumentParser - ) - callback_baz.assert_called_once_with( - mock.ANY, # the option / action instance - "--baz", - ("1", "2"), - mock.ANY, # the OptionParser / ArgumentParser - ) - - -@pytest.mark.parametrize( - ("type_s", "input_val", "expected"), - ( - ("int", "5", 5), - ("long", "6", 6), - ("string", "foo", "foo"), - ("float", "1.5", 1.5), - ("complex", "1+5j", 1 + 5j), - # optparse allows this but does not document it - ("str", "foo", "foo"), - ), -) -def test_optparse_normalize_types(optmanager, type_s, input_val, expected): - """Test the optparse shim for type="typename".""" - optmanager.add_option("--foo", type=type_s) - opts = optmanager.parse_args(["--foo", input_val]) - assert opts.foo == expected - - -def test_optparse_normalize_choice_type(optmanager): - """Test the optparse shim for type="choice".""" - optmanager.add_option("--foo", type="choice", choices=("1", "2", "3")) - opts = optmanager.parse_args(["--foo", "1"]) - assert opts.foo == "1" - # fails to parse - with pytest.raises(SystemExit): - optmanager.parse_args(["--foo", "4"]) - - -def test_optparse_normalize_help(optmanager, capsys): - """Test the optparse shim for %default in help text.""" - optmanager.add_option("--foo", default="bar", help="default: %default") - with pytest.raises(SystemExit): - optmanager.parse_args(["--help"]) - out, err = capsys.readouterr() - output = out + err - assert "default: bar" in output - - @pytest.mark.parametrize( ("s", "is_auto", "n_jobs"), ( From aa002ee4ed7fcb4c6ffdec29b4dc122244638c64 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 18 Nov 2022 11:32:13 -0500 Subject: [PATCH 035/131] require python 3.8.1+ --- .github/workflows/main.yml | 18 +++---- .pre-commit-config.yaml | 4 +- setup.cfg | 8 +-- src/flake8/_compat.py | 18 ------- src/flake8/plugins/finder.py | 14 ++--- tests/integration/test_checker.py | 4 +- tests/integration/test_main.py | 2 - tests/unit/plugins/finder_test.py | 82 ++++++++++++++--------------- tests/unit/plugins/reporter_test.py | 4 +- tests/unit/test_debug.py | 4 +- tests/unit/test_file_checker.py | 4 +- tox.ini | 2 +- 12 files changed, 70 insertions(+), 94 deletions(-) delete mode 100644 src/flake8/_compat.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 868b5ea..e5799e1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,10 +13,7 @@ jobs: include: # linux - os: ubuntu-latest - python: pypy-3.7 - toxenv: py - - os: ubuntu-latest - python: 3.7 + python: pypy-3.8 toxenv: py - os: ubuntu-latest python: 3.8 @@ -25,21 +22,24 @@ jobs: python: 3.9 toxenv: py - os: ubuntu-latest - python: '3.10.0-alpha - 3.10.999' + python: '3.10' + toxenv: py + - os: ubuntu-latest + python: '3.11' toxenv: py # windows - os: windows-latest - python: 3.7 + python: 3.8 toxenv: py # misc - os: ubuntu-latest - python: 3.9 + python: '3.10' toxenv: docs - os: ubuntu-latest - python: 3.9 + python: '3.10' toxenv: linters - os: ubuntu-latest - python: 3.9 + python: '3.10' toxenv: dogfood runs-on: ${{ matrix.os }} steps: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9e602e1..10914b7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,14 +13,14 @@ repos: - id: reorder-python-imports args: [ --application-directories, '.:src', - --py37-plus, + --py38-plus, --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade rev: v3.2.2 hooks: - id: pyupgrade - args: [--py37-plus] + args: [--py38-plus] - repo: https://github.com/psf/black rev: 22.10.0 hooks: diff --git a/setup.cfg b/setup.cfg index 5e20adf..36e90b4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,10 +20,6 @@ classifiers = Programming Language :: Python Programming Language :: Python :: 3 Programming Language :: Python :: 3 :: Only - Programming Language :: Python :: 3.7 - Programming Language :: Python :: 3.8 - Programming Language :: Python :: 3.9 - Programming Language :: Python :: 3.10 Programming Language :: Python :: Implementation :: CPython Programming Language :: Python :: Implementation :: PyPy Topic :: Software Development :: Libraries :: Python Modules @@ -41,8 +37,8 @@ install_requires = mccabe>=0.7.0,<0.8.0 pycodestyle>=2.9.0,<2.10.0 pyflakes>=2.5.0,<2.6.0 - importlib-metadata>=1.1.0,<4.3;python_version<"3.8" -python_requires = >=3.7 +# 3.8.0's importlib.metadata is broken +python_requires = >=3.8.1 [options.packages.find] where = src diff --git a/src/flake8/_compat.py b/src/flake8/_compat.py deleted file mode 100644 index 91770bc..0000000 --- a/src/flake8/_compat.py +++ /dev/null @@ -1,18 +0,0 @@ -"""Expose backports in a single place.""" -from __future__ import annotations - -import sys - -if sys.version_info >= (3, 8): # pragma: no cover (PY38+) - import importlib.metadata as importlib_metadata -else: # pragma: no cover ( Generator[Plugin, None, None]: - pyflakes_meta = importlib_metadata.distribution("pyflakes").metadata - pycodestyle_meta = importlib_metadata.distribution("pycodestyle").metadata + pyflakes_meta = importlib.metadata.distribution("pyflakes").metadata + pycodestyle_meta = importlib.metadata.distribution("pycodestyle").metadata for ep in eps: if ep.group not in FLAKE8_GROUPS: @@ -176,7 +176,7 @@ def _flake8_plugins( def _find_importlib_plugins() -> Generator[Plugin, None, None]: # some misconfigured pythons (RHEL) have things on `sys.path` twice seen = set() - for dist in importlib_metadata.distributions(): + for dist in importlib.metadata.distributions(): # assigned to prevent continual reparsing eps = dist.entry_points @@ -221,7 +221,7 @@ def _find_local_plugins( ): name, _, entry_str = plugin_s.partition("=") name, entry_str = name.strip(), entry_str.strip() - ep = importlib_metadata.EntryPoint(name, entry_str, group) + ep = importlib.metadata.EntryPoint(name, entry_str, group) yield Plugin("local", "local", ep) diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py index 13ec8c1..a585f5a 100644 --- a/tests/integration/test_checker.py +++ b/tests/integration/test_checker.py @@ -1,13 +1,13 @@ """Integration tests for the checker submodule.""" from __future__ import annotations +import importlib.metadata import sys from unittest import mock import pytest from flake8 import checker -from flake8._compat import importlib_metadata from flake8.plugins import finder from flake8.processor import FileProcessor @@ -85,7 +85,7 @@ def mock_file_checker_with_plugin(plugin_target): finder.Plugin( "flake-package", "9001", - importlib_metadata.EntryPoint( + importlib.metadata.EntryPoint( "Q", f"{plugin_target.__module__}:{plugin_target.__name__}", "flake8.extension", diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 96d1182..68b93cb 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -170,8 +170,6 @@ def test_tokenization_error_but_not_syntax_error(tmpdir, capsys): if hasattr(sys, "pypy_version_info"): # pragma: no cover (pypy) expected = "t.py:2:1: E999 SyntaxError: end of file (EOF) in multi-line statement\n" # noqa: E501 - elif sys.version_info < (3, 8): # pragma: no cover ( Date: Tue, 22 Nov 2022 01:20:39 +0000 Subject: [PATCH 036/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v0.990 → v0.991](https://github.com/pre-commit/mirrors-mypy/compare/v0.990...v0.991) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 10914b7..13c4a0f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.990 + rev: v0.991 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 047e6f8eb5111674b4dca4bf692651b716152ef5 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 23 Nov 2022 13:31:07 -0500 Subject: [PATCH 037/131] upgrade pycodestyle to 2.10 --- setup.cfg | 2 +- src/flake8/plugins/pycodestyle.py | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/setup.cfg b/setup.cfg index 36e90b4..d2959c8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,7 +35,7 @@ package_dir = # https://flake8.pycqa.org/en/latest/internal/releases.html#releasing-flake8 install_requires = mccabe>=0.7.0,<0.8.0 - pycodestyle>=2.9.0,<2.10.0 + pycodestyle>=2.10.0,<2.11.0 pyflakes>=2.5.0,<2.6.0 # 3.8.0's importlib.metadata is broken python_requires = >=3.8.1 diff --git a/src/flake8/plugins/pycodestyle.py b/src/flake8/plugins/pycodestyle.py index 4b0d67f..7eb90e1 100644 --- a/src/flake8/plugins/pycodestyle.py +++ b/src/flake8/plugins/pycodestyle.py @@ -26,11 +26,7 @@ from pycodestyle import missing_whitespace_after_keyword as _missing_whitespace_ from pycodestyle import missing_whitespace_around_operator as _missing_whitespace_around_operator # noqa: E501 from pycodestyle import module_imports_on_top_of_file as _module_imports_on_top_of_file # noqa: E501 from pycodestyle import python_3000_async_await_keywords as _python_3000_async_await_keywords # noqa: E501 -from pycodestyle import python_3000_backticks as _python_3000_backticks -from pycodestyle import python_3000_has_key as _python_3000_has_key from pycodestyle import python_3000_invalid_escape_sequence as _python_3000_invalid_escape_sequence # noqa: E501 -from pycodestyle import python_3000_not_equal as _python_3000_not_equal -from pycodestyle import python_3000_raise_comma as _python_3000_raise_comma from pycodestyle import tabs_obsolete as _tabs_obsolete from pycodestyle import tabs_or_spaces as _tabs_or_spaces from pycodestyle import trailing_blank_lines as _trailing_blank_lines @@ -83,11 +79,7 @@ def pycodestyle_logical( yield from _missing_whitespace_around_operator(logical_line, tokens) yield from _module_imports_on_top_of_file(logical_line, indent_level, checker_state, noqa) # noqa: E501 yield from _python_3000_async_await_keywords(logical_line, tokens) - yield from _python_3000_backticks(logical_line) - yield from _python_3000_has_key(logical_line, noqa) yield from _python_3000_invalid_escape_sequence(logical_line, tokens, noqa) - yield from _python_3000_not_equal(logical_line) - yield from _python_3000_raise_comma(logical_line) yield from _whitespace_around_comma(logical_line) yield from _whitespace_around_keywords(logical_line) yield from _whitespace_around_named_parameter_equals(logical_line, tokens) From 489be4d30a37273e67683d14758e3a7912d46571 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 23 Nov 2022 13:56:50 -0500 Subject: [PATCH 038/131] upgrade pyflakes to 3.0.0 --- setup.cfg | 2 +- src/flake8/plugins/pyflakes.py | 17 +++-------------- tests/unit/plugins/finder_test.py | 2 +- tests/unit/test_pyflakes_codes.py | 2 +- 4 files changed, 6 insertions(+), 17 deletions(-) diff --git a/setup.cfg b/setup.cfg index d2959c8..6c28b90 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,7 +36,7 @@ package_dir = install_requires = mccabe>=0.7.0,<0.8.0 pycodestyle>=2.10.0,<2.11.0 - pyflakes>=2.5.0,<2.6.0 + pyflakes>=3.0.0,<3.1.0 # 3.8.0's importlib.metadata is broken python_requires = >=3.8.1 diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index cffea36..a9dbc4c 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -4,7 +4,6 @@ from __future__ import annotations import argparse import ast import os -import tokenize from typing import Any from typing import Generator @@ -52,13 +51,13 @@ FLAKE8_PYFLAKES_CODES = { "DefaultExceptNotLast": "F707", "DoctestSyntaxError": "F721", "ForwardAnnotationSyntaxError": "F722", - "CommentAnnotationSyntaxError": "F723", "RedefinedWhileUnused": "F811", "UndefinedName": "F821", "UndefinedExport": "F822", "UndefinedLocal": "F823", "DuplicateArgument": "F831", "UnusedVariable": "F841", + "UnusedAnnotation": "F842", "RaiseNotImplemented": "F901", } @@ -70,12 +69,7 @@ class FlakesChecker(pyflakes.checker.Checker): include_in_doctest: list[str] = [] exclude_from_doctest: list[str] = [] - def __init__( - self, - tree: ast.AST, - file_tokens: list[tokenize.TokenInfo], - filename: str, - ) -> None: + def __init__(self, tree: ast.AST, filename: str) -> None: """Initialize the PyFlakes plugin with an AST tree and filename.""" filename = utils.normalize_path(filename) with_doctest = self.with_doctest @@ -99,12 +93,7 @@ class FlakesChecker(pyflakes.checker.Checker): if overlapped_by: with_doctest = True - super().__init__( - tree, - filename=filename, - withDoctest=with_doctest, - file_tokens=file_tokens, - ) + super().__init__(tree, filename=filename, withDoctest=with_doctest) @classmethod def add_options(cls, parser: OptionManager) -> None: diff --git a/tests/unit/plugins/finder_test.py b/tests/unit/plugins/finder_test.py index 89de0a7..b289bef 100644 --- a/tests/unit/plugins/finder_test.py +++ b/tests/unit/plugins/finder_test.py @@ -685,7 +685,7 @@ def test_load_plugin_ok(): assert loaded == finder.LoadedPlugin( plugin, FlakesChecker, - {"tree": True, "file_tokens": True, "filename": True}, + {"tree": True, "filename": True}, ) diff --git a/tests/unit/test_pyflakes_codes.py b/tests/unit/test_pyflakes_codes.py index 4626e3d..444008a 100644 --- a/tests/unit/test_pyflakes_codes.py +++ b/tests/unit/test_pyflakes_codes.py @@ -27,7 +27,7 @@ def f(): sys = sys """ tree = ast.parse(src) - checker = pyflakes_shim.FlakesChecker(tree, [], "t.py") + checker = pyflakes_shim.FlakesChecker(tree, "t.py") message_texts = [s for _, _, s, _ in checker.run()] assert message_texts == [ "F823 local variable 'sys' defined in enclosing scope on line 1 referenced before assignment", # noqa: E501 From b9a7794c4f425ef8419081e6993f99419cc667ea Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 23 Nov 2022 14:27:13 -0500 Subject: [PATCH 039/131] Release 6.0.0 --- docs/source/internal/releases.rst | 4 ++-- docs/source/release-notes/6.0.0.rst | 35 +++++++++++++++++++++++++++++ docs/source/release-notes/index.rst | 6 +++++ src/flake8/__init__.py | 2 +- 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 docs/source/release-notes/6.0.0.rst diff --git a/docs/source/internal/releases.rst b/docs/source/internal/releases.rst index f15ea0c..39fbdf7 100644 --- a/docs/source/internal/releases.rst +++ b/docs/source/internal/releases.rst @@ -28,9 +28,9 @@ Historically, |Flake8| has generated major releases for: - Unvendoring dependencies (2.0) -- Large scale refactoring (2.0, 3.0, 5.0) +- Large scale refactoring (2.0, 3.0, 5.0, 6.0) -- Subtly breaking CLI changes (3.0, 4.0, 5.0) +- Subtly breaking CLI changes (3.0, 4.0, 5.0, 6.0) - Breaking changes to its plugin interface (3.0) diff --git a/docs/source/release-notes/6.0.0.rst b/docs/source/release-notes/6.0.0.rst new file mode 100644 index 0000000..edb01df --- /dev/null +++ b/docs/source/release-notes/6.0.0.rst @@ -0,0 +1,35 @@ +6.0.0 -- 2022-11-23 +------------------- + +You can view the `6.0.0 milestone`_ on GitHub for more details. + +Backwards Incompatible Changes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- Remove ``--diff`` option (See also :issue:`1389`, :pull:`1720`). +- Produce an error when invalid codes are specified in configuration (See also + :issue:`1689`, :pull:`1713`). +- Produce an error if the file specified in ``--extend-config`` does not exist + (See also :issue:`1729`, :pull:`1732`). +- Remove ``optparse`` compatibility support (See also :pull:`1739`). + +New Dependency Information +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- pycodestyle has been updated to >= 2.10.0, < 2.11.0 (See also :pull:`1746`). +- Pyflakes has been updated to >= 3.0.0, < 3.1.0 (See also :pull:`1748`). + +Features +~~~~~~~~ + +- Require python >= 3.8.1 (See also :pull:`1633`, :pull:`1741`). +- List available formatters in for ``--format`` option in ``--help`` (See also + :issue:`223`, :pull:`1624`). +- Improve multiprocessing performance (See also :pull:`1723`). +- Enable multiprocessing on non-fork platforms (See also :pull:`1723`). +- Ensure results are sorted when discovered from files (See also :issue:`1670`, + :pull:`1726`). + +.. all links +.. _6.0.0 milestone: + https://github.com/PyCQA/flake8/milestone/47 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index d511f30..a9eff5f 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -5,6 +5,12 @@ All of the release notes that have been recorded for Flake8 are organized here with the newest releases first. +6.x Release Series +================== + +.. toctree:: + 6.0.0 + 5.x Release Series ================== diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index 43f3bec..c6d48b7 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ import sys LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "5.0.4" +__version__ = "6.0.0" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From cf6d3fe0c6faf7a8a3835ba97edac3238c8b1edc Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 24 Nov 2022 16:28:16 -0500 Subject: [PATCH 040/131] remove MANIFEST.in packagers: please package from the github tgz --- MANIFEST.in | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 74b090b..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,9 +0,0 @@ -include *.rst -include CONTRIBUTORS.txt -include LICENSE -include *.ini -global-exclude *.pyc -recursive-include docs *.rst *.py -recursive-include tests *.py *.ini *.rst *_diff -recursive-include src *.py -prune docs/build/ From 5fde644640496af61b580193178e34c8aafefc86 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Nov 2022 04:04:15 +0000 Subject: [PATCH 041/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/pre-commit-hooks: v4.3.0 → v4.4.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.3.0...v4.4.0) - [github.com/PyCQA/flake8: 5.0.4 → 6.0.0](https://github.com/PyCQA/flake8/compare/5.0.4...6.0.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 13c4a0f..ad9c62b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.3.0 + rev: v4.4.0 hooks: - id: check-yaml - id: debug-statements @@ -27,7 +27,7 @@ repos: - id: black args: [--line-length=79] - repo: https://github.com/PyCQA/flake8 - rev: 5.0.4 + rev: 6.0.0 hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy From c0d0ced5b83ca5f72d438ea86d0cc0fdf2dd8345 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 Dec 2022 02:21:54 +0000 Subject: [PATCH 042/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.2.2 → v3.3.0](https://github.com/asottile/pyupgrade/compare/v3.2.2...v3.3.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ad9c62b..f2ca587 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.2.2 + rev: v3.3.0 hooks: - id: pyupgrade args: [--py38-plus] From 00fbfd7774aca3d3fa36e5b3c8e1ac6080edf88c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Dec 2022 03:27:50 +0000 Subject: [PATCH 043/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.3.0 → v3.3.1](https://github.com/asottile/pyupgrade/compare/v3.3.0...v3.3.1) - [github.com/psf/black: 22.10.0 → 22.12.0](https://github.com/psf/black/compare/22.10.0...22.12.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f2ca587..1796476 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,12 +17,12 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.3.0 + rev: v3.3.1 hooks: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 22.10.0 + rev: 22.12.0 hooks: - id: black args: [--line-length=79] From 837e81948bdf81da1193584dfbb98ca16a2dc997 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 14 Dec 2022 12:55:37 -0500 Subject: [PATCH 044/131] communicate that --select is usually unnecessary --- docs/source/user/options.rst | 8 ++++++-- src/flake8/defaults.py | 1 - src/flake8/main/options.py | 14 +++++++++----- src/flake8/style_guide.py | 2 +- tests/unit/test_decision_engine.py | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/docs/source/user/options.rst b/docs/source/user/options.rst index f9e8b86..8db2df8 100644 --- a/docs/source/user/options.rst +++ b/docs/source/user/options.rst @@ -607,13 +607,14 @@ Options and their Descriptions :ref:`Go back to index ` + **You usually do not need to specify this option as the default includes + all installed plugin codes.** + Specify the list of error codes you wish |Flake8| to report. Similarly to :option:`--ignore`. You can specify a portion of an error code to get all that start with that string. For example, you can use ``E``, ``E4``, ``E43``, and ``E431``. - This defaults to: ``E,F,W,C90`` - Command-line example: .. prompt:: bash @@ -649,6 +650,9 @@ Options and their Descriptions .. versionadded:: 4.0.0 + **You usually do not need to specify this option as the default includes + all installed plugin codes.** + Specify a list of codes to add to the list of selected ones. Similar considerations as in :option:`--select` apply here with regard to the value. diff --git a/src/flake8/defaults.py b/src/flake8/defaults.py index e3edf32..8add695 100644 --- a/src/flake8/defaults.py +++ b/src/flake8/defaults.py @@ -16,7 +16,6 @@ EXCLUDE = ( "*.egg", ) IGNORE = ("E121", "E123", "E126", "E226", "E24", "E704", "W503", "W504") -SELECT = ("E", "F", "W", "C90") MAX_LINE_LENGTH = 79 INDENT_SIZE = 4 diff --git a/src/flake8/main/options.py b/src/flake8/main/options.py index 891bae9..9d57321 100644 --- a/src/flake8/main/options.py +++ b/src/flake8/main/options.py @@ -297,9 +297,11 @@ def register_default_options(option_manager: OptionManager) -> None: parse_from_config=True, comma_separated_list=True, help=( - f"Comma-separated list of error codes to enable. " - f"For example, ``--select=E4,E51,W234``. " - f"(Default: {','.join(defaults.SELECT)})" + "Limit the reported error codes to codes prefix-matched by this " + "list. " + "You usually do not need to specify this option as the default " + "includes all installed plugin codes. " + "For example, ``--select=E4,E51,W234``." ), ) @@ -309,8 +311,10 @@ def register_default_options(option_manager: OptionManager) -> None: parse_from_config=True, comma_separated_list=True, help=( - "Comma-separated list of error codes to add to the list " - "of selected ones. For example, ``--extend-select=E4,E51,W234``." + "Add additional error codes to the default ``--select``. " + "You usually do not need to specify this option as the default " + "includes all installed plugin codes. " + "For example, ``--extend-select=E4,E51,W234``." ), ) diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index 8431650..a409484 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -88,7 +88,7 @@ class DecisionEngine: self.selected = _select_ignore( option=options.select, - default=defaults.SELECT, + default=(), extended_default=options.extended_default_select, extend=options.extend_select, ) diff --git a/tests/unit/test_decision_engine.py b/tests/unit/test_decision_engine.py index 27ced3f..d543d5e 100644 --- a/tests/unit/test_decision_engine.py +++ b/tests/unit/test_decision_engine.py @@ -14,7 +14,7 @@ def create_options(**kwargs): kwargs.setdefault("ignore", None) kwargs.setdefault("extend_select", None) kwargs.setdefault("extend_ignore", None) - kwargs.setdefault("extended_default_select", []) + kwargs.setdefault("extended_default_select", ["C90", "F", "E", "W"]) kwargs.setdefault("extended_default_ignore", []) kwargs.setdefault("disable_noqa", False) return argparse.Namespace(**kwargs) From 4778fe9643e94123f93a306f1ed63b52323eee35 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 14 Dec 2022 13:25:35 -0500 Subject: [PATCH 045/131] deprecate --include-in-doctest --exclude-from-doctest --- src/flake8/plugins/pyflakes.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index a9dbc4c..f165c95 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse import ast +import logging import os from typing import Any from typing import Generator @@ -12,6 +13,8 @@ import pyflakes.checker from flake8 import utils from flake8.options.manager import OptionManager +LOG = logging.getLogger(__name__) + FLAKE8_PYFLAKES_CODES = { "UnusedImport": "F401", "ImportShadowedByLoopVar": "F402", @@ -137,6 +140,12 @@ class FlakesChecker(pyflakes.checker.Checker): cls.builtIns = cls.builtIns.union(options.builtins) cls.with_doctest = options.doctests + if options.include_in_doctest or options.exclude_from_doctest: + LOG.warning( + "--include-in-doctest / --exclude-from-doctest will be " + "removed in a future version. see PyCQA/flake8#1747" + ) + included_files = [] for included_file in options.include_in_doctest: if included_file == "": From 61138f1f4b00ae9e00b3262c566de9b2afdc5584 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 2 Jan 2023 10:25:13 -0500 Subject: [PATCH 046/131] correct tidelift link --- .github/FUNDING.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 7267456..1ac2512 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,2 +1,2 @@ github: asottile -tidelift: pypi/pre-commit +tidelift: pypi/flake8 From bc6e08723541125b0340bd6b6f72fdd3e30e9ca9 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 2 Jan 2023 07:41:47 -0800 Subject: [PATCH 047/131] use pypy3.9 to work around coverage issue --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e5799e1..7f28cf9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,7 +13,7 @@ jobs: include: # linux - os: ubuntu-latest - python: pypy-3.8 + python: pypy-3.9 toxenv: py - os: ubuntu-latest python: 3.8 From 0be0ae784269a96c3022c47ead652ec93e07bf63 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 11 Jan 2023 09:19:54 -0500 Subject: [PATCH 048/131] add myself to maintenance --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index f2a6d43..1ea6096 100644 --- a/README.rst +++ b/README.rst @@ -85,5 +85,6 @@ Links Maintenance =========== -Flake8 was created by Tarek Ziadé and is currently maintained by `Ian Cordasco +Flake8 was created by Tarek Ziadé and is currently maintained by `anthony sottile +`_ and `Ian Cordasco `_ From e6f9db5039a79d0776e416314e1e331c611d7319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastien=20G=C3=A9rard?= Date: Sat, 21 Jan 2023 21:37:35 +0100 Subject: [PATCH 049/131] Update python-api.rst Fix minor typo in doc --- docs/source/user/python-api.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/user/python-api.rst b/docs/source/user/python-api.rst index ed7e5e5..f51b146 100644 --- a/docs/source/user/python-api.rst +++ b/docs/source/user/python-api.rst @@ -70,7 +70,7 @@ namely .. warning:: These are not *perfectly* backwards compatible. Not all arguments are - respsected, and some of the types necessary for something to work have + respected, and some of the types necessary for something to work have changed. Most people, we observed, were using From 50755610d2161e8d6b15d37ca3c9a651eca7a443 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Feb 2023 07:21:58 +0000 Subject: [PATCH 050/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 22.12.0 → 23.1.0](https://github.com/psf/black/compare/22.12.0...23.1.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1796476..4fa1718 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,7 +22,7 @@ repos: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 22.12.0 + rev: 23.1.0 hooks: - id: black args: [--line-length=79] From b0d7912f7e4232cbaa87d9ba26df3cbd583b0ea0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Feb 2023 07:22:49 +0000 Subject: [PATCH 051/131] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/flake8/checker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 2cae545..8d70034 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -160,7 +160,7 @@ class Manager: def _handle_results(self, filename: str, results: Results) -> int: style_guide = self.style_guide reported_results_count = 0 - for (error_code, line_number, column, text, physical_line) in results: + for error_code, line_number, column, text, physical_line in results: reported_results_count += style_guide.handle_error( code=error_code, filename=filename, @@ -423,7 +423,7 @@ class FileChecker: runner = checker.run() except AttributeError: runner = checker - for (line_number, offset, text, _) in runner: + for line_number, offset, text, _ in runner: self.report( error_code=None, line_number=line_number, From 77e075c66a4f2642d1168da307f6fe42c5348ba8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Feb 2023 05:36:21 +0000 Subject: [PATCH 052/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v0.991 → v1.0.0](https://github.com/pre-commit/mirrors-mypy/compare/v0.991...v1.0.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4fa1718..6387b06 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.991 + rev: v1.0.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 2615d21a5bb19d7c6df0172cb3f536cf4c2fec72 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Feb 2023 05:18:56 +0000 Subject: [PATCH 053/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.0.0 → v1.0.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.0.0...v1.0.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6387b06..123eb87 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.0.0 + rev: v1.0.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From f096f853c26bc8db75a0c096251122d27262f099 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Mar 2023 06:44:36 +0000 Subject: [PATCH 054/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.0.1 → v1.1.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.0.1...v1.1.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 123eb87..059562b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.0.1 + rev: v1.1.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 050d0dcf4d9c4ff1b1083f02eb946eca6eab5988 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Apr 2023 09:38:50 +0000 Subject: [PATCH 055/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 23.1.0 → 23.3.0](https://github.com/psf/black/compare/23.1.0...23.3.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 059562b..f0303bd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,7 +22,7 @@ repos: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 23.1.0 + rev: 23.3.0 hooks: - id: black args: [--line-length=79] From 426f952fc89d9cf9b8f0ad9fa5fd4d28ef96b13c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Apr 2023 06:25:16 +0000 Subject: [PATCH 056/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.1.1 → v1.2.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.1.1...v1.2.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f0303bd..e2b95f5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.1.1 + rev: v1.2.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From cabc45abab39e136290f1c93ab36177cf2151eba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 25 Apr 2023 07:12:41 +0000 Subject: [PATCH 057/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.3.1 → v3.3.2](https://github.com/asottile/pyupgrade/compare/v3.3.1...v3.3.2) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e2b95f5..5994d20 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.3.1 + rev: v3.3.2 hooks: - id: pyupgrade args: [--py38-plus] From 0c3cf066d301a140eb87d3b0681d32c60f0139a5 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 30 Apr 2023 15:03:01 -0400 Subject: [PATCH 058/131] remove regex link --- src/flake8/defaults.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/flake8/defaults.py b/src/flake8/defaults.py index 8add695..57abda1 100644 --- a/src/flake8/defaults.py +++ b/src/flake8/defaults.py @@ -36,7 +36,6 @@ NOQA_INLINE_REGEXP = re.compile( # We do not want to capture the ``: `` that follows ``noqa`` # We do not care about the casing of ``noqa`` # We want a comma-separated list of errors - # https://regex101.com/r/4XUuax/2 full explanation of the regex r"# noqa(?::[\s]?(?P([A-Z]+[0-9]+(?:[,\s]+)?)+))?", re.IGNORECASE, ) From a466e461e5cb45b0c0d8c41a75c77f9bbf29786b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 9 May 2023 08:00:29 +0000 Subject: [PATCH 059/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - https://github.com/asottile/reorder_python_imports → https://github.com/asottile/reorder-python-imports - [github.com/asottile/pyupgrade: v3.3.2 → v3.4.0](https://github.com/asottile/pyupgrade/compare/v3.3.2...v3.4.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5994d20..2214c16 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,7 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace exclude: ^tests/fixtures/ -- repo: https://github.com/asottile/reorder_python_imports +- repo: https://github.com/asottile/reorder-python-imports rev: v3.9.0 hooks: - id: reorder-python-imports @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.3.2 + rev: v3.4.0 hooks: - id: pyupgrade args: [--py38-plus] From 4b7ede13fb64288ccc3a361a8fd4560c955cf13a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 16 May 2023 07:06:03 +0000 Subject: [PATCH 060/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.2.0 → v1.3.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.2.0...v1.3.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2214c16..7047121 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.2.0 + rev: v1.3.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 9b6887762bbde293f1e0807fafd997d6213775ff Mon Sep 17 00:00:00 2001 From: Abdulfatai Aka <32123313+abdulfataiaka@users.noreply.github.com> Date: Tue, 16 May 2023 14:44:14 +0100 Subject: [PATCH 061/131] Update invocation.rst file --- docs/source/user/invocation.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/user/invocation.rst b/docs/source/user/invocation.rst index 0049ec9..61cef97 100644 --- a/docs/source/user/invocation.rst +++ b/docs/source/user/invocation.rst @@ -51,7 +51,7 @@ Or This is the last time we will show both versions of an invocation. From now on, we'll simply use ``flake8`` and assume that the user - knows they can instead use ``python -m flake8`` instead. + knows they can instead use ``python -m flake8``. It's also possible to narrow what |Flake8| will try to check by specifying exactly the paths and directories you want it to check. Let's assume that From 43266a2e26ec49a55b866c78b295deaebb1debf7 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 12 Jun 2023 22:02:26 -0400 Subject: [PATCH 062/131] mute FSTRING_MIDDLE tokens --- src/flake8/processor.py | 8 +++++++- tests/integration/test_plugins.py | 32 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 16fa16b..c9c9e5d 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -5,6 +5,7 @@ import argparse import ast import contextlib import logging +import sys import tokenize from typing import Any from typing import Generator @@ -178,7 +179,7 @@ class FileProcessor: self.blank_lines = 0 self.tokens = [] - def build_logical_line_tokens(self) -> _Logical: + def build_logical_line_tokens(self) -> _Logical: # noqa: C901 """Build the mapping, comments, and logical line lists.""" logical = [] comments = [] @@ -195,6 +196,11 @@ class FileProcessor: continue if token_type == tokenize.STRING: text = mutate_string(text) + elif ( + sys.version_info >= (3, 12) + and token_type == tokenize.FSTRING_MIDDLE + ): + text = "x" * len(text) if previous_row: (start_row, start_column) = start if previous_row != start_row: diff --git a/tests/integration/test_plugins.py b/tests/integration/test_plugins.py index edba048..b67e6d6 100644 --- a/tests/integration/test_plugins.py +++ b/tests/integration/test_plugins.py @@ -197,3 +197,35 @@ t.py:3:1: T001 '"""\\n' ''' out, err = capsys.readouterr() assert out == expected + + +def yields_logical_line(logical_line): + yield 0, f"T001 {logical_line!r}" + + +def test_logical_line_plugin(tmpdir, capsys): + cfg_s = f"""\ +[flake8] +extend-ignore = F +[flake8:local-plugins] +extension = + T = {yields_logical_line.__module__}:{yields_logical_line.__name__} +""" + + cfg = tmpdir.join("tox.ini") + cfg.write(cfg_s) + + src = """\ +f'hello world' +""" + t_py = tmpdir.join("t.py") + t_py.write_binary(src.encode()) + + with tmpdir.as_cwd(): + assert main(("t.py", "--config", str(cfg))) == 1 + + expected = """\ +t.py:1:1: T001 "f'xxxxxxxxxxx'" +""" + out, err = capsys.readouterr() + assert out == expected From 4d0c97d7afd3c76cbf99f027896c3e93f963136a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Jun 2023 08:00:37 +0000 Subject: [PATCH 063/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.4.0 → v3.6.0](https://github.com/asottile/pyupgrade/compare/v3.4.0...v3.6.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7047121..75b58c0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.4.0 + rev: v3.6.0 hooks: - id: pyupgrade args: [--py38-plus] From ad03ede045ce35a8390f4d41b62992de381d28e8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Jun 2023 08:45:09 +0000 Subject: [PATCH 064/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/reorder-python-imports: v3.9.0 → v3.10.0](https://github.com/asottile/reorder-python-imports/compare/v3.9.0...v3.10.0) - [github.com/asottile/pyupgrade: v3.6.0 → v3.7.0](https://github.com/asottile/pyupgrade/compare/v3.6.0...v3.7.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 75b58c0..f15bf88 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/reorder-python-imports - rev: v3.9.0 + rev: v3.10.0 hooks: - id: reorder-python-imports args: [ @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.6.0 + rev: v3.7.0 hooks: - id: pyupgrade args: [--py38-plus] From 24999ec87e6e8cd5ddaa815f575467f6ed8bb33e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 27 Jun 2023 07:48:29 +0000 Subject: [PATCH 065/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.3.0 → v1.4.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.3.0...v1.4.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f15bf88..6d2b716 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.3.0 + rev: v1.4.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From fa42096dfe62fc6d4059b70042e27e4e59353984 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Jul 2023 09:39:09 +0000 Subject: [PATCH 066/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.7.0 → v3.8.0](https://github.com/asottile/pyupgrade/compare/v3.7.0...v3.8.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6d2b716..d8c9662 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.7.0 + rev: v3.8.0 hooks: - id: pyupgrade args: [--py38-plus] From 73825ae81c9e0085efc7d10cdec7eb2917d81b9e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Jul 2023 10:15:18 +0000 Subject: [PATCH 067/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.8.0 → v3.9.0](https://github.com/asottile/pyupgrade/compare/v3.8.0...v3.9.0) - [github.com/psf/black: 23.3.0 → 23.7.0](https://github.com/psf/black/compare/23.3.0...23.7.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d8c9662..dbe6918 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,12 +17,12 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.8.0 + rev: v3.9.0 hooks: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 23.3.0 + rev: 23.7.0 hooks: - id: black args: [--line-length=79] From 5bd63bc5528cf0c89ed93c353e65b1089836c105 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 13:03:38 -0400 Subject: [PATCH 068/131] upgrade pyflakes to 3.1.x --- docs/source/user/error-codes.rst | 2 -- setup.cfg | 2 +- src/flake8/plugins/pyflakes.py | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/source/user/error-codes.rst b/docs/source/user/error-codes.rst index d12a07f..2a91413 100644 --- a/docs/source/user/error-codes.rst +++ b/docs/source/user/error-codes.rst @@ -81,8 +81,6 @@ generates its own :term:`error code`\ s for ``pyflakes``: +------+---------------------------------------------------------------------+ | F702 | a ``continue`` statement outside of a ``while`` or ``for`` loop | +------+---------------------------------------------------------------------+ -| F703 | a ``continue`` statement in a ``finally`` block in a loop | -+------+---------------------------------------------------------------------+ | F704 | a ``yield`` or ``yield from`` statement outside of a function | +------+---------------------------------------------------------------------+ | F706 | a ``return`` statement outside of a function/method | diff --git a/setup.cfg b/setup.cfg index 6c28b90..15dcfeb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,7 +36,7 @@ package_dir = install_requires = mccabe>=0.7.0,<0.8.0 pycodestyle>=2.10.0,<2.11.0 - pyflakes>=3.0.0,<3.1.0 + pyflakes>=3.1.0,<3.2.0 # 3.8.0's importlib.metadata is broken python_requires = >=3.8.1 diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index f165c95..f62527e 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -48,7 +48,6 @@ FLAKE8_PYFLAKES_CODES = { "IfTuple": "F634", "BreakOutsideLoop": "F701", "ContinueOutsideLoop": "F702", - "ContinueInFinally": "F703", "YieldOutsideFunction": "F704", "ReturnOutsideFunction": "F706", "DefaultExceptNotLast": "F707", From 9786562feb573d30c73f18e1a0a6685c8584e9b5 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 13:23:01 -0400 Subject: [PATCH 069/131] upgrade to pycodestyle 2.11.x --- setup.cfg | 2 +- src/flake8/plugins/pycodestyle.py | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/setup.cfg b/setup.cfg index 15dcfeb..593c2e1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,7 +35,7 @@ package_dir = # https://flake8.pycqa.org/en/latest/internal/releases.html#releasing-flake8 install_requires = mccabe>=0.7.0,<0.8.0 - pycodestyle>=2.10.0,<2.11.0 + pycodestyle>=2.11.0,<2.12.0 pyflakes>=3.1.0,<3.2.0 # 3.8.0's importlib.metadata is broken python_requires = >=3.8.1 diff --git a/src/flake8/plugins/pycodestyle.py b/src/flake8/plugins/pycodestyle.py index 7eb90e1..9e1d2bb 100644 --- a/src/flake8/plugins/pycodestyle.py +++ b/src/flake8/plugins/pycodestyle.py @@ -23,9 +23,7 @@ from pycodestyle import maximum_doc_length as _maximum_doc_length from pycodestyle import maximum_line_length as _maximum_line_length from pycodestyle import missing_whitespace as _missing_whitespace from pycodestyle import missing_whitespace_after_keyword as _missing_whitespace_after_keyword # noqa: E501 -from pycodestyle import missing_whitespace_around_operator as _missing_whitespace_around_operator # noqa: E501 from pycodestyle import module_imports_on_top_of_file as _module_imports_on_top_of_file # noqa: E501 -from pycodestyle import python_3000_async_await_keywords as _python_3000_async_await_keywords # noqa: E501 from pycodestyle import python_3000_invalid_escape_sequence as _python_3000_invalid_escape_sequence # noqa: E501 from pycodestyle import tabs_obsolete as _tabs_obsolete from pycodestyle import tabs_or_spaces as _tabs_or_spaces @@ -74,11 +72,9 @@ def pycodestyle_logical( yield from _imports_on_separate_lines(logical_line) yield from _indentation(logical_line, previous_logical, indent_char, indent_level, previous_indent_level, indent_size) # noqa: E501 yield from _maximum_doc_length(logical_line, max_doc_length, noqa, tokens) - yield from _missing_whitespace(logical_line) + yield from _missing_whitespace(logical_line, tokens) yield from _missing_whitespace_after_keyword(logical_line, tokens) - yield from _missing_whitespace_around_operator(logical_line, tokens) yield from _module_imports_on_top_of_file(logical_line, indent_level, checker_state, noqa) # noqa: E501 - yield from _python_3000_async_await_keywords(logical_line, tokens) yield from _python_3000_invalid_escape_sequence(logical_line, tokens, noqa) yield from _whitespace_around_comma(logical_line) yield from _whitespace_around_keywords(logical_line) From acca35b8006e2760ceed58c1fbec34fbac024ff2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 14:30:55 -0400 Subject: [PATCH 070/131] add 3.12 to ci matrix --- .github/workflows/main.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7f28cf9..e210204 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,6 +27,9 @@ jobs: - os: ubuntu-latest python: '3.11' toxenv: py + - os: ubuntu-latest + python: '3.12-dev' + toxenv: py # windows - os: windows-latest python: 3.8 From 1ed78d592a355ece4edfee2884185dccaa732dbc Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 14:25:54 -0400 Subject: [PATCH 071/131] handle multiline fstrings in 3.12 --- src/flake8/_compat.py | 11 +++++++ src/flake8/checker.py | 15 +++++---- src/flake8/processor.py | 53 +++++++++++++++---------------- tests/integration/test_plugins.py | 32 +++++++++++++++++++ tests/unit/test_file_processor.py | 25 ++++----------- 5 files changed, 83 insertions(+), 53 deletions(-) create mode 100644 src/flake8/_compat.py diff --git a/src/flake8/_compat.py b/src/flake8/_compat.py new file mode 100644 index 0000000..f4a0903 --- /dev/null +++ b/src/flake8/_compat.py @@ -0,0 +1,11 @@ +from __future__ import annotations + +import sys +import tokenize + +if sys.version_info >= (3, 12): + FSTRING_START = tokenize.FSTRING_START + FSTRING_MIDDLE = tokenize.FSTRING_MIDDLE + FSTRING_END = tokenize.FSTRING_END +else: + FSTRING_START = FSTRING_MIDDLE = FSTRING_END = -1 diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 8d70034..6c4caef 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -20,6 +20,7 @@ from flake8 import defaults from flake8 import exceptions from flake8 import processor from flake8 import utils +from flake8._compat import FSTRING_START from flake8.discover_files import expand_paths from flake8.options.parse_args import parse_args from flake8.plugins.finder import Checkers @@ -551,15 +552,17 @@ class FileChecker: ) -> None: """Run physical checks if and only if it is at the end of the line.""" assert self.processor is not None + if token.type == FSTRING_START: # pragma: >=3.12 cover + self.processor.fstring_start(token.start[0]) # a newline token ends a single physical line. - if processor.is_eol_token(token): + elif processor.is_eol_token(token): # if the file does not end with a newline, the NEWLINE # token is inserted by the parser, but it does not contain # the previous physical line in `token[4]` - if token[4] == "": + if token.line == "": self.run_physical_checks(prev_physical) else: - self.run_physical_checks(token[4]) + self.run_physical_checks(token.line) elif processor.is_multiline_string(token): # Less obviously, a string that contains newlines is a # multiline string, either triple-quoted or with internal @@ -572,10 +575,8 @@ class FileChecker: # - have to wind self.line_number back because initially it # points to the last line of the string, and we want # check_physical() to give accurate feedback - line_no = token[2][0] - with self.processor.inside_multiline(line_number=line_no): - for line in self.processor.split_line(token): - self.run_physical_checks(line) + for line in self.processor.multiline_string(token): + self.run_physical_checks(line) def _try_initialize_processpool( diff --git a/src/flake8/processor.py b/src/flake8/processor.py index c9c9e5d..2eea88f 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -3,9 +3,7 @@ from __future__ import annotations import argparse import ast -import contextlib import logging -import sys import tokenize from typing import Any from typing import Generator @@ -14,6 +12,8 @@ from typing import Tuple from flake8 import defaults from flake8 import utils +from flake8._compat import FSTRING_END +from flake8._compat import FSTRING_MIDDLE from flake8.plugins.finder import LoadedPlugin LOG = logging.getLogger(__name__) @@ -117,6 +117,7 @@ class FileProcessor: self._file_tokens: list[tokenize.TokenInfo] | None = None # map from line number to the line we'll search for `noqa` in self._noqa_line_mapping: dict[int, str] | None = None + self._fstring_start = -1 @property def file_tokens(self) -> list[tokenize.TokenInfo]: @@ -129,14 +130,26 @@ class FileProcessor: return self._file_tokens - @contextlib.contextmanager - def inside_multiline( - self, line_number: int - ) -> Generator[None, None, None]: - """Context-manager to toggle the multiline attribute.""" - self.line_number = line_number + def fstring_start(self, lineno: int) -> None: + """Signal the beginning of an fstring.""" + self._fstring_start = lineno + + def multiline_string( + self, token: tokenize.TokenInfo + ) -> Generator[str, None, None]: + """Iterate through the lines of a multiline string.""" + if token.type == FSTRING_END: + start = self._fstring_start + else: + start = token.start[0] + self.multiline = True - yield + self.line_number = start + # intentionally don't include the last line, that line will be + # terminated later by a future end-of-line + for _ in range(start, token.end[0]): + yield self.lines[self.line_number - 1] + self.line_number += 1 self.multiline = False def reset_blank_before(self) -> None: @@ -196,10 +209,7 @@ class FileProcessor: continue if token_type == tokenize.STRING: text = mutate_string(text) - elif ( - sys.version_info >= (3, 12) - and token_type == tokenize.FSTRING_MIDDLE - ): + elif token_type == FSTRING_MIDDLE: text = "x" * len(text) if previous_row: (start_row, start_column) = start @@ -231,19 +241,6 @@ class FileProcessor: self.statistics["logical lines"] += 1 return joined_comments, self.logical_line, mapping_list - def split_line( - self, token: tokenize.TokenInfo - ) -> Generator[str, None, None]: - """Split a physical line's line based on new-lines. - - This also auto-increments the line number for the caller. - """ - # intentionally don't include the last line, that line will be - # terminated later by a future end-of-line - for line_no in range(token.start[0], token.end[0]): - yield self.lines[line_no - 1] - self.line_number += 1 - def keyword_arguments_for( self, parameters: dict[str, bool], @@ -398,7 +395,9 @@ def is_eol_token(token: tokenize.TokenInfo) -> bool: def is_multiline_string(token: tokenize.TokenInfo) -> bool: """Check if this is a multiline string.""" - return token[0] == tokenize.STRING and "\n" in token[1] + return token.type == FSTRING_END or ( + token.type == tokenize.STRING and "\n" in token.string + ) def token_is_newline(token: tokenize.TokenInfo) -> bool: diff --git a/tests/integration/test_plugins.py b/tests/integration/test_plugins.py index b67e6d6..d4c22b0 100644 --- a/tests/integration/test_plugins.py +++ b/tests/integration/test_plugins.py @@ -199,6 +199,38 @@ t.py:3:1: T001 '"""\\n' assert out == expected +def test_physical_line_plugin_multiline_fstring(tmpdir, capsys): + cfg_s = f"""\ +[flake8:local-plugins] +extension = + T = {yields_physical_line.__module__}:{yields_physical_line.__name__} +""" + + cfg = tmpdir.join("tox.ini") + cfg.write(cfg_s) + + src = '''\ +y = 1 +x = f""" +hello {y} +""" +''' + t_py = tmpdir.join("t.py") + t_py.write_binary(src.encode()) + + with tmpdir.as_cwd(): + assert main(("t.py", "--config", str(cfg))) == 1 + + expected = '''\ +t.py:1:1: T001 'y = 1\\n' +t.py:2:1: T001 'x = f"""\\n' +t.py:3:1: T001 'hello {y}\\n' +t.py:4:1: T001 '"""\\n' +''' + out, err = capsys.readouterr() + assert out == expected + + def yields_logical_line(logical_line): yield 0, f"T001 {logical_line!r}" diff --git a/tests/unit/test_file_processor.py b/tests/unit/test_file_processor.py index bd693b3..a90c628 100644 --- a/tests/unit/test_file_processor.py +++ b/tests/unit/test_file_processor.py @@ -275,13 +275,15 @@ def test_processor_split_line(default_options): (3, 3), 'x = """\ncontents\n"""\n', ) - expected = [('x = """\n', 0), ("contents\n", 1)] + expected = [('x = """\n', 1, True), ("contents\n", 2, True)] + assert file_processor.multiline is False actual = [ - (line, file_processor.line_number) - for line in file_processor.split_line(token) + (line, file_processor.line_number, file_processor.multiline) + for line in file_processor.multiline_string(token) ] + assert file_processor.multiline is False assert expected == actual - assert file_processor.line_number == 2 + assert file_processor.line_number == 3 def test_build_ast(default_options): @@ -321,21 +323,6 @@ def test_visited_new_blank_line(default_options): assert file_processor.blank_lines == 1 -def test_inside_multiline(default_options): - """Verify we update the line number and reset multiline.""" - file_processor = processor.FileProcessor( - "-", default_options, lines=["a = 1\n"] - ) - - assert file_processor.multiline is False - assert file_processor.line_number == 0 - with file_processor.inside_multiline(10): - assert file_processor.multiline is True - assert file_processor.line_number == 10 - - assert file_processor.multiline is False - - @pytest.mark.parametrize( "string, expected", [ From cc301ed499511c56ceca3262a2302364dc146b06 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 14:36:44 -0400 Subject: [PATCH 072/131] fix pylintrc warnings --- .pylintrc | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.pylintrc b/.pylintrc index a23de97..92ea2a4 100644 --- a/.pylintrc +++ b/.pylintrc @@ -365,10 +365,3 @@ ext-import-graph= # Create a graph of internal dependencies in the given file (report RP0402 must # not be disabled) int-import-graph= - - -[EXCEPTIONS] - -# Exceptions that will emit a warning when being caught. Defaults to -# "Exception" -overgeneral-exceptions=Exception From 5233d880696a6867b53fabf0c9d4419d83f372ad Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 14:39:06 -0400 Subject: [PATCH 073/131] use setup-cfg-fmt --- .pre-commit-config.yaml | 4 ++++ setup.cfg | 11 +++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dbe6918..6de9c1b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,6 +7,10 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace exclude: ^tests/fixtures/ +- repo: https://github.com/asottile/setup-cfg-fmt + rev: v2.4.0 + hooks: + - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder-python-imports rev: v3.10.0 hooks: diff --git a/setup.cfg b/setup.cfg index 593c2e1..ebf4355 100644 --- a/setup.cfg +++ b/setup.cfg @@ -10,7 +10,7 @@ author_email = tarek@ziade.org maintainer = Ian Stapleton Cordasco maintainer_email = graffatcolmingov@gmail.com license = MIT -license_file = LICENSE +license_files = LICENSE classifiers = Development Status :: 5 - Production/Stable Environment :: Console @@ -27,18 +27,13 @@ classifiers = [options] packages = find: -package_dir = - =src -# We document the reasoning for using ranges here: -# https://flake8.pycqa.org/en/latest/faq.html#why-does-flake8-use-ranges-for-its-dependencies -# And in which releases we will update those ranges here: -# https://flake8.pycqa.org/en/latest/internal/releases.html#releasing-flake8 install_requires = mccabe>=0.7.0,<0.8.0 pycodestyle>=2.11.0,<2.12.0 pyflakes>=3.1.0,<3.2.0 -# 3.8.0's importlib.metadata is broken python_requires = >=3.8.1 +package_dir = + =src [options.packages.find] where = src From 7ef0350a439c93166bc8ba89fcc3de6a9a664e6c Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 15:04:17 -0400 Subject: [PATCH 074/131] Release 6.1.0 --- docs/source/internal/releases.rst | 4 ++-- docs/source/release-notes/6.1.0.rst | 22 ++++++++++++++++++++++ docs/source/release-notes/index.rst | 1 + src/flake8/__init__.py | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 docs/source/release-notes/6.1.0.rst diff --git a/docs/source/internal/releases.rst b/docs/source/internal/releases.rst index 39fbdf7..fc29bd3 100644 --- a/docs/source/internal/releases.rst +++ b/docs/source/internal/releases.rst @@ -81,9 +81,9 @@ for users. Before releasing, the following tox test environments must pass: -- Python 3.6 (a.k.a., ``tox -e py36``) +- Python 3.8 (a.k.a., ``tox -e py38``) -- Python 3.7 (a.k.a., ``tox -e py37``) +- Python 3.12 (a.k.a., ``tox -e py312``) - PyPy 3 (a.k.a., ``tox -e pypy3``) diff --git a/docs/source/release-notes/6.1.0.rst b/docs/source/release-notes/6.1.0.rst new file mode 100644 index 0000000..a2244a9 --- /dev/null +++ b/docs/source/release-notes/6.1.0.rst @@ -0,0 +1,22 @@ +6.1.0 -- 2023-07-29 +------------------- + +You can view the `6.1.0 milestone`_ on GitHub for more details. + +New Dependency Information +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- Pyflakes has been updated to >= 3.1.0, < 3.2.0 (See also :pull:`1847`). +- pycodestyle has been updated to >= 2.11.0, < 2.12.0 (See also :pull:`1848`). + +Features +~~~~~~~~ + +- Deprecate ``--include-in-doctest``, ``--exclude-from-doctest`` (See also + :issue:`1747`, :pull:`1768`). +- Add support for python 3.12 (See also :pull:`1832`, :pull:`1849`, + :pull:`1850`). + +.. all links +.. _6.1.0 milestone: + https://github.com/PyCQA/flake8/milestone/48 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index a9eff5f..8cd7573 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -10,6 +10,7 @@ with the newest releases first. .. toctree:: 6.0.0 + 6.1.0 5.x Release Series ================== diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index c6d48b7..171b1db 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ import sys LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "6.0.0" +__version__ = "6.1.0" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From 4a47bab979b251324aa31fc3db61e64c8ed3d15d Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 15:43:21 -0400 Subject: [PATCH 075/131] improve coverage --- .coveragerc | 26 ------------- setup.cfg | 9 +++++ src/flake8/_compat.py | 4 +- src/flake8/processor.py | 81 +++++++++++++++++------------------------ tox.ini | 1 + 5 files changed, 45 insertions(+), 76 deletions(-) delete mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc deleted file mode 100644 index d74ca19..0000000 --- a/.coveragerc +++ /dev/null @@ -1,26 +0,0 @@ -[run] -branch = True -source = - flake8 - tests -omit = - # Don't complain if non-runnable code isn't run - */__main__.py - -[report] -show_missing = True -skip_covered = True -exclude_lines = - # Have to re-enable the standard pragma - \#\s*pragma: no cover - - # Don't complain if tests don't hit defensive assertion code: - ^\s*raise AssertionError\b - ^\s*raise NotImplementedError\b - ^\s*return NotImplemented\b - ^\s*raise$ - - # Don't complain if non-runnable code isn't run: - ^if __name__ == ['"]__main__['"]:$ - ^\s*if False: - ^\s*if TYPE_CHECKING: diff --git a/setup.cfg b/setup.cfg index ebf4355..70c2b8d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -54,6 +54,15 @@ flake8.report = [bdist_wheel] universal = 1 +[coverage:run] +source = + flake8 + tests +plugins = covdefaults + +[coverage:report] +fail_under = 97 + [mypy] check_untyped_defs = true disallow_any_generics = true diff --git a/src/flake8/_compat.py b/src/flake8/_compat.py index f4a0903..e8a3ccd 100644 --- a/src/flake8/_compat.py +++ b/src/flake8/_compat.py @@ -3,9 +3,9 @@ from __future__ import annotations import sys import tokenize -if sys.version_info >= (3, 12): +if sys.version_info >= (3, 12): # pragma: >=3.12 cover FSTRING_START = tokenize.FSTRING_START FSTRING_MIDDLE = tokenize.FSTRING_MIDDLE FSTRING_END = tokenize.FSTRING_END -else: +else: # pragma: <3.12 cover FSTRING_START = FSTRING_MIDDLE = FSTRING_END = -1 diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 2eea88f..21a25e0 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse import ast +import functools import logging import tokenize from typing import Any @@ -114,23 +115,15 @@ class FileProcessor: self.verbose = options.verbose #: Statistics dictionary self.statistics = {"logical lines": 0} - self._file_tokens: list[tokenize.TokenInfo] | None = None - # map from line number to the line we'll search for `noqa` in - self._noqa_line_mapping: dict[int, str] | None = None self._fstring_start = -1 - @property + @functools.cached_property def file_tokens(self) -> list[tokenize.TokenInfo]: """Return the complete set of tokens for a file.""" - if self._file_tokens is None: - line_iter = iter(self.lines) - self._file_tokens = list( - tokenize.generate_tokens(lambda: next(line_iter)) - ) + line_iter = iter(self.lines) + return list(tokenize.generate_tokens(lambda: next(line_iter))) - return self._file_tokens - - def fstring_start(self, lineno: int) -> None: + def fstring_start(self, lineno: int) -> None: # pragma: >=3.12 cover """Signal the beginning of an fstring.""" self._fstring_start = lineno @@ -138,7 +131,7 @@ class FileProcessor: self, token: tokenize.TokenInfo ) -> Generator[str, None, None]: """Iterate through the lines of a multiline string.""" - if token.type == FSTRING_END: + if token.type == FSTRING_END: # pragma: >=3.12 cover start = self._fstring_start else: start = token.start[0] @@ -209,7 +202,7 @@ class FileProcessor: continue if token_type == tokenize.STRING: text = mutate_string(text) - elif token_type == FSTRING_MIDDLE: + elif token_type == FSTRING_MIDDLE: # pragma: >=3.12 cover text = "x" * len(text) if previous_row: (start_row, start_column) = start @@ -277,41 +270,37 @@ class FileProcessor: joined = "".join(self.lines[min_line - 1 : max_line]) return dict.fromkeys(line_range, joined) - def noqa_line_for(self, line_number: int) -> str | None: - """Retrieve the line which will be used to determine noqa.""" - if self._noqa_line_mapping is None: - try: - file_tokens = self.file_tokens - except (tokenize.TokenError, SyntaxError): - # if we failed to parse the file tokens, we'll always fail in - # the future, so set this so the code does not try again - self._noqa_line_mapping = {} - else: - ret = {} + @functools.cached_property + def _noqa_line_mapping(self) -> dict[int, str]: + """Map from line number to the line we'll search for `noqa` in.""" + try: + file_tokens = self.file_tokens + except (tokenize.TokenError, SyntaxError): + # if we failed to parse the file tokens, we'll always fail in + # the future, so set this so the code does not try again + return {} + else: + ret = {} - min_line = len(self.lines) + 2 - max_line = -1 - for tp, _, (s_line, _), (e_line, _), _ in file_tokens: - if tp == tokenize.ENDMARKER: - break + min_line = len(self.lines) + 2 + max_line = -1 + for tp, _, (s_line, _), (e_line, _), _ in file_tokens: + if tp == tokenize.ENDMARKER or tp == tokenize.DEDENT: + continue - min_line = min(min_line, s_line) - max_line = max(max_line, e_line) + min_line = min(min_line, s_line) + max_line = max(max_line, e_line) - if tp in (tokenize.NL, tokenize.NEWLINE): - ret.update(self._noqa_line_range(min_line, max_line)) - - min_line = len(self.lines) + 2 - max_line = -1 - - # in newer versions of python, a `NEWLINE` token is inserted - # at the end of the file even if it doesn't have one. - # on old pythons, they will not have hit a `NEWLINE` - if max_line != -1: + if tp in (tokenize.NL, tokenize.NEWLINE): ret.update(self._noqa_line_range(min_line, max_line)) - self._noqa_line_mapping = ret + min_line = len(self.lines) + 2 + max_line = -1 + return ret + + def noqa_line_for(self, line_number: int) -> str | None: + """Retrieve the line which will be used to determine noqa.""" # NOTE(sigmavirus24): Some plugins choose to report errors for empty # files on Line 1. In those cases, we shouldn't bother trying to # retrieve a physical line (since none exist). @@ -377,12 +366,8 @@ class FileProcessor: # If we have nothing to analyze quit early return - first_byte = ord(self.lines[0][0]) - if first_byte not in (0xEF, 0xFEFF): - return - # If the first byte of the file is a UTF-8 BOM, strip it - if first_byte == 0xFEFF: + if self.lines[0][:1] == "\uFEFF": self.lines[0] = self.lines[0][1:] elif self.lines[0][:3] == "\xEF\xBB\xBF": self.lines[0] = self.lines[0][3:] diff --git a/tox.ini b/tox.ini index aae002d..539b5c4 100644 --- a/tox.ini +++ b/tox.ini @@ -6,6 +6,7 @@ envlist = py,flake8,linters,docs deps = pytest!=3.0.5,!=5.2.3 coverage>=6 + covdefaults commands = coverage run -m pytest {posargs} coverage report From b3e251512238ad39c87e4b8d1614903fe493f7ea Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Jul 2023 15:53:22 -0400 Subject: [PATCH 076/131] remove --include-in-doctest and --exclude-in-doctest --- docs/source/user/options.rst | 60 ------------------------ src/flake8/plugins/pyflakes.py | 85 ++-------------------------------- 2 files changed, 3 insertions(+), 142 deletions(-) diff --git a/docs/source/user/options.rst b/docs/source/user/options.rst index 8db2df8..d767748 100644 --- a/docs/source/user/options.rst +++ b/docs/source/user/options.rst @@ -98,10 +98,6 @@ Index of Options - :option:`flake8 --doctests` -- :option:`flake8 --include-in-doctest` - -- :option:`flake8 --exclude-from-doctest` - - :option:`flake8 --benchmark` - :option:`flake8 --bug-report` @@ -997,62 +993,6 @@ Options and their Descriptions doctests = True -.. option:: --include-in-doctest= - - :ref:`Go back to index ` - - Specify which files are checked by PyFlakes for doctest syntax. - - This is registered by the default PyFlakes plugin. - - Command-line example: - - .. prompt:: bash - - flake8 --include-in-doctest=dir/subdir/file.py,dir/other/file.py dir/ - - This **can** be specified in config files. - - Example config file usage: - - .. code-block:: ini - - include-in-doctest = - dir/subdir/file.py, - dir/other/file.py - include_in_doctest = - dir/subdir/file.py, - dir/other/file.py - - -.. option:: --exclude-from-doctest= - - :ref:`Go back to index ` - - Specify which files are not to be checked by PyFlakes for doctest syntax. - - This is registered by the default PyFlakes plugin. - - Command-line example: - - .. prompt:: bash - - flake8 --exclude-from-doctest=dir/subdir/file.py,dir/other/file.py dir/ - - This **can** be specified in config files. - - Example config file usage: - - .. code-block:: ini - - exclude-from-doctest = - dir/subdir/file.py, - dir/other/file.py - exclude_from_doctest = - dir/subdir/file.py, - dir/other/file.py - - .. option:: --benchmark :ref:`Go back to index ` diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index f62527e..6c57619 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -4,13 +4,11 @@ from __future__ import annotations import argparse import ast import logging -import os from typing import Any from typing import Generator import pyflakes.checker -from flake8 import utils from flake8.options.manager import OptionManager LOG = logging.getLogger(__name__) @@ -68,34 +66,12 @@ class FlakesChecker(pyflakes.checker.Checker): """Subclass the Pyflakes checker to conform with the flake8 API.""" with_doctest = False - include_in_doctest: list[str] = [] - exclude_from_doctest: list[str] = [] def __init__(self, tree: ast.AST, filename: str) -> None: """Initialize the PyFlakes plugin with an AST tree and filename.""" - filename = utils.normalize_path(filename) - with_doctest = self.with_doctest - included_by = [ - include - for include in self.include_in_doctest - if include != "" and filename.startswith(include) - ] - if included_by: - with_doctest = True - - for exclude in self.exclude_from_doctest: - if exclude != "" and filename.startswith(exclude): - with_doctest = False - overlapped_by = [ - include - for include in included_by - if include.startswith(exclude) - ] - - if overlapped_by: - with_doctest = True - - super().__init__(tree, filename=filename, withDoctest=with_doctest) + super().__init__( + tree, filename=filename, withDoctest=self.with_doctest + ) @classmethod def add_options(cls, parser: OptionManager) -> None: @@ -113,24 +89,6 @@ class FlakesChecker(pyflakes.checker.Checker): parse_from_config=True, help="also check syntax of the doctests", ) - parser.add_option( - "--include-in-doctest", - default="", - dest="include_in_doctest", - parse_from_config=True, - comma_separated_list=True, - normalize_paths=True, - help="Run doctests only on these files", - ) - parser.add_option( - "--exclude-from-doctest", - default="", - dest="exclude_from_doctest", - parse_from_config=True, - comma_separated_list=True, - normalize_paths=True, - help="Skip these files when running doctests", - ) @classmethod def parse_options(cls, options: argparse.Namespace) -> None: @@ -139,43 +97,6 @@ class FlakesChecker(pyflakes.checker.Checker): cls.builtIns = cls.builtIns.union(options.builtins) cls.with_doctest = options.doctests - if options.include_in_doctest or options.exclude_from_doctest: - LOG.warning( - "--include-in-doctest / --exclude-from-doctest will be " - "removed in a future version. see PyCQA/flake8#1747" - ) - - included_files = [] - for included_file in options.include_in_doctest: - if included_file == "": - continue - if not included_file.startswith((os.sep, "./", "~/")): - included_files.append(f"./{included_file}") - else: - included_files.append(included_file) - cls.include_in_doctest = utils.normalize_paths(included_files) - - excluded_files = [] - for excluded_file in options.exclude_from_doctest: - if excluded_file == "": - continue - if not excluded_file.startswith((os.sep, "./", "~/")): - excluded_files.append(f"./{excluded_file}") - else: - excluded_files.append(excluded_file) - cls.exclude_from_doctest = utils.normalize_paths(excluded_files) - - inc_exc = set(cls.include_in_doctest).intersection( - cls.exclude_from_doctest - ) - if inc_exc: - raise ValueError( - f"{inc_exc!r} was specified in both the " - f"include-in-doctest and exclude-from-doctest " - f"options. You are not allowed to specify it in " - f"both for doctesting." - ) - def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]: """Run the plugin.""" for message in self.messages: From d734e316890aad2cab1ad02e8ba8a277f23d7dfb Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 31 Jul 2023 19:27:59 -0400 Subject: [PATCH 077/131] add minimal rtd configuration --- .readthedocs.yaml | 10 ++++++++++ docs/source/requirements.txt | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 .readthedocs.yaml diff --git a/.readthedocs.yaml b/.readthedocs.yaml new file mode 100644 index 0000000..0425dc2 --- /dev/null +++ b/.readthedocs.yaml @@ -0,0 +1,10 @@ +version: 2 + +build: + os: ubuntu-22.04 + tools: + python: "3.11" +python: + install: + - path: . + - requirements: docs/source/requirements.txt diff --git a/docs/source/requirements.txt b/docs/source/requirements.txt index da3b991..93f773e 100644 --- a/docs/source/requirements.txt +++ b/docs/source/requirements.txt @@ -1,4 +1,4 @@ sphinx>=2.1.0,!=3.1.0 -sphinx_rtd_theme -sphinx-prompt +sphinx-rtd-theme>=1.2.2 +sphinx-prompt>=1.5.0 docutils!=0.18 From 5cd0bcb45a22e490545d8741e57557bf4400f488 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Aug 2023 12:27:47 +0000 Subject: [PATCH 078/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.9.0 → v3.10.1](https://github.com/asottile/pyupgrade/compare/v3.9.0...v3.10.1) - [github.com/PyCQA/flake8: 6.0.0 → 6.1.0](https://github.com/PyCQA/flake8/compare/6.0.0...6.1.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6de9c1b..0c8544e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,7 +21,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.9.0 + rev: v3.10.1 hooks: - id: pyupgrade args: [--py38-plus] @@ -31,7 +31,7 @@ repos: - id: black args: [--line-length=79] - repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 + rev: 6.1.0 hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy From 5a5ebaf10a5996de1d759f7088ca9aaec2175f12 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 10:33:21 +0000 Subject: [PATCH 079/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.4.1 → v1.5.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.4.1...v1.5.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0c8544e..10c91d1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.4.1 + rev: v1.5.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From aceddfeabb6a59b8cef318ac29bab15a8bc78b6f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:03:53 +0000 Subject: [PATCH 080/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.5.0 → v1.5.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.5.0...v1.5.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 10c91d1..1bcfab6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.5.0 + rev: v1.5.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From a0f393ca71c7e19b7cf2a30e801f84ceb3c12106 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Sep 2023 13:10:16 +0000 Subject: [PATCH 081/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 23.7.0 → 23.9.1](https://github.com/psf/black/compare/23.7.0...23.9.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1bcfab6..1394545 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,7 +26,7 @@ repos: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 23.7.0 + rev: 23.9.1 hooks: - id: black args: [--line-length=79] From 7899a82c5de1489849e722905661f703d89ece7f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 19 Sep 2023 12:01:32 +0000 Subject: [PATCH 082/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/reorder-python-imports: v3.10.0 → v3.11.0](https://github.com/asottile/reorder-python-imports/compare/v3.10.0...v3.11.0) - [github.com/asottile/pyupgrade: v3.10.1 → v3.11.0](https://github.com/asottile/pyupgrade/compare/v3.10.1...v3.11.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1394545..5969610 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,7 @@ repos: hooks: - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder-python-imports - rev: v3.10.0 + rev: v3.11.0 hooks: - id: reorder-python-imports args: [ @@ -21,7 +21,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.10.1 + rev: v3.11.0 hooks: - id: pyupgrade args: [--py38-plus] From fb5759b37facaee1b8db4a7666ee6b82d2293ee0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Sep 2023 12:18:31 +0000 Subject: [PATCH 083/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.11.0 → v3.13.0](https://github.com/asottile/pyupgrade/compare/v3.11.0...v3.13.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5969610..4783496 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,7 +21,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.11.0 + rev: v3.13.0 hooks: - id: pyupgrade args: [--py38-plus] From 0a67cbc61ff6fece30b3be1d6e089bbc04cc3e5f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:57:35 +0000 Subject: [PATCH 084/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/setup-cfg-fmt: v2.4.0 → v2.5.0](https://github.com/asottile/setup-cfg-fmt/compare/v2.4.0...v2.5.0) - [github.com/asottile/reorder-python-imports: v3.11.0 → v3.12.0](https://github.com/asottile/reorder-python-imports/compare/v3.11.0...v3.12.0) - [github.com/asottile/pyupgrade: v3.13.0 → v3.14.0](https://github.com/asottile/pyupgrade/compare/v3.13.0...v3.14.0) --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4783496..b51f834 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,11 +8,11 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/setup-cfg-fmt - rev: v2.4.0 + rev: v2.5.0 hooks: - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder-python-imports - rev: v3.11.0 + rev: v3.12.0 hooks: - id: reorder-python-imports args: [ @@ -21,7 +21,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.13.0 + rev: v3.14.0 hooks: - id: pyupgrade args: [--py38-plus] From 67c0ecc6df951cf3e37f41db935cb7390c1c1678 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 21:55:48 +0000 Subject: [PATCH 085/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/pre-commit-hooks: v4.4.0 → v4.5.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.4.0...v4.5.0) - [github.com/asottile/pyupgrade: v3.14.0 → v3.15.0](https://github.com/asottile/pyupgrade/compare/v3.14.0...v3.15.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b51f834..595cae4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-yaml - id: debug-statements @@ -21,7 +21,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.14.0 + rev: v3.15.0 hooks: - id: pyupgrade args: [--py38-plus] From 8bdec0b54ef230e9d0fa5ca9554d4a3d6a764cf5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Oct 2023 21:14:00 +0000 Subject: [PATCH 086/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.5.1 → v1.6.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.5.1...v1.6.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 595cae4..372a9ec 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.5.1 + rev: v1.6.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 61d6ca224df709be82fd370bf1a84d0ddd69965f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:45:49 +0000 Subject: [PATCH 087/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 23.9.1 → 23.10.1](https://github.com/psf/black/compare/23.9.1...23.10.1) - [github.com/pre-commit/mirrors-mypy: v1.6.0 → v1.6.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.6.0...v1.6.1) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 372a9ec..92467ac 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,7 +26,7 @@ repos: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 23.9.1 + rev: 23.10.1 hooks: - id: black args: [--line-length=79] @@ -35,7 +35,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.6.0 + rev: v1.6.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From d1b1ec73be7ecbf3cbc55a1a5d07d8e37c725ee1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Nov 2023 21:55:32 +0000 Subject: [PATCH 088/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 23.10.1 → 23.11.0](https://github.com/psf/black/compare/23.10.1...23.11.0) - [github.com/pre-commit/mirrors-mypy: v1.6.1 → v1.7.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.6.1...v1.7.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 92467ac..3dbc531 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,7 +26,7 @@ repos: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 23.10.1 + rev: 23.11.0 hooks: - id: black args: [--line-length=79] @@ -35,7 +35,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.6.1 + rev: v1.7.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 7e1c87554df99a3c779747b30e6a52aace323d4f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 27 Nov 2023 21:09:51 +0000 Subject: [PATCH 089/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.7.0 → v1.7.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.7.0...v1.7.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3dbc531..fa2ea66 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.7.0 + rev: v1.7.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From c8801c129ab3138c4f3db4841d76bb30ed8e3f8c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:54:28 +0000 Subject: [PATCH 090/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 23.11.0 → 23.12.0](https://github.com/psf/black/compare/23.11.0...23.12.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fa2ea66..44f785d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,7 +26,7 @@ repos: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 23.11.0 + rev: 23.12.0 hooks: - id: black args: [--line-length=79] From b67ce03a4a9c9902fea163021a844f34287ee6bc Mon Sep 17 00:00:00 2001 From: Ian Stapleton Cordasco Date: Wed, 20 Dec 2023 06:54:05 -0600 Subject: [PATCH 091/131] Fix bugbear lints --- src/flake8/checker.py | 3 ++- tests/unit/plugins/reporter_test.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 6c4caef..39c43e3 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -70,7 +70,8 @@ def _mp_init(argv: Sequence[str]) -> None: signal.signal(signal.SIGINT, signal.SIG_IGN) try: - _mp_plugins, _mp_options # for `fork` this'll already be set + # for `fork` this'll already be set + _mp_plugins, _mp_options # noqa: B018 except NameError: plugins, options = parse_args(argv) _mp_plugins, _mp_options = plugins.checkers, options diff --git a/tests/unit/plugins/reporter_test.py b/tests/unit/plugins/reporter_test.py index ff4d97f..842465a 100644 --- a/tests/unit/plugins/reporter_test.py +++ b/tests/unit/plugins/reporter_test.py @@ -11,7 +11,7 @@ from flake8.plugins import reporter def _opts(**kwargs): - kwargs.setdefault("quiet", 0), + kwargs.setdefault("quiet", 0) kwargs.setdefault("color", "never") kwargs.setdefault("output_file", None) return argparse.Namespace(**kwargs) From 06c1503842ee90a4cca5ed57908c0f27595a6f4d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Dec 2023 12:54:27 +0000 Subject: [PATCH 092/131] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/flake8/checker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 39c43e3..329a2cc 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -71,7 +71,7 @@ def _mp_init(argv: Sequence[str]) -> None: try: # for `fork` this'll already be set - _mp_plugins, _mp_options # noqa: B018 + _mp_plugins, _mp_options # noqa: B018 except NameError: plugins, options = parse_args(argv) _mp_plugins, _mp_options = plugins.checkers, options From e27611f1eadc16a5bd02125aa8a054c632d3b0c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Dec 2023 21:35:51 +0000 Subject: [PATCH 093/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 23.12.0 → 23.12.1](https://github.com/psf/black/compare/23.12.0...23.12.1) - [github.com/pre-commit/mirrors-mypy: v1.7.1 → v1.8.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.7.1...v1.8.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 44f785d..bbb1833 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,7 +26,7 @@ repos: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/psf/black - rev: 23.12.0 + rev: 23.12.1 hooks: - id: black args: [--line-length=79] @@ -35,7 +35,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.7.1 + rev: v1.8.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From cde8570df3bf4b647dfa65a97613fb325a9f1bbd Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 4 Jan 2024 19:36:48 -0500 Subject: [PATCH 094/131] upgrade pyflakes to 3.2.x --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 70c2b8d..2254902 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,7 +30,7 @@ packages = find: install_requires = mccabe>=0.7.0,<0.8.0 pycodestyle>=2.11.0,<2.12.0 - pyflakes>=3.1.0,<3.2.0 + pyflakes>=3.2.0,<3.3.0 python_requires = >=3.8.1 package_dir = =src From 88a4f9b2f48fc44b025a48fa6a8ac7cc89ef70e0 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 4 Jan 2024 19:41:07 -0500 Subject: [PATCH 095/131] Release 7.0.0 --- docs/source/internal/releases.rst | 2 +- docs/source/release-notes/7.0.0.rst | 19 +++++++++++++++++++ docs/source/release-notes/index.rst | 6 ++++++ src/flake8/__init__.py | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 docs/source/release-notes/7.0.0.rst diff --git a/docs/source/internal/releases.rst b/docs/source/internal/releases.rst index fc29bd3..0081509 100644 --- a/docs/source/internal/releases.rst +++ b/docs/source/internal/releases.rst @@ -30,7 +30,7 @@ Historically, |Flake8| has generated major releases for: - Large scale refactoring (2.0, 3.0, 5.0, 6.0) -- Subtly breaking CLI changes (3.0, 4.0, 5.0, 6.0) +- Subtly breaking CLI changes (3.0, 4.0, 5.0, 6.0, 7.0) - Breaking changes to its plugin interface (3.0) diff --git a/docs/source/release-notes/7.0.0.rst b/docs/source/release-notes/7.0.0.rst new file mode 100644 index 0000000..6cd852a --- /dev/null +++ b/docs/source/release-notes/7.0.0.rst @@ -0,0 +1,19 @@ +7.0.0 -- 2024-01-04 +------------------- + +You can view the `7.0.0 milestone`_ on GitHub for more details. + +Backwards Incompatible Changes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- Remove ``--include-in-doctest`` and ``--exclude-from-doctest`` options. + (See also :issue:`1747`, :pull:`1854`) + +New Dependency Information +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- Pyflakes has been updated to >= 3.2.0, < 3.3.0 (See also :pull:`1906`). + +.. all links +.. _7.0.0 milestone: + https://github.com/PyCQA/flake8/milestone/49 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index 8cd7573..58c6845 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -5,6 +5,12 @@ All of the release notes that have been recorded for Flake8 are organized here with the newest releases first. +7.x Release Series +================== + +.. toctree:: + 7.0.0 + 6.x Release Series ================== diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index 171b1db..ea7a56d 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ import sys LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "6.1.0" +__version__ = "7.0.0" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From 26d3184ae2432eb382980590c77b47bb393ce4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Fri, 5 Jan 2024 12:14:44 +0100 Subject: [PATCH 096/131] update plugins for sphinx-prompt-1.8.0 The sphinx-prompt plugin has renamed its package in 1.8.0 from erraneous `sphinx-prompt` name to `sphinx_prompt`. Adjust the conf accordingly. --- docs/source/conf.py | 2 +- docs/source/requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index a2b4af3..9b7aa5a 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -34,7 +34,7 @@ extensions = [ "sphinx.ext.todo", "sphinx.ext.coverage", "sphinx.ext.viewcode", - "sphinx-prompt", + "sphinx_prompt", ] # Add any paths that contain templates here, relative to this directory. diff --git a/docs/source/requirements.txt b/docs/source/requirements.txt index 93f773e..765fb13 100644 --- a/docs/source/requirements.txt +++ b/docs/source/requirements.txt @@ -1,4 +1,4 @@ sphinx>=2.1.0,!=3.1.0 sphinx-rtd-theme>=1.2.2 -sphinx-prompt>=1.5.0 +sphinx-prompt>=1.8.0 docutils!=0.18 From a2b68c84e760a4906ad47eac84d277c85aaf8322 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 8 Jan 2024 21:35:25 +0000 Subject: [PATCH 097/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/PyCQA/flake8: 6.1.0 → 7.0.0](https://github.com/PyCQA/flake8/compare/6.1.0...7.0.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bbb1833..ad60be1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: - id: black args: [--line-length=79] - repo: https://github.com/PyCQA/flake8 - rev: 6.1.0 + rev: 7.0.0 hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy From 49f52a8598d8a934b07f367a1b3ad87dbe51be5b Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:06:23 +0100 Subject: [PATCH 098/131] Update documentation regarding entry points Refer to the PyPA packaging guide Replace references to the deprecated `pkg_resources` docs --- docs/source/conf.py | 6 +++++- docs/source/plugin-development/index.rst | 7 ++----- .../registering-plugins.rst | 21 ++++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 9b7aa5a..48f8a52 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -296,7 +296,11 @@ texinfo_documents = [ # Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = {"python": ("https://docs.python.org/3/", None)} +intersphinx_mapping = { + "python": ("https://docs.python.org/3/", None), + "packaging": ("https://packaging.python.org/en/latest/", None), + "setuptools": ("https://setuptools.pypa.io/en/latest/", None), +} extlinks = { "issue": ("https://github.com/pycqa/flake8/issues/%s", "#%s"), diff --git a/docs/source/plugin-development/index.rst b/docs/source/plugin-development/index.rst index c89e5f0..82f4a6b 100644 --- a/docs/source/plugin-development/index.rst +++ b/docs/source/plugin-development/index.rst @@ -30,7 +30,8 @@ To get started writing a |Flake8| :term:`plugin` you first need: Once you've gathered these things, you can get started. -All plugins for |Flake8| must be registered via `entry points`_. In this +All plugins for |Flake8| must be registered via +:external:doc:`entry points`. In this section we cover: - How to register your plugin so |Flake8| can find it @@ -62,7 +63,3 @@ Here's a tutorial which goes over building an ast checking plugin from scratch: registering-plugins plugin-parameters formatters - - -.. _entry points: - https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points diff --git a/docs/source/plugin-development/registering-plugins.rst b/docs/source/plugin-development/registering-plugins.rst index ca74008..e5568b8 100644 --- a/docs/source/plugin-development/registering-plugins.rst +++ b/docs/source/plugin-development/registering-plugins.rst @@ -12,16 +12,15 @@ To register any kind of plugin with |Flake8|, you need: #. A name for your plugin that will (ideally) be unique. -#. A somewhat recent version of setuptools (newer than 0.7.0 but preferably as - recent as you can attain). - -|Flake8| relies on functionality provided by setuptools called -`Entry Points`_. These allow any package to register a plugin with |Flake8| -via that package's ``setup.py`` file. +|Flake8| relies on functionality provided by build tools called +:external:doc:`entry points`. These allow +any package to register a plugin with |Flake8| via that package's metadata. Let's presume that we already have our plugin written and it's in a module -called ``flake8_example``. We might have a ``setup.py`` that looks something -like: +called ``flake8_example``. We will also assume ``setuptools`` is used as a +:term:`Build Backend`, but be aware that most backends support entry points. + +We might have a ``setup.py`` that looks something like: .. code-block:: python @@ -150,5 +149,7 @@ If your plugin is intended to be opt-in, it can set the attribute :ref:`enable-extensions` with your plugin's entry point. -.. _Entry Points: - https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points +.. seealso:: + + The :external:doc:`setuptools user guide ` about + entry points. From 350f2545fd3ec75640a1605e4995a2f921e8b38b Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Sun, 17 Mar 2024 08:56:07 +0100 Subject: [PATCH 099/131] Use explicit external references --- docs/source/plugin-development/index.rst | 2 +- .../plugin-development/registering-plugins.rst | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/source/plugin-development/index.rst b/docs/source/plugin-development/index.rst index 82f4a6b..9b38fe2 100644 --- a/docs/source/plugin-development/index.rst +++ b/docs/source/plugin-development/index.rst @@ -31,7 +31,7 @@ To get started writing a |Flake8| :term:`plugin` you first need: Once you've gathered these things, you can get started. All plugins for |Flake8| must be registered via -:external:doc:`entry points`. In this +:external+packaging:doc:`entry points`. In this section we cover: - How to register your plugin so |Flake8| can find it diff --git a/docs/source/plugin-development/registering-plugins.rst b/docs/source/plugin-development/registering-plugins.rst index e5568b8..41c34e0 100644 --- a/docs/source/plugin-development/registering-plugins.rst +++ b/docs/source/plugin-development/registering-plugins.rst @@ -13,12 +13,14 @@ To register any kind of plugin with |Flake8|, you need: #. A name for your plugin that will (ideally) be unique. |Flake8| relies on functionality provided by build tools called -:external:doc:`entry points`. These allow -any package to register a plugin with |Flake8| via that package's metadata. +:external+packaging:doc:`entry points`. These +allow any package to register a plugin with |Flake8| via that package's +metadata. Let's presume that we already have our plugin written and it's in a module +:external+packaging:term:`Build Backend`, but be aware that most backends called ``flake8_example``. We will also assume ``setuptools`` is used as a -:term:`Build Backend`, but be aware that most backends support entry points. +support entry points. We might have a ``setup.py`` that looks something like: @@ -151,5 +153,5 @@ point. .. seealso:: - The :external:doc:`setuptools user guide ` about - entry points. + The :external+setuptools:doc:`setuptools user guide ` + about entry points. From ba0f56610adbd4d8733772ce1c63efcab1b70079 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Sun, 17 Mar 2024 08:56:07 +0100 Subject: [PATCH 100/131] Use explicit external references --- docs/source/plugin-development/registering-plugins.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/plugin-development/registering-plugins.rst b/docs/source/plugin-development/registering-plugins.rst index 41c34e0..aac87e8 100644 --- a/docs/source/plugin-development/registering-plugins.rst +++ b/docs/source/plugin-development/registering-plugins.rst @@ -20,6 +20,7 @@ metadata. Let's presume that we already have our plugin written and it's in a module :external+packaging:term:`Build Backend`, but be aware that most backends called ``flake8_example``. We will also assume ``setuptools`` is used as a +:external+packaging:term:`Build Backend`, but be aware that most backends support entry points. We might have a ``setup.py`` that looks something like: From 6659b213c9aa8fa49235e13a365fcd34f58cbc6b Mon Sep 17 00:00:00 2001 From: Ian Stapleton Cordasco Date: Mon, 18 Mar 2024 10:43:13 -0500 Subject: [PATCH 101/131] Fix toctree ordering in index Also fix line-length issue in PR --- docs/source/plugin-development/index.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/plugin-development/index.rst b/docs/source/plugin-development/index.rst index 9b38fe2..9088942 100644 --- a/docs/source/plugin-development/index.rst +++ b/docs/source/plugin-development/index.rst @@ -55,6 +55,8 @@ Here's a tutorial which goes over building an ast checking plugin from scratch: +Detailed Plugin Development Documentation +========================================= .. toctree:: :caption: Plugin Developer Documentation From 866ad729c64eea359960a8ac4e3f1201104ee55c Mon Sep 17 00:00:00 2001 From: mzagol Date: Mon, 15 Apr 2024 15:52:25 -0500 Subject: [PATCH 102/131] Add --extend-exclude to the TOC --- docs/source/user/options.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/user/options.rst b/docs/source/user/options.rst index d767748..bd80c87 100644 --- a/docs/source/user/options.rst +++ b/docs/source/user/options.rst @@ -46,6 +46,8 @@ Index of Options - :option:`flake8 --exclude` +- :option:`flake8 --extend-exclude` + - :option:`flake8 --filename` - :option:`flake8 --stdin-display-name` From defd315175b7b77472affb61a410e5720dabdc1a Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 15 Jun 2024 17:30:39 -0400 Subject: [PATCH 103/131] latest pycodestyle --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 2254902..28e6f93 100644 --- a/setup.cfg +++ b/setup.cfg @@ -29,7 +29,7 @@ classifiers = packages = find: install_requires = mccabe>=0.7.0,<0.8.0 - pycodestyle>=2.11.0,<2.12.0 + pycodestyle>=2.12.0,<2.13.0 pyflakes>=3.2.0,<3.3.0 python_requires = >=3.8.1 package_dir = From 65a38c42a7f1a05ff8d99b313160754fc9b7a0d8 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 15 Jun 2024 17:36:14 -0400 Subject: [PATCH 104/131] Release 7.1.0 --- docs/source/release-notes/7.1.0.rst | 13 +++++++++++++ docs/source/release-notes/index.rst | 1 + src/flake8/__init__.py | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 docs/source/release-notes/7.1.0.rst diff --git a/docs/source/release-notes/7.1.0.rst b/docs/source/release-notes/7.1.0.rst new file mode 100644 index 0000000..2229baa --- /dev/null +++ b/docs/source/release-notes/7.1.0.rst @@ -0,0 +1,13 @@ +7.1.0 -- 2024-06-15 +------------------- + +You can view the `7.1.0 milestone`_ on GitHub for more details. + +New Dependency Information +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- pycodestyle has been updated to >= 2.12.0, < 2.13.0 (See also :pull:`1939`). + +.. all links +.. _7.1.0 milestone: + https://github.com/PyCQA/flake8/milestone/50 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index 58c6845..4ae650a 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -10,6 +10,7 @@ with the newest releases first. .. toctree:: 7.0.0 + 7.1.0 6.x Release Series ================== diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index ea7a56d..19df059 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ import sys LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "7.0.0" +__version__ = "7.1.0" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From 10314ad9e5236f1ddf70cb25c2854c93c0840b66 Mon Sep 17 00:00:00 2001 From: Victorien <65306057+Viicos@users.noreply.github.com> Date: Tue, 23 Jul 2024 13:39:49 +0200 Subject: [PATCH 105/131] Fix wording of plugins documentation --- docs/source/plugin-development/registering-plugins.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/source/plugin-development/registering-plugins.rst b/docs/source/plugin-development/registering-plugins.rst index aac87e8..964ff99 100644 --- a/docs/source/plugin-development/registering-plugins.rst +++ b/docs/source/plugin-development/registering-plugins.rst @@ -18,7 +18,6 @@ allow any package to register a plugin with |Flake8| via that package's metadata. Let's presume that we already have our plugin written and it's in a module -:external+packaging:term:`Build Backend`, but be aware that most backends called ``flake8_example``. We will also assume ``setuptools`` is used as a :external+packaging:term:`Build Backend`, but be aware that most backends support entry points. From bdcd5c2c0afadaf7c92a4b26d96055cecdd38cf3 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 1 Aug 2024 11:08:06 +0100 Subject: [PATCH 106/131] Handle escaped braces in f-strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To use a curly brace in an f-string, you must escape it. For example: >>> k = 1 >>> f'{{{k}' '{1' Saving this as a script and running the 'tokenize' module highlights something odd around the counting of tokens: ❯ python -m tokenize wow.py 0,0-0,0: ENCODING 'utf-8' 1,0-1,1: NAME 'k' 1,2-1,3: OP '=' 1,4-1,5: NUMBER '1' 1,5-1,6: NEWLINE '\n' 2,0-2,2: FSTRING_START "f'" 2,2-2,3: FSTRING_MIDDLE '{' # <-- here... 2,4-2,5: OP '{' # <-- and here 2,5-2,6: NAME 'k' 2,6-2,7: OP '}' 2,7-2,8: FSTRING_END "'" 2,8-2,9: NEWLINE '\n' 3,0-3,0: ENDMARKER '' The FSTRING_MIDDLE character we have is the escaped/post-parse single curly brace rather than the raw double curly brace, however, while our end index of this token accounts for the parsed form, the start index of the next token does not (put another way, it jumps from 3 -> 4). This triggers some existing, unrelated code that we need to bypass. Do just that. Signed-off-by: Stephen Finucane Closes: #1948 --- src/flake8/processor.py | 8 ++++++- tests/integration/test_plugins.py | 35 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 21a25e0..e44547b 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -203,7 +203,13 @@ class FileProcessor: if token_type == tokenize.STRING: text = mutate_string(text) elif token_type == FSTRING_MIDDLE: # pragma: >=3.12 cover - text = "x" * len(text) + # A curly brace in an FSTRING_MIDDLE token must be an escaped + # curly brace. Both 'text' and 'end' will account for the + # escaped version of the token (i.e. a single brace) rather + # than the raw double brace version, so we must counteract this + brace_offset = text.count("{") + text.count("}") + text = "x" * (len(text) + brace_offset) + end = (end[0], end[1] + brace_offset) if previous_row: (start_row, start_column) = start if previous_row != start_row: diff --git a/tests/integration/test_plugins.py b/tests/integration/test_plugins.py index d4c22b0..90ca555 100644 --- a/tests/integration/test_plugins.py +++ b/tests/integration/test_plugins.py @@ -1,6 +1,8 @@ """Integration tests for plugin loading.""" from __future__ import annotations +import sys + import pytest from flake8.main.cli import main @@ -261,3 +263,36 @@ t.py:1:1: T001 "f'xxxxxxxxxxx'" """ out, err = capsys.readouterr() assert out == expected + + +def test_escaping_of_fstrings_in_string_redacter(tmpdir, capsys): + cfg_s = f"""\ +[flake8] +extend-ignore = F +[flake8:local-plugins] +extension = + T = {yields_logical_line.__module__}:{yields_logical_line.__name__} +""" + + cfg = tmpdir.join("tox.ini") + cfg.write(cfg_s) + + src = """\ +f'{{"{hello}": "{world}"}}' +""" + t_py = tmpdir.join("t.py") + t_py.write_binary(src.encode()) + + with tmpdir.as_cwd(): + assert main(("t.py", "--config", str(cfg))) == 1 + + if sys.version_info >= (3, 12): # pragma: >=3.12 cover + expected = """\ +t.py:1:1: T001 "f'xxx{hello}xxxx{world}xxx'" +""" + else: # pragma: <3.12 cover + expected = """\ +t.py:1:1: T001 "f'xxxxxxxxxxxxxxxxxxxxxxxx'" +""" + out, err = capsys.readouterr() + assert out == expected From cf1542cefa3e766670b2066dd75c4571d682a649 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 4 Aug 2024 16:31:56 -0400 Subject: [PATCH 107/131] Release 7.1.1 --- docs/source/release-notes/7.1.1.rst | 15 +++++++++++++++ docs/source/release-notes/index.rst | 1 + src/flake8/__init__.py | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 docs/source/release-notes/7.1.1.rst diff --git a/docs/source/release-notes/7.1.1.rst b/docs/source/release-notes/7.1.1.rst new file mode 100644 index 0000000..62f2d11 --- /dev/null +++ b/docs/source/release-notes/7.1.1.rst @@ -0,0 +1,15 @@ +7.1.1 -- 2024-08-04 +------------------- + +You can view the `7.1.1 milestone`_ on GitHub for more details. + +Bugs Fixed +~~~~~~~~~~ + +- Properly preserve escaped `{` and `}` in fstrings in logical lines in 3.12+. + (See also :issue:`1948`, :pull:`1949`). + + +.. all links +.. _7.1.1 milestone: + https://github.com/PyCQA/flake8/milestone/51 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index 4ae650a..9bf8646 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -11,6 +11,7 @@ with the newest releases first. .. toctree:: 7.0.0 7.1.0 + 7.1.1 6.x Release Series ================== diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index 19df059..101eafe 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ import sys LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "7.1.0" +__version__ = "7.1.1" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From f35737a32d8283fd0d3456bd9919718ff2d1b077 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 16 Feb 2025 13:29:05 -0500 Subject: [PATCH 108/131] avoid starting unnecessary processes when file count is limited --- src/flake8/checker.py | 1 + tests/unit/test_checker_manager.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 329a2cc..ff62679 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -256,6 +256,7 @@ class Manager: exclude=self.exclude, ) ) + self.jobs = min(len(self.filenames), self.jobs) def stop(self) -> None: """Stop checking files.""" diff --git a/tests/unit/test_checker_manager.py b/tests/unit/test_checker_manager.py index 68dd82a..8d6b3dc 100644 --- a/tests/unit/test_checker_manager.py +++ b/tests/unit/test_checker_manager.py @@ -61,6 +61,16 @@ def test_multiprocessing_cpu_count_not_implemented(): assert manager.jobs == 0 +def test_jobs_count_limited_to_file_count(): + style_guide = style_guide_mock() + style_guide.options.jobs = JobsArgument("4") + style_guide.options.filenames = ["file1", "file2"] + manager = checker.Manager(style_guide, finder.Checkers([], [], []), []) + assert manager.jobs == 4 + manager.start() + assert manager.jobs == 2 + + def test_make_checkers(): """Verify that we create a list of FileChecker instances.""" style_guide = style_guide_mock() From fffee8ba9dc5903484f99390e6c7f4bbef59bda7 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 16 Feb 2025 13:48:15 -0500 Subject: [PATCH 109/131] Release 7.1.2 --- docs/source/release-notes/7.1.2.rst | 15 +++++++++++++++ docs/source/release-notes/index.rst | 1 + src/flake8/__init__.py | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 docs/source/release-notes/7.1.2.rst diff --git a/docs/source/release-notes/7.1.2.rst b/docs/source/release-notes/7.1.2.rst new file mode 100644 index 0000000..010656c --- /dev/null +++ b/docs/source/release-notes/7.1.2.rst @@ -0,0 +1,15 @@ +7.1.2 -- 2025-02-16 +------------------- + +You can view the `7.1.2 milestone`_ on GitHub for more details. + +Bugs Fixed +~~~~~~~~~~ + +- Avoid starting unnecessary processes when "# files" < "jobs". + (See also :pull:`1966`). + + +.. all links +.. _7.1.2 milestone: + https://github.com/PyCQA/flake8/milestone/52 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index 9bf8646..2145179 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -12,6 +12,7 @@ with the newest releases first. 7.0.0 7.1.0 7.1.1 + 7.1.2 6.x Release Series ================== diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index 101eafe..0345499 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ import sys LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "7.1.1" +__version__ = "7.1.2" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From fa2ed7145cdf80fd983c2a436226a074f9a9d664 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 16 Feb 2025 15:21:24 -0500 Subject: [PATCH 110/131] remove a few unnecessary mocks in test_checker_manager noticed while implementing the --jobs limiter --- tests/unit/test_checker_manager.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/unit/test_checker_manager.py b/tests/unit/test_checker_manager.py index 8d6b3dc..593822b 100644 --- a/tests/unit/test_checker_manager.py +++ b/tests/unit/test_checker_manager.py @@ -76,9 +76,5 @@ def test_make_checkers(): style_guide = style_guide_mock() style_guide.options.filenames = ["file1", "file2"] manager = checker.Manager(style_guide, finder.Checkers([], [], []), []) - - with mock.patch("flake8.utils.fnmatch", return_value=True): - with mock.patch("flake8.processor.FileProcessor"): - manager.start() - + manager.start() assert manager.filenames == ("file1", "file2") From 9d55ccdb729d1255b9cf09438b6073b05b9ce52c Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Mar 2025 15:39:58 -0400 Subject: [PATCH 111/131] py39+ --- .github/workflows/main.yml | 10 +++++----- .pre-commit-config.yaml | 10 +++++----- bin/gen-pycodestyle-plugin | 10 +++++----- docs/source/internal/releases.rst | 4 ++-- docs/source/user/invocation.rst | 10 +++++----- example-plugin/setup.py | 2 -- setup.cfg | 2 +- src/flake8/checker.py | 10 ++++------ src/flake8/discover_files.py | 8 ++++---- src/flake8/main/application.py | 2 +- src/flake8/main/cli.py | 2 +- src/flake8/options/aggregator.py | 2 +- src/flake8/options/manager.py | 2 +- src/flake8/options/parse_args.py | 2 +- src/flake8/plugins/finder.py | 12 ++++++------ src/flake8/plugins/pycodestyle.py | 6 +++--- src/flake8/plugins/pyflakes.py | 4 ++-- src/flake8/processor.py | 16 ++++++---------- src/flake8/statistics.py | 4 ++-- src/flake8/style_guide.py | 18 ++++++------------ src/flake8/utils.py | 4 ++-- src/flake8/violation.py | 2 +- 22 files changed, 64 insertions(+), 78 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e210204..08f54ea 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,9 +15,6 @@ jobs: - os: ubuntu-latest python: pypy-3.9 toxenv: py - - os: ubuntu-latest - python: 3.8 - toxenv: py - os: ubuntu-latest python: 3.9 toxenv: py @@ -28,11 +25,14 @@ jobs: python: '3.11' toxenv: py - os: ubuntu-latest - python: '3.12-dev' + python: '3.12' + toxenv: py + - os: ubuntu-latest + python: '3.13' toxenv: py # windows - os: windows-latest - python: 3.8 + python: 3.9 toxenv: py # misc - os: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ad60be1..9df4a79 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,19 +12,19 @@ repos: hooks: - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder-python-imports - rev: v3.12.0 + rev: v3.14.0 hooks: - id: reorder-python-imports args: [ --application-directories, '.:src', - --py38-plus, + --py39-plus, --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.15.0 + rev: v3.19.1 hooks: - id: pyupgrade - args: [--py38-plus] + args: [--py39-plus] - repo: https://github.com/psf/black rev: 23.12.1 hooks: @@ -35,7 +35,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.8.0 + rev: v1.15.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) diff --git a/bin/gen-pycodestyle-plugin b/bin/gen-pycodestyle-plugin index 8bc2efc..c93fbfe 100755 --- a/bin/gen-pycodestyle-plugin +++ b/bin/gen-pycodestyle-plugin @@ -3,9 +3,9 @@ from __future__ import annotations import inspect import os.path +from collections.abc import Generator from typing import Any from typing import Callable -from typing import Generator from typing import NamedTuple import pycodestyle @@ -42,7 +42,7 @@ class Call(NamedTuple): return cls(func.__name__, inspect.isgeneratorfunction(func), params) -def lines() -> Generator[str, None, None]: +def lines() -> Generator[str]: logical = [] physical = [] @@ -58,8 +58,8 @@ def lines() -> Generator[str, None, None]: yield "# fmt: off" yield "from __future__ import annotations" yield "" + yield "from collections.abc import Generator" yield "from typing import Any" - yield "from typing import Generator" yield "" imports = sorted(call.name for call in logical + physical) for name in imports: @@ -71,7 +71,7 @@ def lines() -> Generator[str, None, None]: logical_params = {param for call in logical for param in call.params} for param in sorted(logical_params): yield f" {param}: Any," - yield ") -> Generator[tuple[int, str], None, None]:" + yield ") -> Generator[tuple[int, str]]:" yield ' """Run pycodestyle logical checks."""' for call in sorted(logical): yield call.to_src() @@ -82,7 +82,7 @@ def lines() -> Generator[str, None, None]: physical_params = {param for call in physical for param in call.params} for param in sorted(physical_params): yield f" {param}: Any," - yield ") -> Generator[tuple[int, str], None, None]:" + yield ") -> Generator[tuple[int, str]]:" yield ' """Run pycodestyle physical checks."""' for call in sorted(physical): yield call.to_src() diff --git a/docs/source/internal/releases.rst b/docs/source/internal/releases.rst index 0081509..d71796d 100644 --- a/docs/source/internal/releases.rst +++ b/docs/source/internal/releases.rst @@ -81,9 +81,9 @@ for users. Before releasing, the following tox test environments must pass: -- Python 3.8 (a.k.a., ``tox -e py38``) +- Python 3.9 (a.k.a., ``tox -e py39``) -- Python 3.12 (a.k.a., ``tox -e py312``) +- Python 3.13 (a.k.a., ``tox -e py313``) - PyPy 3 (a.k.a., ``tox -e pypy3``) diff --git a/docs/source/user/invocation.rst b/docs/source/user/invocation.rst index 61cef97..10895dd 100644 --- a/docs/source/user/invocation.rst +++ b/docs/source/user/invocation.rst @@ -14,25 +14,25 @@ like so: Where you simply allow the shell running in your terminal to locate |Flake8|. In some cases, though, you may have installed |Flake8| for multiple versions -of Python (e.g., Python 3.8 and Python 3.9) and you need to call a specific +of Python (e.g., Python 3.13 and Python 3.14) and you need to call a specific version. In that case, you will have much better results using: .. prompt:: bash - python3.8 -m flake8 + python3.13 -m flake8 Or .. prompt:: bash - python3.9 -m flake8 + python3.14 -m flake8 Since that will tell the correct version of Python to run |Flake8|. .. note:: - Installing |Flake8| once will not install it on both Python 3.8 and - Python 3.9. It will only install it for the version of Python that + Installing |Flake8| once will not install it on both Python 3.13 and + Python 3.14. It will only install it for the version of Python that is running pip. It is also possible to specify command-line options directly to |Flake8|: diff --git a/example-plugin/setup.py b/example-plugin/setup.py index c0720bd..9e7c89f 100644 --- a/example-plugin/setup.py +++ b/example-plugin/setup.py @@ -23,8 +23,6 @@ setuptools.setup( "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Software Development :: Quality Assurance", ], diff --git a/setup.cfg b/setup.cfg index 28e6f93..688e349 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,7 +31,7 @@ install_requires = mccabe>=0.7.0,<0.8.0 pycodestyle>=2.12.0,<2.13.0 pyflakes>=3.2.0,<3.3.0 -python_requires = >=3.8.1 +python_requires = >=3.9 package_dir = =src diff --git a/src/flake8/checker.py b/src/flake8/checker.py index ff62679..d1659b7 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -9,12 +9,10 @@ import multiprocessing.pool import operator import signal import tokenize +from collections.abc import Generator +from collections.abc import Sequence from typing import Any -from typing import Generator -from typing import List from typing import Optional -from typing import Sequence -from typing import Tuple from flake8 import defaults from flake8 import exceptions @@ -27,7 +25,7 @@ from flake8.plugins.finder import Checkers from flake8.plugins.finder import LoadedPlugin from flake8.style_guide import StyleGuideManager -Results = List[Tuple[str, int, int, str, Optional[str]]] +Results = list[tuple[str, int, int, str, Optional[str]]] LOG = logging.getLogger(__name__) @@ -53,7 +51,7 @@ _mp_options: argparse.Namespace @contextlib.contextmanager def _mp_prefork( plugins: Checkers, options: argparse.Namespace -) -> Generator[None, None, None]: +) -> Generator[None]: # we can save significant startup work w/ `fork` multiprocessing global _mp_plugins, _mp_options _mp_plugins, _mp_options = plugins, options diff --git a/src/flake8/discover_files.py b/src/flake8/discover_files.py index 580d5fd..da28ba5 100644 --- a/src/flake8/discover_files.py +++ b/src/flake8/discover_files.py @@ -3,9 +3,9 @@ from __future__ import annotations import logging import os.path +from collections.abc import Generator +from collections.abc import Sequence from typing import Callable -from typing import Generator -from typing import Sequence from flake8 import utils @@ -16,7 +16,7 @@ def _filenames_from( arg: str, *, predicate: Callable[[str], bool], -) -> Generator[str, None, None]: +) -> Generator[str]: """Generate filenames from an argument. :param arg: @@ -55,7 +55,7 @@ def expand_paths( stdin_display_name: str, filename_patterns: Sequence[str], exclude: Sequence[str], -) -> Generator[str, None, None]: +) -> Generator[str]: """Expand out ``paths`` from commandline to the lintable files.""" if not paths: paths = ["."] diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index b6bfae3..4704cbd 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -5,7 +5,7 @@ import argparse import json import logging import time -from typing import Sequence +from collections.abc import Sequence import flake8 from flake8 import checker diff --git a/src/flake8/main/cli.py b/src/flake8/main/cli.py index 01a67ac..1a52f36 100644 --- a/src/flake8/main/cli.py +++ b/src/flake8/main/cli.py @@ -2,7 +2,7 @@ from __future__ import annotations import sys -from typing import Sequence +from collections.abc import Sequence from flake8.main import application diff --git a/src/flake8/options/aggregator.py b/src/flake8/options/aggregator.py index af8e744..999161a 100644 --- a/src/flake8/options/aggregator.py +++ b/src/flake8/options/aggregator.py @@ -8,7 +8,7 @@ from __future__ import annotations import argparse import configparser import logging -from typing import Sequence +from collections.abc import Sequence from flake8.options import config from flake8.options.manager import OptionManager diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index 4fd26b2..cb195fe 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -5,9 +5,9 @@ import argparse import enum import functools import logging +from collections.abc import Sequence from typing import Any from typing import Callable -from typing import Sequence from flake8 import utils from flake8.plugins.finder import Plugins diff --git a/src/flake8/options/parse_args.py b/src/flake8/options/parse_args.py index e3f8795..ff5e08f 100644 --- a/src/flake8/options/parse_args.py +++ b/src/flake8/options/parse_args.py @@ -2,7 +2,7 @@ from __future__ import annotations import argparse -from typing import Sequence +from collections.abc import Sequence import flake8 from flake8.main import options diff --git a/src/flake8/plugins/finder.py b/src/flake8/plugins/finder.py index 380ec3a..88b66a0 100644 --- a/src/flake8/plugins/finder.py +++ b/src/flake8/plugins/finder.py @@ -7,9 +7,9 @@ import inspect import itertools import logging import sys +from collections.abc import Generator +from collections.abc import Iterable from typing import Any -from typing import Generator -from typing import Iterable from typing import NamedTuple from flake8 import utils @@ -68,7 +68,7 @@ class Plugins(NamedTuple): reporters: dict[str, LoadedPlugin] disabled: list[LoadedPlugin] - def all_plugins(self) -> Generator[LoadedPlugin, None, None]: + def all_plugins(self) -> Generator[LoadedPlugin]: """Return an iterator over all :class:`LoadedPlugin`s.""" yield from self.checkers.tree yield from self.checkers.logical_line @@ -151,7 +151,7 @@ def _flake8_plugins( eps: Iterable[importlib.metadata.EntryPoint], name: str, version: str, -) -> Generator[Plugin, None, None]: +) -> Generator[Plugin]: pyflakes_meta = importlib.metadata.distribution("pyflakes").metadata pycodestyle_meta = importlib.metadata.distribution("pycodestyle").metadata @@ -173,7 +173,7 @@ def _flake8_plugins( yield Plugin(name, version, ep) -def _find_importlib_plugins() -> Generator[Plugin, None, None]: +def _find_importlib_plugins() -> Generator[Plugin]: # some misconfigured pythons (RHEL) have things on `sys.path` twice seen = set() for dist in importlib.metadata.distributions(): @@ -212,7 +212,7 @@ def _find_importlib_plugins() -> Generator[Plugin, None, None]: def _find_local_plugins( cfg: configparser.RawConfigParser, -) -> Generator[Plugin, None, None]: +) -> Generator[Plugin]: for plugin_type in ("extension", "report"): group = f"flake8.{plugin_type}" for plugin_s in utils.parse_comma_separated_list( diff --git a/src/flake8/plugins/pycodestyle.py b/src/flake8/plugins/pycodestyle.py index 9e1d2bb..cd760dc 100644 --- a/src/flake8/plugins/pycodestyle.py +++ b/src/flake8/plugins/pycodestyle.py @@ -2,8 +2,8 @@ # fmt: off from __future__ import annotations +from collections.abc import Generator from typing import Any -from typing import Generator from pycodestyle import ambiguous_identifier as _ambiguous_identifier from pycodestyle import bare_except as _bare_except @@ -55,7 +55,7 @@ def pycodestyle_logical( previous_unindented_logical_line: Any, tokens: Any, verbose: Any, -) -> Generator[tuple[int, str], None, None]: +) -> Generator[tuple[int, str]]: """Run pycodestyle logical checks.""" yield from _ambiguous_identifier(logical_line, tokens) yield from _bare_except(logical_line, noqa) @@ -93,7 +93,7 @@ def pycodestyle_physical( noqa: Any, physical_line: Any, total_lines: Any, -) -> Generator[tuple[int, str], None, None]: +) -> Generator[tuple[int, str]]: """Run pycodestyle physical checks.""" ret = _maximum_line_length(physical_line, max_line_length, multiline, line_number, noqa) # noqa: E501 if ret is not None: diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index 6c57619..2835e03 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -4,8 +4,8 @@ from __future__ import annotations import argparse import ast import logging +from collections.abc import Generator from typing import Any -from typing import Generator import pyflakes.checker @@ -97,7 +97,7 @@ class FlakesChecker(pyflakes.checker.Checker): cls.builtIns = cls.builtIns.union(options.builtins) cls.with_doctest = options.doctests - def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]: + def run(self) -> Generator[tuple[int, int, str, type[Any]]]: """Run the plugin.""" for message in self.messages: col = getattr(message, "col", 0) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index e44547b..610964d 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -6,10 +6,8 @@ import ast import functools import logging import tokenize +from collections.abc import Generator from typing import Any -from typing import Generator -from typing import List -from typing import Tuple from flake8 import defaults from flake8 import utils @@ -24,8 +22,8 @@ SKIP_TOKENS = frozenset( [tokenize.NL, tokenize.NEWLINE, tokenize.INDENT, tokenize.DEDENT] ) -_LogicalMapping = List[Tuple[int, Tuple[int, int]]] -_Logical = Tuple[List[str], List[str], _LogicalMapping] +_LogicalMapping = list[tuple[int, tuple[int, int]]] +_Logical = tuple[list[str], list[str], _LogicalMapping] class FileProcessor: @@ -127,9 +125,7 @@ class FileProcessor: """Signal the beginning of an fstring.""" self._fstring_start = lineno - def multiline_string( - self, token: tokenize.TokenInfo - ) -> Generator[str, None, None]: + def multiline_string(self, token: tokenize.TokenInfo) -> Generator[str]: """Iterate through the lines of a multiline string.""" if token.type == FSTRING_END: # pragma: >=3.12 cover start = self._fstring_start @@ -210,7 +206,7 @@ class FileProcessor: brace_offset = text.count("{") + text.count("}") text = "x" * (len(text) + brace_offset) end = (end[0], end[1] + brace_offset) - if previous_row: + if previous_row is not None and previous_column is not None: (start_row, start_column) = start if previous_row != start_row: row_index = previous_row - 1 @@ -263,7 +259,7 @@ class FileProcessor: ) return ret - def generate_tokens(self) -> Generator[tokenize.TokenInfo, None, None]: + def generate_tokens(self) -> Generator[tokenize.TokenInfo]: """Tokenize the file and yield the tokens.""" for token in tokenize.generate_tokens(self.next_line): if token[2][0] > self.total_lines: diff --git a/src/flake8/statistics.py b/src/flake8/statistics.py index a33e6a6..5a22254 100644 --- a/src/flake8/statistics.py +++ b/src/flake8/statistics.py @@ -1,7 +1,7 @@ """Statistic collection logic for Flake8.""" from __future__ import annotations -from typing import Generator +from collections.abc import Generator from typing import NamedTuple from flake8.violation import Violation @@ -36,7 +36,7 @@ class Statistics: def statistics_for( self, prefix: str, filename: str | None = None - ) -> Generator[Statistic, None, None]: + ) -> Generator[Statistic]: """Generate statistics for the prefix and filename. If you have a :class:`Statistics` object that has recorded errors, diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index a409484..f72e6d8 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -7,8 +7,8 @@ import copy import enum import functools import logging -from typing import Generator -from typing import Sequence +from collections.abc import Generator +from collections.abc import Sequence from flake8 import defaults from flake8 import statistics @@ -225,13 +225,11 @@ class StyleGuideManager: *self.populate_style_guides_with(options), ] - self.style_guide_for = functools.lru_cache(maxsize=None)( - self._style_guide_for - ) + self.style_guide_for = functools.cache(self._style_guide_for) def populate_style_guides_with( self, options: argparse.Namespace - ) -> Generator[StyleGuide, None, None]: + ) -> Generator[StyleGuide]: """Generate style guides from the per-file-ignores option. :param options: @@ -253,9 +251,7 @@ class StyleGuideManager: ) @contextlib.contextmanager - def processing_file( - self, filename: str - ) -> Generator[StyleGuide, None, None]: + def processing_file(self, filename: str) -> Generator[StyleGuide]: """Record the fact that we're processing the file's results.""" guide = self.style_guide_for(filename) with guide.processing_file(filename): @@ -338,9 +334,7 @@ class StyleGuide: ) @contextlib.contextmanager - def processing_file( - self, filename: str - ) -> Generator[StyleGuide, None, None]: + def processing_file(self, filename: str) -> Generator[StyleGuide]: """Record the fact that we're processing the file's results.""" self.formatter.beginning(filename) yield self diff --git a/src/flake8/utils.py b/src/flake8/utils.py index afc3896..67db33f 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -11,9 +11,9 @@ import re import sys import textwrap import tokenize +from collections.abc import Sequence +from re import Pattern from typing import NamedTuple -from typing import Pattern -from typing import Sequence from flake8 import exceptions diff --git a/src/flake8/violation.py b/src/flake8/violation.py index 96161d4..ae1631a 100644 --- a/src/flake8/violation.py +++ b/src/flake8/violation.py @@ -4,7 +4,7 @@ from __future__ import annotations import functools import linecache import logging -from typing import Match +from re import Match from typing import NamedTuple from flake8 import defaults From d56d569ce40a623a17c212ea7f2b306714f27f31 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Mar 2025 15:53:41 -0400 Subject: [PATCH 112/131] update versions of pycodestyle / pyflakes --- setup.cfg | 4 ++-- src/flake8/plugins/pyflakes.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index 688e349..6f63f5a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -29,8 +29,8 @@ classifiers = packages = find: install_requires = mccabe>=0.7.0,<0.8.0 - pycodestyle>=2.12.0,<2.13.0 - pyflakes>=3.2.0,<3.3.0 + pycodestyle>=2.13.0,<2.14.0 + pyflakes>=3.3.0,<3.4.0 python_requires = >=3.9 package_dir = =src diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index 2835e03..3620a27 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -55,6 +55,7 @@ FLAKE8_PYFLAKES_CODES = { "UndefinedName": "F821", "UndefinedExport": "F822", "UndefinedLocal": "F823", + "UnusedIndirectAssignment": "F824", "DuplicateArgument": "F831", "UnusedVariable": "F841", "UnusedAnnotation": "F842", From 16f5f28a384f0781bebb37a08aa45e65b9526c50 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 29 Mar 2025 16:17:35 -0400 Subject: [PATCH 113/131] Release 7.2.0 --- docs/source/release-notes/7.2.0.rst | 19 +++++++++++++++++++ docs/source/release-notes/index.rst | 1 + src/flake8/__init__.py | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 docs/source/release-notes/7.2.0.rst diff --git a/docs/source/release-notes/7.2.0.rst b/docs/source/release-notes/7.2.0.rst new file mode 100644 index 0000000..fe124d7 --- /dev/null +++ b/docs/source/release-notes/7.2.0.rst @@ -0,0 +1,19 @@ +7.2.0 -- 2025-03-29 +------------------- + +You can view the `7.2.0 milestone`_ on GitHub for more details. + +New Dependency Information +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- pycodestyle has been updated to >= 2.13.0, < 2.14.0 (See also :pull:`1974`). +- pyflakes has been updated to >= 3.3.0, < 3.4.0 (See also :pull:`1974`). + +Features +~~~~~~~~ + +- Require python >= 3.9 (See also :pull:`1973`). + +.. all links +.. _7.2.0 milestone: + https://github.com/PyCQA/flake8/milestone/53 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index 2145179..a4d8bfc 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -13,6 +13,7 @@ with the newest releases first. 7.1.0 7.1.1 7.1.2 + 7.2.0 6.x Release Series ================== diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index 0345499..cf91f8b 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ import sys LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "7.1.2" +__version__ = "7.2.0" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From 3613896bd9051147ffa7fd04ac1a98cbc9e35cf2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 31 Mar 2025 10:05:31 -0400 Subject: [PATCH 114/131] document F824 --- docs/source/user/error-codes.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/source/user/error-codes.rst b/docs/source/user/error-codes.rst index 2a91413..3090d47 100644 --- a/docs/source/user/error-codes.rst +++ b/docs/source/user/error-codes.rst @@ -102,6 +102,9 @@ generates its own :term:`error code`\ s for ``pyflakes``: +------+---------------------------------------------------------------------+ | F823 | local variable ``name`` ... referenced before assignment | +------+---------------------------------------------------------------------+ +| F824 | ``global name`` / ``nonlocal name`` is unused: name is never | +| | assigned in scope | ++------+---------------------------------------------------------------------+ | F831 | duplicate argument ``name`` in function definition | +------+---------------------------------------------------------------------+ | F841 | local variable ``name`` is assigned to but never used | From 8dfa6695b4fb1e1401b357367a0a71037d29f6aa Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 11 Apr 2025 17:39:39 -0400 Subject: [PATCH 115/131] add rtd sphinx config --- .readthedocs.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 0425dc2..dfa8b9d 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -8,3 +8,5 @@ python: install: - path: . - requirements: docs/source/requirements.txt +sphinx: + configuration: docs/source/conf.py From 019424b80d3d7d5d8a2a1638f5877080546e3f46 Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Fri, 23 May 2025 16:25:06 -0400 Subject: [PATCH 116/131] add support for t-strings --- src/flake8/_compat.py | 7 +++++++ src/flake8/checker.py | 3 +++ src/flake8/processor.py | 17 +++++++++++++--- tests/integration/test_plugins.py | 33 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/flake8/_compat.py b/src/flake8/_compat.py index e8a3ccd..22bb84e 100644 --- a/src/flake8/_compat.py +++ b/src/flake8/_compat.py @@ -9,3 +9,10 @@ if sys.version_info >= (3, 12): # pragma: >=3.12 cover FSTRING_END = tokenize.FSTRING_END else: # pragma: <3.12 cover FSTRING_START = FSTRING_MIDDLE = FSTRING_END = -1 + +if sys.version_info >= (3, 14): # pragma: >=3.14 cover + TSTRING_START = tokenize.TSTRING_START + TSTRING_MIDDLE = tokenize.TSTRING_MIDDLE + TSTRING_END = tokenize.TSTRING_END +else: # pragma: <3.14 cover + TSTRING_START = TSTRING_MIDDLE = TSTRING_END = -1 diff --git a/src/flake8/checker.py b/src/flake8/checker.py index d1659b7..84d45aa 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -19,6 +19,7 @@ from flake8 import exceptions from flake8 import processor from flake8 import utils from flake8._compat import FSTRING_START +from flake8._compat import TSTRING_START from flake8.discover_files import expand_paths from flake8.options.parse_args import parse_args from flake8.plugins.finder import Checkers @@ -554,6 +555,8 @@ class FileChecker: assert self.processor is not None if token.type == FSTRING_START: # pragma: >=3.12 cover self.processor.fstring_start(token.start[0]) + elif token.type == TSTRING_START: # pragma: >=3.14 cover + self.processor.tstring_start(token.start[0]) # a newline token ends a single physical line. elif processor.is_eol_token(token): # if the file does not end with a newline, the NEWLINE diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 610964d..ccb4c57 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -13,6 +13,8 @@ from flake8 import defaults from flake8 import utils from flake8._compat import FSTRING_END from flake8._compat import FSTRING_MIDDLE +from flake8._compat import TSTRING_END +from flake8._compat import TSTRING_MIDDLE from flake8.plugins.finder import LoadedPlugin LOG = logging.getLogger(__name__) @@ -113,7 +115,7 @@ class FileProcessor: self.verbose = options.verbose #: Statistics dictionary self.statistics = {"logical lines": 0} - self._fstring_start = -1 + self._fstring_start = self._tstring_start = -1 @functools.cached_property def file_tokens(self) -> list[tokenize.TokenInfo]: @@ -125,10 +127,16 @@ class FileProcessor: """Signal the beginning of an fstring.""" self._fstring_start = lineno + def tstring_start(self, lineno: int) -> None: # pragma: >=3.14 cover + """Signal the beginning of an tstring.""" + self._tstring_start = lineno + def multiline_string(self, token: tokenize.TokenInfo) -> Generator[str]: """Iterate through the lines of a multiline string.""" if token.type == FSTRING_END: # pragma: >=3.12 cover start = self._fstring_start + elif token.type == TSTRING_END: # pragma: >=3.14 cover + start = self._tstring_start else: start = token.start[0] @@ -198,7 +206,10 @@ class FileProcessor: continue if token_type == tokenize.STRING: text = mutate_string(text) - elif token_type == FSTRING_MIDDLE: # pragma: >=3.12 cover + elif token_type in { + FSTRING_MIDDLE, + TSTRING_MIDDLE, + }: # pragma: >=3.12 cover # noqa: E501 # A curly brace in an FSTRING_MIDDLE token must be an escaped # curly brace. Both 'text' and 'end' will account for the # escaped version of the token (i.e. a single brace) rather @@ -382,7 +393,7 @@ def is_eol_token(token: tokenize.TokenInfo) -> bool: def is_multiline_string(token: tokenize.TokenInfo) -> bool: """Check if this is a multiline string.""" - return token.type == FSTRING_END or ( + return token.type in {FSTRING_END, TSTRING_END} or ( token.type == tokenize.STRING and "\n" in token.string ) diff --git a/tests/integration/test_plugins.py b/tests/integration/test_plugins.py index 90ca555..471cab8 100644 --- a/tests/integration/test_plugins.py +++ b/tests/integration/test_plugins.py @@ -296,3 +296,36 @@ t.py:1:1: T001 "f'xxxxxxxxxxxxxxxxxxxxxxxx'" """ out, err = capsys.readouterr() assert out == expected + + +@pytest.mark.xfail(sys.version_info < (3, 14), reason="3.14+") +def test_tstring_logical_line(tmpdir, capsys): # pragma: >=3.14 cover + cfg_s = f"""\ +[flake8] +extend-ignore = F +[flake8:local-plugins] +extension = + T = {yields_logical_line.__module__}:{yields_logical_line.__name__} +""" + + cfg = tmpdir.join("tox.ini") + cfg.write(cfg_s) + + src = """\ +t''' +hello {world} +''' +t'{{"{hello}": "{world}"}}' +""" + t_py = tmpdir.join("t.py") + t_py.write_binary(src.encode()) + + with tmpdir.as_cwd(): + assert main(("t.py", "--config", str(cfg))) == 1 + + expected = """\ +t.py:1:1: T001 "t'''xxxxxxx{world}x'''" +t.py:4:1: T001 "t'xxx{hello}xxxx{world}xxx'" +""" + out, err = capsys.readouterr() + assert out == expected From 4941a3e32e54488698ecbc23993bfeb2a60c0fc5 Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Fri, 20 Jun 2025 15:15:53 -0400 Subject: [PATCH 117/131] upgrade pyflakes / pycodestyle --- setup.cfg | 4 ++-- src/flake8/plugins/pyflakes.py | 1 + tests/unit/test_checker_manager.py | 8 +++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/setup.cfg b/setup.cfg index 6f63f5a..a6b5a5e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -29,8 +29,8 @@ classifiers = packages = find: install_requires = mccabe>=0.7.0,<0.8.0 - pycodestyle>=2.13.0,<2.14.0 - pyflakes>=3.3.0,<3.4.0 + pycodestyle>=2.14.0,<2.15.0 + pyflakes>=3.4.0,<3.5.0 python_requires = >=3.9 package_dir = =src diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index 3620a27..66d8c1c 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -36,6 +36,7 @@ FLAKE8_PYFLAKES_CODES = { "StringDotFormatMissingArgument": "F524", "StringDotFormatMixingAutomatic": "F525", "FStringMissingPlaceholders": "F541", + "TStringMissingPlaceholders": "F542", "MultiValueRepeatedKeyLiteral": "F601", "MultiValueRepeatedKeyVariable": "F602", "TooManyExpressionsInStarredAssignment": "F621", diff --git a/tests/unit/test_checker_manager.py b/tests/unit/test_checker_manager.py index 593822b..eecba3b 100644 --- a/tests/unit/test_checker_manager.py +++ b/tests/unit/test_checker_manager.py @@ -41,9 +41,11 @@ def test_oserrors_are_reraised(): err = OSError(errno.EAGAIN, "Ominous message") with mock.patch("_multiprocessing.SemLock", side_effect=err): manager = _parallel_checker_manager() - with mock.patch.object(manager, "run_serial") as serial: - with pytest.raises(OSError): - manager.run() + with ( + mock.patch.object(manager, "run_serial") as serial, + pytest.raises(OSError), + ): + manager.run() assert serial.call_count == 0 From 6bcdb628597fa2d03494965089ff87a492ffc1e9 Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Fri, 20 Jun 2025 15:21:27 -0400 Subject: [PATCH 118/131] document F542 --- docs/source/user/error-codes.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/user/error-codes.rst b/docs/source/user/error-codes.rst index 3090d47..c8b46c1 100644 --- a/docs/source/user/error-codes.rst +++ b/docs/source/user/error-codes.rst @@ -59,6 +59,8 @@ generates its own :term:`error code`\ s for ``pyflakes``: +------+---------------------------------------------------------------------+ | F541 | f-string without any placeholders | +------+---------------------------------------------------------------------+ +| F542 | t-string without any placeholders | ++------+---------------------------------------------------------------------+ +------+---------------------------------------------------------------------+ | F601 | dictionary key ``name`` repeated with different values | +------+---------------------------------------------------------------------+ From c48217e1fc006c2dddd14df54e83b67da15de5cd Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Fri, 20 Jun 2025 15:30:19 -0400 Subject: [PATCH 119/131] Release 7.3.0 --- docs/source/release-notes/7.3.0.rst | 15 +++++++++++++++ docs/source/release-notes/index.rst | 11 ++++++----- src/flake8/__init__.py | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 docs/source/release-notes/7.3.0.rst diff --git a/docs/source/release-notes/7.3.0.rst b/docs/source/release-notes/7.3.0.rst new file mode 100644 index 0000000..dedc918 --- /dev/null +++ b/docs/source/release-notes/7.3.0.rst @@ -0,0 +1,15 @@ +7.3.0 -- 2025-06-20 +------------------- + +You can view the `7.3.0 milestone`_ on GitHub for more details. + +New Dependency Information +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- Added support for python 3.14 (See also :pull:`1983`). +- pycodestyle has been updated to >= 2.14.0, < 2.15.0 (See also :pull:`1985`). +- Pyflakes has been updated to >= 3.4.0, < 3.5.0 (See also :pull:`1985`). + +.. all links +.. _7.3.0 milestone: + https://github.com/PyCQA/flake8/milestone/54 diff --git a/docs/source/release-notes/index.rst b/docs/source/release-notes/index.rst index a4d8bfc..10697df 100644 --- a/docs/source/release-notes/index.rst +++ b/docs/source/release-notes/index.rst @@ -9,18 +9,19 @@ with the newest releases first. ================== .. toctree:: - 7.0.0 - 7.1.0 - 7.1.1 - 7.1.2 + 7.3.0 7.2.0 + 7.1.2 + 7.1.1 + 7.1.0 + 7.0.0 6.x Release Series ================== .. toctree:: - 6.0.0 6.1.0 + 6.0.0 5.x Release Series ================== diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index cf91f8b..db29166 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -17,7 +17,7 @@ import sys LOG = logging.getLogger(__name__) LOG.addHandler(logging.NullHandler()) -__version__ = "7.2.0" +__version__ = "7.3.0" __version_info__ = tuple(int(i) for i in __version__.split(".") if i.isdigit()) _VERBOSITY_TO_LOG_LEVEL = { From 628aece714c9265e8def265f5fcc574605aca524 Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Fri, 20 Jun 2025 15:40:43 -0400 Subject: [PATCH 120/131] adjust global variable definition for new pyflakes the original code was only passing pyflakes by accident due to __future__.annotations --- src/flake8/checker.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/flake8/checker.py b/src/flake8/checker.py index 84d45aa..c52955d 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -45,8 +45,7 @@ SERIAL_RETRY_ERRNOS = { # noise in diffs. } -_mp_plugins: Checkers -_mp_options: argparse.Namespace +_mp: tuple[Checkers, argparse.Namespace] | None = None @contextlib.contextmanager @@ -54,31 +53,31 @@ def _mp_prefork( plugins: Checkers, options: argparse.Namespace ) -> Generator[None]: # we can save significant startup work w/ `fork` multiprocessing - global _mp_plugins, _mp_options - _mp_plugins, _mp_options = plugins, options + global _mp + _mp = plugins, options try: yield finally: - del _mp_plugins, _mp_options + _mp = None def _mp_init(argv: Sequence[str]) -> None: - global _mp_plugins, _mp_options + global _mp # Ensure correct signaling of ^C using multiprocessing.Pool. signal.signal(signal.SIGINT, signal.SIG_IGN) - try: - # for `fork` this'll already be set - _mp_plugins, _mp_options # noqa: B018 - except NameError: + # for `fork` this'll already be set + if _mp is None: plugins, options = parse_args(argv) - _mp_plugins, _mp_options = plugins.checkers, options + _mp = plugins.checkers, options def _mp_run(filename: str) -> tuple[str, Results, dict[str, int]]: + assert _mp is not None, _mp + plugins, options = _mp return FileChecker( - filename=filename, plugins=_mp_plugins, options=_mp_options + filename=filename, plugins=plugins, options=options ).run_checks() From 5fab0d18870b861ffbcb2757adb44154aff126c2 Mon Sep 17 00:00:00 2001 From: Max R Date: Sun, 20 Jul 2025 19:12:27 -0400 Subject: [PATCH 121/131] Update hooks and use `autopep8` + `add-trailing-comma` instead of `black` --- .pre-commit-config.yaml | 23 ++++++++++++--------- setup.cfg | 1 - src/flake8/__init__.py | 2 +- src/flake8/api/legacy.py | 4 ++-- src/flake8/checker.py | 16 +++++++-------- src/flake8/formatting/base.py | 2 +- src/flake8/main/application.py | 2 +- src/flake8/main/debug.py | 2 +- src/flake8/main/options.py | 2 +- src/flake8/options/config.py | 6 +++--- src/flake8/options/manager.py | 2 +- src/flake8/plugins/finder.py | 10 ++++----- src/flake8/plugins/pyflakes.py | 2 +- src/flake8/processor.py | 10 ++++----- src/flake8/statistics.py | 4 ++-- src/flake8/style_guide.py | 10 ++++----- src/flake8/utils.py | 6 +++--- src/flake8/violation.py | 2 +- tests/integration/test_checker.py | 6 +++--- tests/integration/test_main.py | 6 +++--- tests/integration/test_plugins.py | 2 +- tests/unit/plugins/finder_test.py | 32 +++++++++++++++++------------ tests/unit/plugins/reporter_test.py | 4 ++-- tests/unit/test_application.py | 2 +- tests/unit/test_base_formatter.py | 6 +++--- tests/unit/test_debug.py | 2 +- tests/unit/test_decision_engine.py | 10 ++++----- tests/unit/test_discover_files.py | 6 +++--- tests/unit/test_file_processor.py | 12 +++++------ tests/unit/test_main_options.py | 2 +- tests/unit/test_option_manager.py | 8 ++++---- tests/unit/test_options_config.py | 4 ++-- tests/unit/test_style_guide.py | 4 ++-- 33 files changed, 110 insertions(+), 102 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9df4a79..837e6e5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,10 @@ repos: +- repo: https://github.com/asottile/add-trailing-comma + rev: v3.2.0 + hooks: + - id: add-trailing-comma - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v5.0.0 hooks: - id: check-yaml - id: debug-statements @@ -8,11 +12,11 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/setup-cfg-fmt - rev: v2.5.0 + rev: v2.8.0 hooks: - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder-python-imports - rev: v3.14.0 + rev: v3.15.0 hooks: - id: reorder-python-imports args: [ @@ -21,21 +25,20 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.19.1 + rev: v3.20.0 hooks: - id: pyupgrade args: [--py39-plus] -- repo: https://github.com/psf/black - rev: 23.12.1 +- repo: https://github.com/hhatto/autopep8 + rev: v2.3.2 hooks: - - id: black - args: [--line-length=79] + - id: autopep8 - repo: https://github.com/PyCQA/flake8 - rev: 7.0.0 + rev: 7.3.0 hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.15.0 + rev: v1.17.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) diff --git a/setup.cfg b/setup.cfg index a6b5a5e..dc967d3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,7 +16,6 @@ classifiers = Environment :: Console Framework :: Flake8 Intended Audience :: Developers - License :: OSI Approved :: MIT License Programming Language :: Python Programming Language :: Python :: 3 Programming Language :: Python :: 3 :: Only diff --git a/src/flake8/__init__.py b/src/flake8/__init__.py index db29166..0dea638 100644 --- a/src/flake8/__init__.py +++ b/src/flake8/__init__.py @@ -66,5 +66,5 @@ def configure_logging( LOG.addHandler(handler) LOG.setLevel(log_level) LOG.debug( - "Added a %s logging handler to logger root at %s", filename, __name__ + "Added a %s logging handler to logger root at %s", filename, __name__, ) diff --git a/src/flake8/api/legacy.py b/src/flake8/api/legacy.py index 446df29..4d5c91d 100644 --- a/src/flake8/api/legacy.py +++ b/src/flake8/api/legacy.py @@ -135,7 +135,7 @@ class StyleGuide: stdin_display_name=self.options.stdin_display_name, filename_patterns=self.options.filename, exclude=self.options.exclude, - ) + ), ) return not paths @@ -153,7 +153,7 @@ class StyleGuide: if not issubclass(reporter, formatter.BaseFormatter): raise ValueError( "Report should be subclass of " - "flake8.formatter.BaseFormatter." + "flake8.formatter.BaseFormatter.", ) self._application.formatter = reporter(self.options) self._application.guide = None diff --git a/src/flake8/checker.py b/src/flake8/checker.py index c52955d..d957915 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -50,7 +50,7 @@ _mp: tuple[Checkers, argparse.Namespace] | None = None @contextlib.contextmanager def _mp_prefork( - plugins: Checkers, options: argparse.Namespace + plugins: Checkers, options: argparse.Namespace, ) -> Generator[None]: # we can save significant startup work w/ `fork` multiprocessing global _mp @@ -77,7 +77,7 @@ def _mp_run(filename: str) -> tuple[str, Results, dict[str, int]]: assert _mp is not None, _mp plugins, options = _mp return FileChecker( - filename=filename, plugins=plugins, options=options + filename=filename, plugins=plugins, options=options, ).run_checks() @@ -137,7 +137,7 @@ class Manager: if utils.is_using_stdin(self.options.filenames): LOG.warning( "The --jobs option is not compatible with supplying " - "input using - . Ignoring --jobs arguments." + "input using - . Ignoring --jobs arguments.", ) return 0 @@ -252,7 +252,7 @@ class Manager: stdin_display_name=self.options.stdin_display_name, filename_patterns=self.options.filename, exclude=self.exclude, - ) + ), ) self.jobs = min(len(self.filenames), self.jobs) @@ -332,11 +332,11 @@ class FileChecker: assert self.processor is not None, self.filename try: params = self.processor.keyword_arguments_for( - plugin.parameters, arguments + plugin.parameters, arguments, ) except AttributeError as ae: raise exceptions.PluginRequestedUnknownParameters( - plugin_name=plugin.display_name, exception=ae + plugin_name=plugin.display_name, exception=ae, ) try: return plugin.obj(**arguments, **params) @@ -548,7 +548,7 @@ class FileChecker: self.run_logical_checks() def check_physical_eol( - self, token: tokenize.TokenInfo, prev_physical: str + self, token: tokenize.TokenInfo, prev_physical: str, ) -> None: """Run physical checks if and only if it is at the end of the line.""" assert self.processor is not None @@ -598,7 +598,7 @@ def _try_initialize_processpool( def find_offset( - offset: int, mapping: processor._LogicalMapping + offset: int, mapping: processor._LogicalMapping, ) -> tuple[int, int]: """Find the offset tuple for a single offset.""" if isinstance(offset, tuple): diff --git a/src/flake8/formatting/base.py b/src/flake8/formatting/base.py index d986d65..bbbfdff 100644 --- a/src/flake8/formatting/base.py +++ b/src/flake8/formatting/base.py @@ -110,7 +110,7 @@ class BaseFormatter: The formatted error string. """ raise NotImplementedError( - "Subclass of BaseFormatter did not implement" " format." + "Subclass of BaseFormatter did not implement" " format.", ) def show_statistics(self, statistics: Statistics) -> None: diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index 4704cbd..165a6ef 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -76,7 +76,7 @@ class Application: assert self.formatter is not None assert self.options is not None self.guide = style_guide.StyleGuideManager( - self.options, self.formatter + self.options, self.formatter, ) def make_file_checker_manager(self, argv: Sequence[str]) -> None: diff --git a/src/flake8/main/debug.py b/src/flake8/main/debug.py index c3a8b0b..73ca74b 100644 --- a/src/flake8/main/debug.py +++ b/src/flake8/main/debug.py @@ -14,7 +14,7 @@ def information(version: str, plugins: Plugins) -> dict[str, Any]: (loaded.plugin.package, loaded.plugin.version) for loaded in plugins.all_plugins() if loaded.plugin.package not in {"flake8", "local"} - } + }, ) return { "version": version, diff --git a/src/flake8/main/options.py b/src/flake8/main/options.py index 9d57321..e8cbe09 100644 --- a/src/flake8/main/options.py +++ b/src/flake8/main/options.py @@ -32,7 +32,7 @@ def stage1_arg_parser() -> argparse.ArgumentParser: ) parser.add_argument( - "--output-file", default=None, help="Redirect report to a file." + "--output-file", default=None, help="Redirect report to a file.", ) # Config file options diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index b51949c..fddee55 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -78,7 +78,7 @@ def load_config( if config is not None: if not cfg.read(config, encoding="UTF-8"): raise exceptions.ExecutionError( - f"The specified config file does not exist: {config}" + f"The specified config file does not exist: {config}", ) cfg_dir = os.path.dirname(config) else: @@ -89,7 +89,7 @@ def load_config( for filename in extra: if not cfg.read(filename, encoding="UTF-8"): raise exceptions.ExecutionError( - f"The specified config file does not exist: {filename}" + f"The specified config file does not exist: {filename}", ) return cfg, cfg_dir @@ -131,7 +131,7 @@ def parse_config( raise ValueError( f"Error code {error_code!r} " f"supplied to {option_name!r} option " - f"does not match {VALID_CODE_PREFIX.pattern!r}" + f"does not match {VALID_CODE_PREFIX.pattern!r}", ) assert option.config_name is not None diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index cb195fe..addd3ec 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -165,7 +165,7 @@ class Option: if long_option_name is _ARG.NO: raise ValueError( "When specifying parse_from_config=True, " - "a long_option_name must also be specified." + "a long_option_name must also be specified.", ) self.config_name = long_option_name[2:].replace("-", "_") diff --git a/src/flake8/plugins/finder.py b/src/flake8/plugins/finder.py index 88b66a0..4da3402 100644 --- a/src/flake8/plugins/finder.py +++ b/src/flake8/plugins/finder.py @@ -83,8 +83,8 @@ class Plugins(NamedTuple): f"{loaded.plugin.package}: {loaded.plugin.version}" for loaded in self.all_plugins() if loaded.plugin.package not in {"flake8", "local"} - } - ) + }, + ), ) @@ -167,7 +167,7 @@ def _flake8_plugins( # ideally pycodestyle's plugin entrypoints would exactly represent # the codes they produce... yield Plugin( - pycodestyle_meta["name"], pycodestyle_meta["version"], ep + pycodestyle_meta["name"], pycodestyle_meta["version"], ep, ) else: yield Plugin(name, version, ep) @@ -240,7 +240,7 @@ def _check_required_plugins( f"required plugins were not installed!\n" f"- installed: {', '.join(sorted(plugin_names))}\n" f"- expected: {', '.join(sorted(expected_names))}\n" - f"- missing: {', '.join(sorted(missing_plugins))}" + f"- missing: {', '.join(sorted(missing_plugins))}", ) @@ -338,7 +338,7 @@ def _classify_plugins( if not VALID_CODE_PREFIX.match(loaded.entry_name): raise ExecutionError( f"plugin code for `{loaded.display_name}` does not match " - f"{VALID_CODE_PREFIX.pattern}" + f"{VALID_CODE_PREFIX.pattern}", ) return Plugins( diff --git a/src/flake8/plugins/pyflakes.py b/src/flake8/plugins/pyflakes.py index 66d8c1c..9844025 100644 --- a/src/flake8/plugins/pyflakes.py +++ b/src/flake8/plugins/pyflakes.py @@ -72,7 +72,7 @@ class FlakesChecker(pyflakes.checker.Checker): 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 + tree, filename=filename, withDoctest=self.with_doctest, ) @classmethod diff --git a/src/flake8/processor.py b/src/flake8/processor.py index ccb4c57..b1742ca 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -21,7 +21,7 @@ LOG = logging.getLogger(__name__) NEWLINE = frozenset([tokenize.NL, tokenize.NEWLINE]) SKIP_TOKENS = frozenset( - [tokenize.NL, tokenize.NEWLINE, tokenize.INDENT, tokenize.DEDENT] + [tokenize.NL, tokenize.NEWLINE, tokenize.INDENT, tokenize.DEDENT], ) _LogicalMapping = list[tuple[int, tuple[int, int]]] @@ -173,7 +173,7 @@ class FileProcessor: """Update the checker_state attribute for the plugin.""" if "checker_state" in plugin.parameters: self.checker_state = self._checker_states.setdefault( - plugin.entry_name, {} + plugin.entry_name, {}, ) def next_logical_line(self) -> None: @@ -280,7 +280,7 @@ class FileProcessor: def _noqa_line_range(self, min_line: int, max_line: int) -> dict[int, str]: line_range = range(min_line, max_line + 1) - joined = "".join(self.lines[min_line - 1 : max_line]) + joined = "".join(self.lines[min_line - 1: max_line]) return dict.fromkeys(line_range, joined) @functools.cached_property @@ -367,7 +367,7 @@ class FileProcessor: elif any(defaults.NOQA_FILE.search(line) for line in self.lines): LOG.warning( "Detected `flake8: noqa` on line with code. To ignore an " - "error on a line use `noqa` instead." + "error on a line use `noqa` instead.", ) return False else: @@ -388,7 +388,7 @@ class FileProcessor: def is_eol_token(token: tokenize.TokenInfo) -> bool: """Check if the token is an end-of-line token.""" - return token[0] in NEWLINE or token[4][token[3][1] :].lstrip() == "\\\n" + return token[0] in NEWLINE or token[4][token[3][1]:].lstrip() == "\\\n" def is_multiline_string(token: tokenize.TokenInfo) -> bool: diff --git a/src/flake8/statistics.py b/src/flake8/statistics.py index 5a22254..b30e4c7 100644 --- a/src/flake8/statistics.py +++ b/src/flake8/statistics.py @@ -35,7 +35,7 @@ class Statistics: self._store[key].increment() def statistics_for( - self, prefix: str, filename: str | None = None + self, prefix: str, filename: str | None = None, ) -> Generator[Statistic]: """Generate statistics for the prefix and filename. @@ -108,7 +108,7 @@ class Statistic: """ def __init__( - self, error_code: str, filename: str, message: str, count: int + self, error_code: str, filename: str, message: str, count: int, ) -> None: """Initialize our Statistic.""" self.error_code = error_code diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index f72e6d8..d675df7 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -218,7 +218,7 @@ class StyleGuideManager: self.decider = decider or DecisionEngine(options) self.style_guides: list[StyleGuide] = [] self.default_style_guide = StyleGuide( - options, formatter, self.stats, decider=decider + options, formatter, self.stats, decider=decider, ) self.style_guides = [ self.default_style_guide, @@ -228,7 +228,7 @@ class StyleGuideManager: self.style_guide_for = functools.cache(self._style_guide_for) def populate_style_guides_with( - self, options: argparse.Namespace + self, options: argparse.Namespace, ) -> Generator[StyleGuide]: """Generate style guides from the per-file-ignores option. @@ -240,7 +240,7 @@ class StyleGuideManager: per_file = utils.parse_files_to_codes_mapping(options.per_file_ignores) for filename, violations in per_file: yield self.default_style_guide.copy( - filename=filename, extend_ignore_with=violations + filename=filename, extend_ignore_with=violations, ) def _style_guide_for(self, filename: str) -> StyleGuide: @@ -288,7 +288,7 @@ class StyleGuideManager: """ guide = self.style_guide_for(filename) return guide.handle_error( - code, filename, line_number, column_number, text, physical_line + code, filename, line_number, column_number, text, physical_line, ) @@ -330,7 +330,7 @@ class StyleGuide: options.extend_ignore = options.extend_ignore or [] options.extend_ignore.extend(extend_ignore_with or []) return StyleGuide( - options, self.formatter, self.stats, filename=filename + options, self.formatter, self.stats, filename=filename, ) @contextlib.contextmanager diff --git a/src/flake8/utils.py b/src/flake8/utils.py index 67db33f..e5c086e 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -23,7 +23,7 @@ NORMALIZE_PACKAGE_NAME_RE = re.compile(r"[-_.]+") def parse_comma_separated_list( - value: str, regexp: Pattern[str] = COMMA_SEPARATED_LIST_RE + value: str, regexp: Pattern[str] = COMMA_SEPARATED_LIST_RE, ) -> list[str]: """Parse a comma-separated list. @@ -115,7 +115,7 @@ def parse_files_to_codes_mapping( # noqa: C901 f"Expected `per-file-ignores` to be a mapping from file exclude " f"patterns to ignore codes.\n\n" f"Configured `per-file-ignores` setting:\n\n" - f"{textwrap.indent(value.strip(), ' ')}" + f"{textwrap.indent(value.strip(), ' ')}", ) for token in _tokenize_files_to_codes_mapping(value): @@ -150,7 +150,7 @@ def parse_files_to_codes_mapping( # noqa: C901 def normalize_paths( - paths: Sequence[str], parent: str = os.curdir + paths: Sequence[str], parent: str = os.curdir, ) -> list[str]: """Normalize a list of paths relative to a parent directory. diff --git a/src/flake8/violation.py b/src/flake8/violation.py index ae1631a..8535178 100644 --- a/src/flake8/violation.py +++ b/src/flake8/violation.py @@ -64,6 +64,6 @@ class Violation(NamedTuple): return True LOG.debug( - "%r is not ignored inline with ``# noqa: %s``", self, codes_str + "%r is not ignored inline with ``# noqa: %s``", self, codes_str, ) return False diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py index a585f5a..60d0132 100644 --- a/tests/integration/test_checker.py +++ b/tests/integration/test_checker.py @@ -97,7 +97,7 @@ def mock_file_checker_with_plugin(plugin_target): # Prevent it from reading lines from stdin or somewhere else with mock.patch( - "flake8.processor.FileProcessor.read_lines", return_value=["Line 1"] + "flake8.processor.FileProcessor.read_lines", return_value=["Line 1"], ): file_checker = checker.FileChecker( filename="-", @@ -325,12 +325,12 @@ def test_handling_syntaxerrors_across_pythons(): if sys.version_info < (3, 10): # pragma: no cover (<3.10) # Python 3.9 or older err = SyntaxError( - "invalid syntax", ("", 2, 5, "bad python:\n") + "invalid syntax", ("", 2, 5, "bad python:\n"), ) expected = (2, 4) else: # pragma: no cover (3.10+) err = SyntaxError( - "invalid syntax", ("", 2, 1, "bad python:\n", 2, 11) + "invalid syntax", ("", 2, 1, "bad python:\n", 2, 11), ) expected = (2, 1) file_checker = checker.FileChecker( diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 68b93cb..5091573 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -314,7 +314,7 @@ def test_cli_config_option_respected(tmp_path): """\ [flake8] ignore = F401 -""" +""", ) py_file = tmp_path / "t.py" @@ -330,7 +330,7 @@ def test_cli_isolated_overrides_config_option(tmp_path): """\ [flake8] ignore = F401 -""" +""", ) py_file = tmp_path / "t.py" @@ -364,7 +364,7 @@ def test_output_file(tmpdir, capsys): def test_early_keyboard_interrupt_does_not_crash(capsys): with mock.patch.object( - config, "load_config", side_effect=KeyboardInterrupt + config, "load_config", side_effect=KeyboardInterrupt, ): assert cli.main(["does-not-exist"]) == 1 out, err = capsys.readouterr() diff --git a/tests/integration/test_plugins.py b/tests/integration/test_plugins.py index 471cab8..9f9c1a0 100644 --- a/tests/integration/test_plugins.py +++ b/tests/integration/test_plugins.py @@ -86,7 +86,7 @@ def test_local_plugin_can_add_option(local_config): stage1_args, rest = stage1_parser.parse_known_args(argv) cfg, cfg_dir = config.load_config( - config=stage1_args.config, extra=[], isolated=False + config=stage1_args.config, extra=[], isolated=False, ) opts = finder.parse_plugin_options( diff --git a/tests/unit/plugins/finder_test.py b/tests/unit/plugins/finder_test.py index b289bef..a155ef1 100644 --- a/tests/unit/plugins/finder_test.py +++ b/tests/unit/plugins/finder_test.py @@ -42,7 +42,7 @@ def test_plugins_all_plugins(): logical_line_plugin = _loaded(parameters={"logical_line": True}) physical_line_plugin = _loaded(parameters={"physical_line": True}) report_plugin = _loaded( - plugin=_plugin(ep=_ep(name="R", group="flake8.report")) + plugin=_plugin(ep=_ep(name="R", group="flake8.report")), ) plugins = finder.Plugins( @@ -200,14 +200,16 @@ def test_flake8_plugins(flake8_dist, mock_distribution): "flake8", "9001", importlib.metadata.EntryPoint( - "default", "flake8.formatting.default:Default", "flake8.report" + "default", + "flake8.formatting.default:Default", + "flake8.report", ), ), finder.Plugin( "flake8", "9001", importlib.metadata.EntryPoint( - "pylint", "flake8.formatting.default:Pylint", "flake8.report" + "pylint", "flake8.formatting.default:Pylint", "flake8.report", ), ), } @@ -270,7 +272,7 @@ unrelated = unrelated:main "flake8-foo", "1.2.3", importlib.metadata.EntryPoint( - "Q", "flake8_foo:Plugin", "flake8.extension" + "Q", "flake8_foo:Plugin", "flake8.extension", ), ), finder.Plugin( @@ -304,21 +306,23 @@ unrelated = unrelated:main "flake8", "9001", importlib.metadata.EntryPoint( - "default", "flake8.formatting.default:Default", "flake8.report" + "default", + "flake8.formatting.default:Default", + "flake8.report", ), ), finder.Plugin( "flake8", "9001", importlib.metadata.EntryPoint( - "pylint", "flake8.formatting.default:Pylint", "flake8.report" + "pylint", "flake8.formatting.default:Pylint", "flake8.report", ), ), finder.Plugin( "flake8-foo", "1.2.3", importlib.metadata.EntryPoint( - "foo", "flake8_foo:Formatter", "flake8.report" + "foo", "flake8_foo:Formatter", "flake8.report", ), ), } @@ -485,28 +489,30 @@ def test_find_plugins( "flake8", "9001", importlib.metadata.EntryPoint( - "default", "flake8.formatting.default:Default", "flake8.report" + "default", + "flake8.formatting.default:Default", + "flake8.report", ), ), finder.Plugin( "flake8", "9001", importlib.metadata.EntryPoint( - "pylint", "flake8.formatting.default:Pylint", "flake8.report" + "pylint", "flake8.formatting.default:Pylint", "flake8.report", ), ), finder.Plugin( "flake8-foo", "1.2.3", importlib.metadata.EntryPoint( - "Q", "flake8_foo:Plugin", "flake8.extension" + "Q", "flake8_foo:Plugin", "flake8.extension", ), ), finder.Plugin( "flake8-foo", "1.2.3", importlib.metadata.EntryPoint( - "foo", "flake8_foo:Formatter", "flake8.report" + "foo", "flake8_foo:Formatter", "flake8.report", ), ), finder.Plugin( @@ -518,7 +524,7 @@ def test_find_plugins( "local", "local", importlib.metadata.EntryPoint( - "Y", "mod2:attr", "flake8.extension" + "Y", "mod2:attr", "flake8.extension", ), ), finder.Plugin( @@ -723,7 +729,7 @@ def test_import_plugins_extends_sys_path(): def test_classify_plugins(): report_plugin = _loaded( - plugin=_plugin(ep=_ep(name="R", group="flake8.report")) + plugin=_plugin(ep=_ep(name="R", group="flake8.report")), ) tree_plugin = _loaded(parameters={"tree": True}) logical_line_plugin = _loaded(parameters={"logical_line": True}) diff --git a/tests/unit/plugins/reporter_test.py b/tests/unit/plugins/reporter_test.py index 842465a..48b2873 100644 --- a/tests/unit/plugins/reporter_test.py +++ b/tests/unit/plugins/reporter_test.py @@ -25,7 +25,7 @@ def reporters(): "flake8", "123", importlib.metadata.EntryPoint( - name, f"{cls.__module__}:{cls.__name__}", "flake8.report" + name, f"{cls.__module__}:{cls.__name__}", "flake8.report", ), ), cls, @@ -72,5 +72,5 @@ def test_make_formatter_format_string(reporters, caplog): "flake8.plugins.reporter", 30, "'hi %(code)s' is an unknown formatter. Falling back to default.", - ) + ), ] diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index 04147ec..3c93085 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -36,7 +36,7 @@ def application(): ], ) def test_application_exit_code( - result_count, catastrophic, exit_zero, value, application + result_count, catastrophic, exit_zero, value, application, ): """Verify Application.exit_code returns the correct value.""" application.result_count = result_count diff --git a/tests/unit/test_base_formatter.py b/tests/unit/test_base_formatter.py index 5b57335..0d81c81 100644 --- a/tests/unit/test_base_formatter.py +++ b/tests/unit/test_base_formatter.py @@ -50,7 +50,7 @@ def test_format_needs_to_be_implemented(): formatter = base.BaseFormatter(options()) with pytest.raises(NotImplementedError): formatter.format( - Violation("A000", "file.py", 1, 1, "error text", None) + Violation("A000", "file.py", 1, 1, "error text", None), ) @@ -59,7 +59,7 @@ def test_show_source_returns_nothing_when_not_showing_source(): formatter = base.BaseFormatter(options(show_source=False)) assert ( formatter.show_source( - Violation("A000", "file.py", 1, 1, "error text", "line") + Violation("A000", "file.py", 1, 1, "error text", "line"), ) == "" ) @@ -70,7 +70,7 @@ def test_show_source_returns_nothing_when_there_is_source(): formatter = base.BaseFormatter(options(show_source=True)) assert ( formatter.show_source( - Violation("A000", "file.py", 1, 1, "error text", None) + Violation("A000", "file.py", 1, 1, "error text", None), ) == "" ) diff --git a/tests/unit/test_debug.py b/tests/unit/test_debug.py index 4ba604f..298b598 100644 --- a/tests/unit/test_debug.py +++ b/tests/unit/test_debug.py @@ -14,7 +14,7 @@ def test_debug_information(): pkg, version, importlib.metadata.EntryPoint( - ep_name, "dne:dne", "flake8.extension" + ep_name, "dne:dne", "flake8.extension", ), ), None, diff --git a/tests/unit/test_decision_engine.py b/tests/unit/test_decision_engine.py index d543d5e..cd8f80d 100644 --- a/tests/unit/test_decision_engine.py +++ b/tests/unit/test_decision_engine.py @@ -35,7 +35,7 @@ def create_options(**kwargs): def test_was_ignored_ignores_errors(ignore_list, extend_ignore, error_code): """Verify we detect users explicitly ignoring an error.""" decider = style_guide.DecisionEngine( - create_options(ignore=ignore_list, extend_ignore=extend_ignore) + create_options(ignore=ignore_list, extend_ignore=extend_ignore), ) assert decider.was_ignored(error_code) is style_guide.Ignored.Explicitly @@ -53,11 +53,11 @@ def test_was_ignored_ignores_errors(ignore_list, extend_ignore, error_code): ], ) def test_was_ignored_implicitly_selects_errors( - ignore_list, extend_ignore, error_code + ignore_list, extend_ignore, error_code, ): """Verify we detect users does not explicitly ignore an error.""" decider = style_guide.DecisionEngine( - create_options(ignore=ignore_list, extend_ignore=extend_ignore) + create_options(ignore=ignore_list, extend_ignore=extend_ignore), ) assert decider.was_ignored(error_code) is style_guide.Selected.Implicitly @@ -179,7 +179,7 @@ def test_was_selected_excludes_errors(select_list, error_code): ], ) def test_decision_for( - select_list, ignore_list, extend_ignore, error_code, expected + select_list, ignore_list, extend_ignore, error_code, expected, ): """Verify we decide when to report an error.""" decider = style_guide.DecisionEngine( @@ -187,7 +187,7 @@ def test_decision_for( select=select_list, ignore=ignore_list, extend_ignore=extend_ignore, - ) + ), ) assert decider.decision_for(error_code) is expected diff --git a/tests/unit/test_discover_files.py b/tests/unit/test_discover_files.py index ca945c2..ea55ccc 100644 --- a/tests/unit/test_discover_files.py +++ b/tests/unit/test_discover_files.py @@ -47,7 +47,7 @@ def test_filenames_from_a_directory_with_a_predicate(): _filenames_from( arg=_normpath("a/b/"), predicate=lambda path: path.endswith(_normpath("b/c.py")), - ) + ), ) # should not include c.py expected = _normpaths(("a/b/d.py", "a/b/e/f.py")) @@ -61,7 +61,7 @@ def test_filenames_from_a_directory_with_a_predicate_from_the_current_dir(): _filenames_from( arg=_normpath("./a/b"), predicate=lambda path: path == "c.py", - ) + ), ) # none should have matched the predicate so all returned expected = _normpaths(("./a/b/c.py", "./a/b/d.py", "./a/b/e/f.py")) @@ -132,7 +132,7 @@ def _expand_paths( stdin_display_name=stdin_display_name, filename_patterns=filename_patterns, exclude=exclude, - ) + ), ) diff --git a/tests/unit/test_file_processor.py b/tests/unit/test_file_processor.py index a90c628..22c5bcf 100644 --- a/tests/unit/test_file_processor.py +++ b/tests/unit/test_file_processor.py @@ -28,7 +28,7 @@ def _lines_from_file(tmpdir, contents, options): def test_read_lines_universal_newlines(tmpdir, default_options): r"""Verify that line endings are translated to \n.""" lines = _lines_from_file( - tmpdir, b"# coding: utf-8\r\nx = 1\r\n", default_options + tmpdir, b"# coding: utf-8\r\nx = 1\r\n", default_options, ) assert lines == ["# coding: utf-8\n", "x = 1\n"] @@ -36,7 +36,7 @@ def test_read_lines_universal_newlines(tmpdir, default_options): def test_read_lines_incorrect_utf_16(tmpdir, default_options): """Verify that an incorrectly encoded file is read as latin-1.""" lines = _lines_from_file( - tmpdir, b"# coding: utf16\nx = 1\n", default_options + tmpdir, b"# coding: utf16\nx = 1\n", default_options, ) assert lines == ["# coding: utf16\n", "x = 1\n"] @@ -44,7 +44,7 @@ def test_read_lines_incorrect_utf_16(tmpdir, default_options): def test_read_lines_unknown_encoding(tmpdir, default_options): """Verify that an unknown encoding is still read as latin-1.""" lines = _lines_from_file( - tmpdir, b"# coding: fake-encoding\nx = 1\n", default_options + tmpdir, b"# coding: fake-encoding\nx = 1\n", default_options, ) assert lines == ["# coding: fake-encoding\n", "x = 1\n"] @@ -289,7 +289,7 @@ def test_processor_split_line(default_options): def test_build_ast(default_options): """Verify the logic for how we build an AST for plugins.""" file_processor = processor.FileProcessor( - "-", default_options, lines=["a = 1\n"] + "-", default_options, lines=["a = 1\n"], ) module = file_processor.build_ast() @@ -299,7 +299,7 @@ def test_build_ast(default_options): def test_next_logical_line_updates_the_previous_logical_line(default_options): """Verify that we update our tracking of the previous logical line.""" file_processor = processor.FileProcessor( - "-", default_options, lines=["a = 1\n"] + "-", default_options, lines=["a = 1\n"], ) file_processor.indent_level = 1 @@ -315,7 +315,7 @@ def test_next_logical_line_updates_the_previous_logical_line(default_options): def test_visited_new_blank_line(default_options): """Verify we update the number of blank lines seen.""" file_processor = processor.FileProcessor( - "-", default_options, lines=["a = 1\n"] + "-", default_options, lines=["a = 1\n"], ) assert file_processor.blank_lines == 0 diff --git a/tests/unit/test_main_options.py b/tests/unit/test_main_options.py index 7c1feba..0b1fb69 100644 --- a/tests/unit/test_main_options.py +++ b/tests/unit/test_main_options.py @@ -6,7 +6,7 @@ from flake8.main import options def test_stage1_arg_parser(): stage1_parser = options.stage1_arg_parser() opts, args = stage1_parser.parse_known_args( - ["--foo", "--verbose", "src", "setup.py", "--statistics", "--version"] + ["--foo", "--verbose", "src", "setup.py", "--statistics", "--version"], ) assert opts.verbose diff --git a/tests/unit/test_option_manager.py b/tests/unit/test_option_manager.py index 92266f3..9904a2e 100644 --- a/tests/unit/test_option_manager.py +++ b/tests/unit/test_option_manager.py @@ -122,7 +122,7 @@ def test_parse_args_handles_comma_separated_defaults(optmanager): assert optmanager.config_options_dict == {} optmanager.add_option( - "--exclude", default="E123,W234", comma_separated_list=True + "--exclude", default="E123,W234", comma_separated_list=True, ) options = optmanager.parse_args([]) @@ -135,7 +135,7 @@ def test_parse_args_handles_comma_separated_lists(optmanager): assert optmanager.config_options_dict == {} optmanager.add_option( - "--exclude", default="E123,W234", comma_separated_list=True + "--exclude", default="E123,W234", comma_separated_list=True, ) options = optmanager.parse_args(["--exclude", "E201,W111,F280"]) @@ -148,11 +148,11 @@ def test_parse_args_normalize_paths(optmanager): assert optmanager.config_options_dict == {} optmanager.add_option( - "--extra-config", normalize_paths=True, comma_separated_list=True + "--extra-config", normalize_paths=True, comma_separated_list=True, ) options = optmanager.parse_args( - ["--extra-config", "../config.ini,tox.ini,flake8/some-other.cfg"] + ["--extra-config", "../config.ini,tox.ini,flake8/some-other.cfg"], ) assert options.extra_config == [ os.path.abspath("../config.ini"), diff --git a/tests/unit/test_options_config.py b/tests/unit/test_options_config.py index 7de58f0..d73f471 100644 --- a/tests/unit/test_options_config.py +++ b/tests/unit/test_options_config.py @@ -169,7 +169,7 @@ def test_load_extra_config_utf8(tmpdir): @pytest.fixture def opt_manager(): ret = OptionManager( - version="123", plugin_versions="", parents=[], formatter_names=[] + version="123", plugin_versions="", parents=[], formatter_names=[], ) register_default_options(ret) return ret @@ -213,7 +213,7 @@ def test_parse_config_ignores_unknowns(tmp_path, opt_manager, caplog): "flake8.options.config", 10, 'Option "wat" is not registered. Ignoring.', - ) + ), ] diff --git a/tests/unit/test_style_guide.py b/tests/unit/test_style_guide.py index 94fcb26..c66cfd2 100644 --- a/tests/unit/test_style_guide.py +++ b/tests/unit/test_style_guide.py @@ -36,7 +36,7 @@ def test_handle_error_does_not_raise_type_errors(): ) assert 1 == guide.handle_error( - "T111", "file.py", 1, 1, "error found", "a = 1" + "T111", "file.py", 1, 1, "error found", "a = 1", ) @@ -110,7 +110,7 @@ def test_style_guide_manager_pre_file_ignores_parsing(): ], ) def test_style_guide_manager_pre_file_ignores( - ignores, violation, filename, handle_error_return + ignores, violation, filename, handle_error_return, ): """Verify how the StyleGuideManager creates a default style guide.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) From 0f1af5010897439534e95dd4ec38d2738a136bc5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Aug 2025 21:56:25 +0000 Subject: [PATCH 122/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.17.0 → v1.17.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.17.0...v1.17.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 837e6e5..d3fd0cc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.17.0 + rev: v1.17.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 3a2eff0868553e6cec0930bb996ed7a05acd8d52 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Aug 2025 22:22:04 +0000 Subject: [PATCH 123/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/pre-commit-hooks: v5.0.0 → v6.0.0](https://github.com/pre-commit/pre-commit-hooks/compare/v5.0.0...v6.0.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d3fd0cc..30581db 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,7 +4,7 @@ repos: hooks: - id: add-trailing-comma - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v5.0.0 + rev: v6.0.0 hooks: - id: check-yaml - id: debug-statements From 4b13c2cc19ef9436a1048a2dfcf0ee934d17dbf4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 15 Sep 2025 21:47:50 +0000 Subject: [PATCH 124/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.17.1 → v1.18.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.17.1...v1.18.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 30581db..14ac934 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.17.1 + rev: v1.18.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From e9f1cf3f484498c98c41f16c61c676b633b6c49d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Sep 2025 22:17:05 +0000 Subject: [PATCH 125/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.18.1 → v1.18.2](https://github.com/pre-commit/mirrors-mypy/compare/v1.18.1...v1.18.2) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 14ac934..20e006f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.18.1 + rev: v1.18.2 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 567cafc15a0b20ad814579c17b306c058029334c Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 16 Oct 2025 10:01:02 -0400 Subject: [PATCH 126/131] py310+ --- .github/workflows/main.yml | 14 ++++++------ .pre-commit-config.yaml | 12 +++++----- bin/gen-pycodestyle-plugin | 2 +- setup.cfg | 2 +- src/flake8/checker.py | 37 ------------------------------- src/flake8/discover_files.py | 2 +- src/flake8/options/manager.py | 2 +- tests/integration/test_checker.py | 16 ++++--------- tests/integration/test_main.py | 12 ++++------ 9 files changed, 25 insertions(+), 74 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 08f54ea..486b0cb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,10 +13,7 @@ jobs: include: # linux - os: ubuntu-latest - python: pypy-3.9 - toxenv: py - - os: ubuntu-latest - python: 3.9 + python: pypy-3.11 toxenv: py - os: ubuntu-latest python: '3.10' @@ -30,9 +27,12 @@ jobs: - os: ubuntu-latest python: '3.13' toxenv: py + - os: ubuntu-latest + python: '3.14' + toxenv: py # windows - os: windows-latest - python: 3.9 + python: '3.10' toxenv: py # misc - os: ubuntu-latest @@ -46,8 +46,8 @@ jobs: toxenv: dogfood runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python }} - run: python -mpip install --upgrade setuptools pip tox virtualenv diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 20e006f..2fac6b3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/asottile/add-trailing-comma - rev: v3.2.0 + rev: v4.0.0 hooks: - id: add-trailing-comma - repo: https://github.com/pre-commit/pre-commit-hooks @@ -12,23 +12,23 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/setup-cfg-fmt - rev: v2.8.0 + rev: v3.1.0 hooks: - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder-python-imports - rev: v3.15.0 + rev: v3.16.0 hooks: - id: reorder-python-imports args: [ --application-directories, '.:src', - --py39-plus, + --py310-plus, --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.20.0 + rev: v3.21.0 hooks: - id: pyupgrade - args: [--py39-plus] + args: [--py310-plus] - repo: https://github.com/hhatto/autopep8 rev: v2.3.2 hooks: diff --git a/bin/gen-pycodestyle-plugin b/bin/gen-pycodestyle-plugin index c93fbfe..7fc504a 100755 --- a/bin/gen-pycodestyle-plugin +++ b/bin/gen-pycodestyle-plugin @@ -3,9 +3,9 @@ from __future__ import annotations import inspect import os.path +from collections.abc import Callable from collections.abc import Generator from typing import Any -from typing import Callable from typing import NamedTuple import pycodestyle diff --git a/setup.cfg b/setup.cfg index dc967d3..c0b8137 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,7 +30,7 @@ install_requires = mccabe>=0.7.0,<0.8.0 pycodestyle>=2.14.0,<2.15.0 pyflakes>=3.4.0,<3.5.0 -python_requires = >=3.9 +python_requires = >=3.10 package_dir = =src diff --git a/src/flake8/checker.py b/src/flake8/checker.py index d957915..c6a24eb 100644 --- a/src/flake8/checker.py +++ b/src/flake8/checker.py @@ -372,43 +372,6 @@ class FileChecker: token = () row, column = (1, 0) - if ( - column > 0 - and token - and isinstance(exception, SyntaxError) - and len(token) == 4 # Python 3.9 or earlier - ): - # NOTE(sigmavirus24): SyntaxErrors report 1-indexed column - # numbers. We need to decrement the column number by 1 at - # least. - column_offset = 1 - row_offset = 0 - # See also: https://github.com/pycqa/flake8/issues/169, - # https://github.com/PyCQA/flake8/issues/1372 - # On Python 3.9 and earlier, token will be a 4-item tuple with the - # last item being the string. Starting with 3.10, they added to - # the tuple so now instead of it ending with the code that failed - # to parse, it ends with the end of the section of code that - # failed to parse. Luckily the absolute position in the tuple is - # stable across versions so we can use that here - physical_line = token[3] - - # NOTE(sigmavirus24): Not all "tokens" have a string as the last - # argument. In this event, let's skip trying to find the correct - # column and row values. - if physical_line is not None: - # NOTE(sigmavirus24): SyntaxErrors also don't exactly have a - # "physical" line so much as what was accumulated by the point - # tokenizing failed. - # See also: https://github.com/pycqa/flake8/issues/169 - lines = physical_line.rstrip("\n").split("\n") - row_offset = len(lines) - 1 - logical_line = lines[0] - logical_line_length = len(logical_line) - if column > logical_line_length: - column = logical_line_length - row -= row_offset - column -= column_offset return row, column def run_ast_checks(self) -> None: diff --git a/src/flake8/discover_files.py b/src/flake8/discover_files.py index da28ba5..40b6e5c 100644 --- a/src/flake8/discover_files.py +++ b/src/flake8/discover_files.py @@ -3,9 +3,9 @@ from __future__ import annotations import logging import os.path +from collections.abc import Callable from collections.abc import Generator from collections.abc import Sequence -from typing import Callable from flake8 import utils diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index addd3ec..ae40794 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -5,9 +5,9 @@ import argparse import enum import functools import logging +from collections.abc import Callable from collections.abc import Sequence from typing import Any -from typing import Callable from flake8 import utils from flake8.plugins.finder import Plugins diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py index 60d0132..f7f07af 100644 --- a/tests/integration/test_checker.py +++ b/tests/integration/test_checker.py @@ -2,7 +2,6 @@ from __future__ import annotations import importlib.metadata -import sys from unittest import mock import pytest @@ -322,17 +321,10 @@ def test_handling_syntaxerrors_across_pythons(): We need to handle that correctly to avoid crashing. https://github.com/PyCQA/flake8/issues/1372 """ - if sys.version_info < (3, 10): # pragma: no cover (<3.10) - # Python 3.9 or older - err = SyntaxError( - "invalid syntax", ("", 2, 5, "bad python:\n"), - ) - expected = (2, 4) - else: # pragma: no cover (3.10+) - err = SyntaxError( - "invalid syntax", ("", 2, 1, "bad python:\n", 2, 11), - ) - expected = (2, 1) + err = SyntaxError( + "invalid syntax", ("", 2, 1, "bad python:\n", 2, 11), + ) + expected = (2, 1) file_checker = checker.FileChecker( filename="-", plugins=finder.Checkers([], [], []), diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 5091573..0ca5b63 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -168,10 +168,8 @@ def test_tokenization_error_but_not_syntax_error(tmpdir, capsys): tmpdir.join("t.py").write("b'foo' \\\n") assert cli.main(["t.py"]) == 1 - if hasattr(sys, "pypy_version_info"): # pragma: no cover (pypy) - expected = "t.py:2:1: E999 SyntaxError: end of file (EOF) in multi-line statement\n" # noqa: E501 - elif sys.version_info < (3, 10): # pragma: no cover (cp38+) - expected = "t.py:1:8: E999 SyntaxError: unexpected EOF while parsing\n" + if sys.implementation.name == "pypy": # pragma: no cover (pypy) + expected = "t.py:1:9: E999 SyntaxError: unexpected end of file (EOF) in multi-line statement\n" # noqa: E501 else: # pragma: no cover (cp310+) expected = "t.py:1:10: E999 SyntaxError: unexpected EOF while parsing\n" # noqa: E501 @@ -186,10 +184,8 @@ def test_tokenization_error_is_a_syntax_error(tmpdir, capsys): tmpdir.join("t.py").write("if True:\n pass\n pass\n") assert cli.main(["t.py"]) == 1 - if hasattr(sys, "pypy_version_info"): # pragma: no cover (pypy) - expected = "t.py:3:2: E999 IndentationError: unindent does not match any outer indentation level\n" # noqa: E501 - elif sys.version_info < (3, 10): # pragma: no cover ( Date: Mon, 10 Nov 2025 22:38:48 +0000 Subject: [PATCH 127/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.21.0 → v3.21.1](https://github.com/asottile/pyupgrade/compare/v3.21.0...v3.21.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2fac6b3..c35ff64 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,7 +25,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.21.0 + rev: v3.21.1 hooks: - id: pyupgrade args: [--py310-plus] From 01af84d980b9d467e89cbb27e619eb1346c1c673 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Nov 2025 22:27:23 +0000 Subject: [PATCH 128/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v3.21.1 → v3.21.2](https://github.com/asottile/pyupgrade/compare/v3.21.1...v3.21.2) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c35ff64..02808bf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,7 +25,7 @@ repos: --add-import, 'from __future__ import annotations', ] - repo: https://github.com/asottile/pyupgrade - rev: v3.21.1 + rev: v3.21.2 hooks: - id: pyupgrade args: [--py310-plus] From 72c267d2e5eef80b59ff05dbe31a000b22ed4615 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Dec 2025 22:40:09 +0000 Subject: [PATCH 129/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.18.2 → v1.19.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.18.2...v1.19.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 02808bf..8108d1e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.18.2 + rev: v1.19.0 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 45c1af5e2426ff5a3638b6c771f3d27ebed92fbd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 15 Dec 2025 22:21:22 +0000 Subject: [PATCH 130/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v1.19.0 → v1.19.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.19.0...v1.19.1) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8108d1e..7d6a6ca 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,7 +38,7 @@ repos: hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.19.0 + rev: v1.19.1 hooks: - id: mypy exclude: ^(docs/|example-plugin/) From 941f908d6c0fa0aff9993ed1c1bb4e0aa11e9847 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Dec 2025 21:52:21 +0000 Subject: [PATCH 131/131] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/setup-cfg-fmt: v3.1.0 → v3.2.0](https://github.com/asottile/setup-cfg-fmt/compare/v3.1.0...v3.2.0) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7d6a6ca..f75e5ee 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,7 @@ repos: - id: trailing-whitespace exclude: ^tests/fixtures/ - repo: https://github.com/asottile/setup-cfg-fmt - rev: v3.1.0 + rev: v3.2.0 hooks: - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder-python-imports