From c34fd755f06eb06a78bcfcedf8b74aecbc7e801e Mon Sep 17 00:00:00 2001 From: Buck Evan Date: Tue, 20 Dec 2022 12:28:52 -0600 Subject: [PATCH] allow idiomatic setup.cfg comments --- src/flake8/utils.py | 5 +++-- tests/unit/test_options_config.py | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/flake8/utils.py b/src/flake8/utils.py index afc3896..8509d6c 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -17,7 +17,8 @@ from typing import Sequence from flake8 import exceptions -COMMA_SEPARATED_LIST_RE = re.compile(r"[,\s]") +COMMA_SEPARATED_LIST_RE = re.compile(r"[,\s]+") +STRIP_RE = re.compile(r"^\s+|\s+$|\s*#.*$") LOCAL_PLUGIN_LIST_RE = re.compile(r"[,\t\n\r\f\v]") NORMALIZE_PACKAGE_NAME_RE = re.compile(r"[-_.]+") @@ -38,7 +39,7 @@ def parse_comma_separated_list( assert isinstance(value, str), value separated = regexp.split(value) - item_gen = (item.strip() for item in separated) + item_gen = (STRIP_RE.sub("", item) for item in separated) return [item for item in item_gen if item] diff --git a/tests/unit/test_options_config.py b/tests/unit/test_options_config.py index 7de58f0..05f27ad 100644 --- a/tests/unit/test_options_config.py +++ b/tests/unit/test_options_config.py @@ -243,6 +243,15 @@ def test_invalid_ignore_codes_raise_error(tmpdir, opt_manager): assert msg == expected +def test_ignore_code_comments_dont_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) + + ret = config.parse_config(opt_manager, cfg, tmpdir) + assert ret == {"ignore": ["E203"]} + + 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():