From 71ac6f01c5c1cdfd505d795649ce11f87b5aca89 Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Thu, 27 Oct 2022 10:48:57 +0900 Subject: [PATCH 1/2] fix: special case of value with flake8 config --- autopep8.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/autopep8.py b/autopep8.py index ab1a88ac..c67aa473 100755 --- a/autopep8.py +++ b/autopep8.py @@ -3973,13 +3973,18 @@ def parse_args(arguments, apply_config=False): return args -def _get_normalize_options(config, section, option_list): - for (k, _) in config.items(section): +def _get_normalize_options(args, config, section, option_list): + for (k, v) in config.items(section): norm_opt = k.lstrip('-').replace('-', '_') if not option_list.get(norm_opt): continue opt_type = option_list[norm_opt] if opt_type is int: + if v.strip() == "auto": + # skip to special case + if args.verbose: + print(f"ignore config: {k}={v}") + continue value = config.getint(section, k) elif opt_type is bool: value = config.getboolean(section, k) @@ -4023,8 +4028,9 @@ def read_config(args, parser): for section in ['pep8', 'pycodestyle', 'flake8']: if not config.has_section(section): continue - for norm_opt, k, value in _get_normalize_options(config, section, - option_list): + for norm_opt, k, value in _get_normalize_options( + args, config, section, option_list + ): if args.verbose: print("enable config: section={}, key={}, value={}".format( section, k, value)) From f3584cadfa434f2b25807ab02f29c57b70a05cd3 Mon Sep 17 00:00:00 2001 From: Hideo Hattori Date: Fri, 28 Oct 2022 09:22:39 +0900 Subject: [PATCH 2/2] add unit tests --- test/test_autopep8.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/test/test_autopep8.py b/test/test_autopep8.py index d54423f5..99dc8a31 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -6135,7 +6135,8 @@ class ConfigurationTests(unittest.TestCase): def test_local_config(self): args = autopep8.parse_args( [os.path.join(FAKE_CONFIGURATION, 'foo.py'), - '--global-config={}'.format(os.devnull)], + '--global-config={}'.format(os.devnull), + '-vvv'], apply_config=True) self.assertEqual(args.indent_size, 2) @@ -6290,6 +6291,39 @@ def test_pyproject_toml_with_iterable_value(self): self.assertTrue(line in output) self.assertEqual(p.returncode, autopep8.EXIT_CODE_OK) + def test_setupcfg_with_flake8_config(self): + line = "a = 1\n" + fixed = "a = 1\n" + setupcfg_flake8 = """[flake8]\njobs=auto\n""" + with temporary_project_directory() as dirname: + with open(os.path.join(dirname, "setup.cfg"), "w") as fp: + fp.write(setupcfg_flake8) + target_filename = os.path.join(dirname, "foo.py") + with open(target_filename, "w") as fp: + fp.write(line) + p = Popen(list(AUTOPEP8_CMD_TUPLE) + + [target_filename, "-v"], stdout=PIPE) + output = p.communicate()[0].decode("utf-8") + self.assertTrue(fixed in output) + self.assertTrue("ignore config: jobs=auto" in output) + self.assertEqual(p.returncode, autopep8.EXIT_CODE_OK) + + def test_setupcfg_with_pycodestyle_config(self): + line = "a = 1\n" + fixed = "a = 1\n" + setupcfg_flake8 = """[pycodestyle]\ndiff=True\nignore="E,W"\n""" + with temporary_project_directory() as dirname: + with open(os.path.join(dirname, "setup.cfg"), "w") as fp: + fp.write(setupcfg_flake8) + target_filename = os.path.join(dirname, "foo.py") + with open(target_filename, "w") as fp: + fp.write(line) + p = Popen(list(AUTOPEP8_CMD_TUPLE) + + [target_filename, "-v"], stdout=PIPE) + output = p.communicate()[0].decode("utf-8") + self.assertTrue(fixed in output) + self.assertEqual(p.returncode, autopep8.EXIT_CODE_OK) + class ExperimentalSystemTests(unittest.TestCase):