Error handling for KeyValueAction class.
The set --property command requires that the input match the "key=value" type, but if the type don't match, the return value will be None, and the command still can be implemented successfully, this may confuse the users. I think we should raise exception if the argument type don't match "key=value". So I make some changes in KeyValueAction class in this patch. Change-Id: I14e64922faa7e083bc8b5e7e1cac41ef8117c224 Closes-Bug: #1589935
This commit is contained in:
parent
5293bb103e
commit
cf12239773
@ -35,7 +35,9 @@ class KeyValueAction(argparse.Action):
|
|||||||
if '=' in values:
|
if '=' in values:
|
||||||
getattr(namespace, self.dest, {}).update([values.split('=', 1)])
|
getattr(namespace, self.dest, {}).update([values.split('=', 1)])
|
||||||
else:
|
else:
|
||||||
getattr(namespace, self.dest, {}).pop(values, None)
|
msg = _("Expected 'key=value' type, "
|
||||||
|
"but got: %s") % (str(values))
|
||||||
|
raise argparse.ArgumentTypeError(msg)
|
||||||
|
|
||||||
|
|
||||||
class MultiKeyValueAction(argparse.Action):
|
class MultiKeyValueAction(argparse.Action):
|
||||||
|
@ -49,16 +49,13 @@ class TestKeyValueAction(utils.TestCase):
|
|||||||
self.assertDictEqual(expect, actual)
|
self.assertDictEqual(expect, actual)
|
||||||
|
|
||||||
def test_error_values(self):
|
def test_error_values(self):
|
||||||
results = self.parser.parse_args([
|
self.assertRaises(
|
||||||
'--property', 'red',
|
argparse.ArgumentTypeError,
|
||||||
'--property', 'green=100%',
|
self.parser.parse_args,
|
||||||
'--property', 'blue',
|
[
|
||||||
])
|
'--property', 'red',
|
||||||
|
]
|
||||||
actual = getattr(results, 'property', {})
|
)
|
||||||
# There should be no red or blue
|
|
||||||
expect = {'green': '100%', 'format': '#rgb'}
|
|
||||||
self.assertDictEqual(expect, actual)
|
|
||||||
|
|
||||||
|
|
||||||
class TestMultiKeyValueAction(utils.TestCase):
|
class TestMultiKeyValueAction(utils.TestCase):
|
||||||
|
5
releasenotes/notes/bug-1589935-8a56e6a18d836db9.yaml
Normal file
5
releasenotes/notes/bug-1589935-8a56e6a18d836db9.yaml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
fixes:
|
||||||
|
- Raise ``ArgumentTypeError`` if the input arguments do not match the type
|
||||||
|
``key=value`` when we set properties.
|
||||||
|
[Bug `1589935 <https://bugs.launchpad.net/bugs/1589935>`_]
|
Loading…
Reference in New Issue
Block a user