Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reading config from pyproject.toml #1332

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 19 additions & 2 deletions src/flake8/options/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from typing import Optional
from typing import Tuple

import toml

from flake8 import utils

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -51,7 +53,12 @@ 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)

Expand All @@ -77,7 +84,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."
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/config_files/broken-pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.flake8]
ignore = [
"E123",
"W234",
E111,
]
exclude = [
"foo/",
"bar/",
"bogus/",
]
quiet = 1
12 changes: 12 additions & 0 deletions tests/fixtures/config_files/cli-specified-pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.flake8]
ignore = [
"E123",
"W234",
"E111",
]
exclude = [
"foo/",
"bar/",
"bogus/",
]
quiet = 1
12 changes: 12 additions & 0 deletions tests/fixtures/config_files/no-flake8-section-pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.flake9]
ignore = [
"E123",
"W234",
"E111",
]
exclude = [
"foo/",
"bar/",
"bogus/",
]
quiet = 1
25 changes: 22 additions & 3 deletions tests/unit/test_config_file_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@
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)
Expand Down Expand Up @@ -91,13 +104,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):
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_merged_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +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
],
)
def test_is_configured_by(
Expand Down Expand Up @@ -188,6 +192,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(
Expand Down