mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-10 06:44:18 +00:00
Set configuration file-provided values via ArgumentParser.set_defaults()
When calling `ArgumentParser.parse_args()` with the `namespace` argument, command-line options are just added to the namespace without going through any of the argument parsing and type conversion logic (e.g., the `type` keyword argument of `ArgumentParser.add_argument()`). In other words, it is assumed that a namespace is well-formed from a previous invocation of `ArgumentParser.parse_args()`. The `values` parameter is intended to be values already-provided from configuration files. To take advantage of the logic defined by `ArgumentParser.add_argument()`, utilize `ArgumentParser.set_defaults()` instead.
This commit is contained in:
parent
82081badb0
commit
b8c5dbf862
2 changed files with 11 additions and 1 deletions
|
|
@ -433,7 +433,9 @@ class OptionManager(object):
|
||||||
assert isinstance( # nosec (for bandit)
|
assert isinstance( # nosec (for bandit)
|
||||||
self.parser, argparse.ArgumentParser
|
self.parser, argparse.ArgumentParser
|
||||||
), self.parser
|
), self.parser
|
||||||
parsed_args = self.parser.parse_args(args, values)
|
if values:
|
||||||
|
self.parser.set_defaults(**vars(values))
|
||||||
|
parsed_args = self.parser.parse_args(args)
|
||||||
# TODO: refactor callers to not need this
|
# TODO: refactor callers to not need this
|
||||||
return parsed_args, parsed_args.filenames
|
return parsed_args, parsed_args.filenames
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,14 @@ def test_parse_args_forwarding_default_values(optmanager):
|
||||||
assert options.foo == 'bar'
|
assert options.foo == 'bar'
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_args_forwarding_type_coercion(optmanager):
|
||||||
|
"""Verify default provided values are type converted from add_option."""
|
||||||
|
optmanager.add_option('--foo', type=int)
|
||||||
|
namespace = argparse.Namespace(foo='5')
|
||||||
|
options, args = optmanager.parse_args([], namespace)
|
||||||
|
assert options.foo == 5
|
||||||
|
|
||||||
|
|
||||||
def test_add_option_short_option_only(optmanager):
|
def test_add_option_short_option_only(optmanager):
|
||||||
"""Verify the behaviour of adding a short-option only."""
|
"""Verify the behaviour of adding a short-option only."""
|
||||||
assert optmanager.options == []
|
assert optmanager.options == []
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue