From c609b69914e96f483a305534a7a33e2d5b64b809 Mon Sep 17 00:00:00 2001 From: Joseph Egan Date: Wed, 12 May 2021 00:30:04 +0100 Subject: [PATCH 1/2] Add support for reading config from pyproject.toml --- setup.cfg | 1 + src/flake8/options/config.py | 17 +++++++++++++-- .../config_files/broken-pyproject.toml | 12 +++++++++++ .../config_files/cli-specified-pyproject.toml | 12 +++++++++++ .../no-flake8-section-pyproject.toml | 12 +++++++++++ tests/unit/test_config_file_finder.py | 21 ++++++++++++++++--- tests/unit/test_merged_config_parser.py | 2 ++ 7 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/config_files/broken-pyproject.toml create mode 100644 tests/fixtures/config_files/cli-specified-pyproject.toml create mode 100644 tests/fixtures/config_files/no-flake8-section-pyproject.toml diff --git a/setup.cfg b/setup.cfg index 1c542604..3dea164d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -42,6 +42,7 @@ install_requires= pycodestyle >= 2.7.0, < 2.8.0 mccabe >= 0.6.0, < 0.7.0 importlib-metadata; python_version<"3.8" + toml python_requires = >=3.6 diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index e920e582..e9526365 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -7,6 +7,7 @@ from typing import Optional from typing import Tuple +import toml from flake8 import utils LOG = logging.getLogger(__name__) @@ -51,7 +52,9 @@ def __init__( self.user_config_file = self._user_config_file(program_name) # List of filenames to find in the local/project directory - self.project_filenames = ("setup.cfg", "tox.ini", f".{program_name}") + self.project_filenames = ( + "pyproject.toml", "setup.cfg", "tox.ini", f".{program_name}", + ) self.local_directory = os.path.abspath(os.curdir) @@ -77,7 +80,17 @@ def _read_config( found_files = [] for filename in files: try: - found_files.extend(config.read(filename)) + if filename.endswith("pyproject.toml"): + pyproject_config = toml.load(filename) + config.read_dict( + { + "flake8": pyproject_config["tool"]["flake8"], + }, + source=filename, + ) + found_files.append(filename) + else: + found_files.extend(config.read(filename)) except UnicodeDecodeError: LOG.exception( "There was an error decoding a config file." diff --git a/tests/fixtures/config_files/broken-pyproject.toml b/tests/fixtures/config_files/broken-pyproject.toml new file mode 100644 index 00000000..2d7227cf --- /dev/null +++ b/tests/fixtures/config_files/broken-pyproject.toml @@ -0,0 +1,12 @@ +[tool.flake8] +ignore = [ + "E123", + "W234", + E111, +] +exclude = [ + "foo/", + "bar/", + "bogus/", +] +quiet = 1 diff --git a/tests/fixtures/config_files/cli-specified-pyproject.toml b/tests/fixtures/config_files/cli-specified-pyproject.toml new file mode 100644 index 00000000..570a2462 --- /dev/null +++ b/tests/fixtures/config_files/cli-specified-pyproject.toml @@ -0,0 +1,12 @@ +[tool.flake8] +ignore = [ + "E123", + "W234", + "E111", +] +exclude = [ + "foo/", + "bar/", + "bogus/", +] +quiet = 1 diff --git a/tests/fixtures/config_files/no-flake8-section-pyproject.toml b/tests/fixtures/config_files/no-flake8-section-pyproject.toml new file mode 100644 index 00000000..6aa528ae --- /dev/null +++ b/tests/fixtures/config_files/no-flake8-section-pyproject.toml @@ -0,0 +1,12 @@ +[tool.flake9] +ignore = [ + "E123", + "W234", + "E111", +] +exclude = [ + "foo/", + "bar/", + "bogus/", +] +quiet = 1 diff --git a/tests/unit/test_config_file_finder.py b/tests/unit/test_config_file_finder.py index 51167962..364636d2 100644 --- a/tests/unit/test_config_file_finder.py +++ b/tests/unit/test_config_file_finder.py @@ -10,10 +10,19 @@ CLI_SPECIFIED_FILEPATH = "tests/fixtures/config_files/cli-specified.ini" BROKEN_CONFIG_PATH = "tests/fixtures/config_files/broken.ini" +CLI_SPECIFIED_PYPROJECT_CONFIG_PATH = "tests/fixtures/config_files/cli-specified-pyproject.toml" # noqa: E501 +BROKEN_PYPROJECT_CONFIG_PATH = "tests/fixtures/config_files/broken-pyproject.toml" # noqa: E501 -def test_cli_config(): + +@pytest.mark.parametrize( + "cli_filepath", + [ + CLI_SPECIFIED_FILEPATH, + CLI_SPECIFIED_PYPROJECT_CONFIG_PATH, + ], +) +def test_cli_config(cli_filepath): """Verify opening and reading the file specified via the cli.""" - cli_filepath = CLI_SPECIFIED_FILEPATH finder = config.ConfigFileFinder("flake8") parsed_config = finder.cli_config(cli_filepath) @@ -91,13 +100,19 @@ def test_local_configs(): "files", [ [BROKEN_CONFIG_PATH], - [CLI_SPECIFIED_FILEPATH, BROKEN_CONFIG_PATH], + [BROKEN_PYPROJECT_CONFIG_PATH], + [ + CLI_SPECIFIED_FILEPATH, + BROKEN_CONFIG_PATH, + BROKEN_PYPROJECT_CONFIG_PATH, + ], ], ) def test_read_config_catches_broken_config_files(files): """Verify that we do not allow the exception to bubble up.""" _, parsed = config.ConfigFileFinder._read_config(*files) assert BROKEN_CONFIG_PATH not in parsed + assert BROKEN_PYPROJECT_CONFIG_PATH not in parsed def test_read_config_catches_decoding_errors(tmpdir): diff --git a/tests/unit/test_merged_config_parser.py b/tests/unit/test_merged_config_parser.py index b19291c6..3c53f1a5 100644 --- a/tests/unit/test_merged_config_parser.py +++ b/tests/unit/test_merged_config_parser.py @@ -54,6 +54,7 @@ def test_parse_cli_config(optmanager, config_finder): [ ("tests/fixtures/config_files/cli-specified.ini", True), ("tests/fixtures/config_files/no-flake8-section.ini", False), + ("tests/fixtures/config_files/no-flake8-section-pyproject.toml", False), # noqa: E501 ], ) def test_is_configured_by( @@ -188,6 +189,7 @@ def test_parse_uses_cli_config(optmanager): "tests/fixtures/config_files/cli-specified.ini", "tests/fixtures/config_files/cli-specified-with-inline-comments.ini", "tests/fixtures/config_files/cli-specified-without-inline-comments.ini", # noqa: E501 + "tests/fixtures/config_files/cli-specified-pyproject.toml", ], ) def test_parsed_configs_are_equivalent( From a20ce903c72bc49f79c14a61580cfecdc98b55c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 May 2021 23:45:09 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/flake8/options/config.py | 6 +++++- tests/unit/test_config_file_finder.py | 8 ++++++-- tests/unit/test_merged_config_parser.py | 5 ++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index e9526365..56778d5e 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -8,6 +8,7 @@ from typing import Tuple import toml + from flake8 import utils LOG = logging.getLogger(__name__) @@ -53,7 +54,10 @@ def __init__( # List of filenames to find in the local/project directory self.project_filenames = ( - "pyproject.toml", "setup.cfg", "tox.ini", f".{program_name}", + "pyproject.toml", + "setup.cfg", + "tox.ini", + f".{program_name}", ) self.local_directory = os.path.abspath(os.curdir) diff --git a/tests/unit/test_config_file_finder.py b/tests/unit/test_config_file_finder.py index 364636d2..919cc4d1 100644 --- a/tests/unit/test_config_file_finder.py +++ b/tests/unit/test_config_file_finder.py @@ -10,8 +10,12 @@ CLI_SPECIFIED_FILEPATH = "tests/fixtures/config_files/cli-specified.ini" BROKEN_CONFIG_PATH = "tests/fixtures/config_files/broken.ini" -CLI_SPECIFIED_PYPROJECT_CONFIG_PATH = "tests/fixtures/config_files/cli-specified-pyproject.toml" # noqa: E501 -BROKEN_PYPROJECT_CONFIG_PATH = "tests/fixtures/config_files/broken-pyproject.toml" # noqa: E501 +CLI_SPECIFIED_PYPROJECT_CONFIG_PATH = ( + "tests/fixtures/config_files/cli-specified-pyproject.toml" # noqa: E501 +) +BROKEN_PYPROJECT_CONFIG_PATH = ( + "tests/fixtures/config_files/broken-pyproject.toml" # noqa: E501 +) @pytest.mark.parametrize( diff --git a/tests/unit/test_merged_config_parser.py b/tests/unit/test_merged_config_parser.py index 3c53f1a5..c6e00187 100644 --- a/tests/unit/test_merged_config_parser.py +++ b/tests/unit/test_merged_config_parser.py @@ -54,7 +54,10 @@ def test_parse_cli_config(optmanager, config_finder): [ ("tests/fixtures/config_files/cli-specified.ini", True), ("tests/fixtures/config_files/no-flake8-section.ini", False), - ("tests/fixtures/config_files/no-flake8-section-pyproject.toml", False), # noqa: E501 + ( + "tests/fixtures/config_files/no-flake8-section-pyproject.toml", + False, + ), # noqa: E501 ], ) def test_is_configured_by(