Improving the flake8 version 2/3 “polyfill”
The suggested implementation to support both variants for flake8 could be improved by using Option.get_opt_string()
instead of the first argument. This would allow you to use a shorthand option in *args
as the first value. So register_opt(…, '-b', '--bar', …)
would actually search for bar
in the config with this addition but for b
without it.
def register_opt(parser, *args, **kwargs):
try:
# Flake8 3.x registration
parser.add_option(*args, **kwargs)
except (optparse.OptionError, TypeError):
# Flake8 2.x registration
parse_from_config = kwargs.pop('parse_from_config', False)
kwargs.pop('comma_separated_list', False)
kwargs.pop('normalize_paths', False)
option = parser.add_option(*args, **kwargs)
if parse_from_config:
parser.config_options.append(option.get_opt_string().lstrip('-'))
Additionally we can actually just delete the two unhandled arguments. Although to be honest they should cause errors as the result is different between both versions when they are set to True
. Something like the following should work:
assert kwargs.get('comma_separated_list', False) is False
assert kwargs.get('normalize_paths', False) is False