Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: special case of auto value with flake8 config #653

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions autopep8.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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