Skip to content

Commit

Permalink
Merge pull request #653 from hhatto/fix-issue-515-652
Browse files Browse the repository at this point in the history
fix: special case of  `auto` value with flake8 config
  • Loading branch information
hhatto committed Oct 28, 2022
2 parents 2338500 + f3584ca commit 6e7635e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
14 changes: 10 additions & 4 deletions autopep8.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
36 changes: 35 additions & 1 deletion test/test_autopep8.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit 6e7635e

Please sign in to comment.