Skip to content

Commit

Permalink
Parse hyphenated config names also
Browse files Browse the repository at this point in the history
Previously Flake8 parsed both

    max-line-length = 110

And

    max_line_length = 110

From the config file without issue. When we updated our logic, I forgot
to test for that and we lost that behaviour temporarily.

Closes #152
  • Loading branch information
sigmavirus24 committed Jun 28, 2016
1 parent 95c373c commit 31c32e3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/flake8/options/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _parse_config(self, config_parser):
LOG.debug('Option "%s" returned value: %r', option_name, value)

final_value = self._normalize_value(option, value)
config_dict[option_name] = final_value
config_dict[option.config_name] = final_value

return config_dict

Expand Down
4 changes: 3 additions & 1 deletion src/flake8/options/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ def add_option(self, *args, **kwargs):
self.parser.add_option(option.to_optparse())
self.options.append(option)
if option.parse_from_config:
self.config_options_dict[option.config_name] = option
name = option.config_name
self.config_options_dict[name] = option
self.config_options_dict[name.replace('_', '-')] = option
LOG.debug('Registered option "%s".', option)

def remove_from_default_ignore(self, error_codes):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
max-line-length = 110
enable_extensions =
H101,
H235
28 changes: 28 additions & 0 deletions tests/unit/test_merged_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,31 @@ def test_parsed_configs_are_equivalent(optmanager, config_fixture_path):
os.path.abspath('bar/'),
os.path.abspath('bogus/'),
]


@pytest.mark.parametrize('config_file', [
'tests/fixtures/config_files/config-with-hyphenated-options.ini'
])
def test_parsed_hyphenated_and_underscored_names(optmanager, config_file):
"""Verify we find hyphenated option names as well as underscored.
This tests for options like --max-line-length and --enable-extensions
which are able to be specified either as max-line-length or
max_line_length in our config files.
"""
optmanager.add_option('--max-line-length', parse_from_config=True,
type='int')
optmanager.add_option('--enable-extensions', parse_from_config=True,
comma_separated_list=True)
parser = config.MergedConfigParser(optmanager)
config_finder = parser.config_finder

with mock.patch.object(config_finder, 'local_config_files') as localcfs:
localcfs.return_value = [config_file]
with mock.patch.object(config_finder,
'user_config_file') as usercf:
usercf.return_value = []
parsed_config = parser.merge_user_and_local_config()

assert parsed_config['max_line_length'] == 110
assert parsed_config['enable_extensions'] == ['H101', 'H235']

0 comments on commit 31c32e3

Please sign in to comment.