From 45577b3a47648528e96f50400728bbe6153b4e5c Mon Sep 17 00:00:00 2001 From: "David A. Hannasch" Date: Sun, 2 Oct 2022 16:21:09 -0600 Subject: [PATCH] Emit error on invalid config (multiple files). Emit an error message if the configuration is invalid due to multiple configuration files. This shows how we can easily emit an error in the simplest case, though we can also use _walk_up_directories_to_root_or_home to make this more robust. Resolves #1704. --- src/flake8/options/config.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index c003cc5..75c4823 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -53,8 +53,12 @@ def _walk_up_directories_to_root_or_home( def _find_config_file(path: str) -> str | None: + configuration_file_priority_order = ("setup.cfg", "tox.ini", ".flake8") + # This priority order is documented in docs/source/user/configuration.rst + # and must be kept in sync with the documentation there. for path in _walk_up_directories_to_root_or_home(path): - for candidate in ("setup.cfg", "tox.ini", ".flake8"): + cfg_paths_found = set() + for candidate in configuration_file_priority_order: cfg = configparser.RawConfigParser() cfg_path = os.path.join(path, candidate) try: @@ -65,6 +69,12 @@ def _find_config_file(path: str) -> str | None: # only consider it a config if it contains flake8 sections if "flake8" in cfg or "flake8:local-plugins" in cfg: return cfg_path + if len(cfg_paths_found) == 1: + return cfg_paths_found.pop() + elif len(cfg_paths_found) > 1: + raise exceptions.ExecutionError( + f"Multiple config files found: {cfg_paths_found}" + ) # did not find any configuration file return None