From 59284839ef4262be4f28bd26d62c086ef3342c26 Mon Sep 17 00:00:00 2001 From: francisco souza <108725+fsouza@users.noreply.github.com> Date: Sat, 8 Oct 2022 22:58:30 -0400 Subject: [PATCH] Update merge_configuration_file to prefer values from flags The idea is that options set with flags take precedence over options set in the configuration file (any configuration file). --- autoflake.py | 5 +++-- test_autoflake.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/autoflake.py b/autoflake.py index b29bd1e..c14b15c 100755 --- a/autoflake.py +++ b/autoflake.py @@ -1245,8 +1245,9 @@ def merge_configuration_file(args): name, ) return False - if value: - setattr(args, name.replace("-", "_"), value) + arg = name.replace("-", "_") + if value and not hasattr(args, arg): + setattr(args, arg, value) else: _LOGGER.error("'%s' is not a valid configuration option", name) return False diff --git a/test_autoflake.py b/test_autoflake.py index fd2b1a1..7538d6c 100755 --- a/test_autoflake.py +++ b/test_autoflake.py @@ -3430,6 +3430,30 @@ def test_merge_with_cli_set_list_property(self): {"files": files, "imports": "my_lib,other_lib", "config_file": None}, ) + def test_merge_doesnt_override_existing_attributes(self): + self.create_file("test_me.py") + self.create_file( + "pyproject.toml", + "[tool.autoflake]\ncheck = false\n", + ) + files = [self.effective_path("test_me.py")] + args = Namespace( + files=files, + imports="other_lib", + config_file=None, + check=True, + ) + assert autoflake.merge_configuration_file(args) + self.assert_namespace( + args, + { + "files": files, + "imports": "other_lib", + "config_file": None, + "check": True, + }, + ) + @contextlib.contextmanager def temporary_file(contents, directory=".", suffix=".py", prefix=""):