Update register_opts hacking check to allow tuples

Update the hacking check for register_opts to allow tuples to be
passed in directly as an argument. Previously tuples were getting
flagged as invalid due to a missing pair of parenthesis in the
hacking check.

Change-Id: Ida2ae47eae4ebc6188696dc25943226acd67b5de
Closes-Bug: 1511010
This commit is contained in:
Ryan McNair
2015-10-27 22:46:46 +00:00
parent 8f60efa9c6
commit cf4378382a
2 changed files with 13 additions and 11 deletions

View File

@@ -304,6 +304,9 @@ class CheckOptRegistrationArgs(BaseASTChecker):
else: # could be Subscript, Call or many more
return None
def _is_list_or_tuple(self, obj):
return isinstance(obj, ast.List) or isinstance(obj, ast.Tuple)
def visit_Call(self, node):
"""Look for the register_opt/register_opts calls."""
# extract the obj_name and method_name
@@ -319,7 +322,6 @@ class CheckOptRegistrationArgs(BaseASTChecker):
return (super(CheckOptRegistrationArgs,
self).generic_visit(node))
# argument should be a list with register_opts()
if len(node.args) > 0:
argument_name = self._find_name(node.args[0])
if argument_name:
@@ -335,12 +337,10 @@ class CheckOptRegistrationArgs(BaseASTChecker):
# passing in a variable referencing the options being
# registered.
if (method_name == self.singular_method and
(isinstance(node.args[0], ast.List)) or
isinstance(node.args[0], ast.Tuple)):
self._is_list_or_tuple(node.args[0])):
self.add_error(node.args[0])
elif (method_name == self.plural_method and
(not isinstance(node.args[0], ast.List)) or
isinstance(node.args[0], ast.Tuple)):
elif (method_name == self.plural_method and not
self._is_list_or_tuple(node.args[0])):
self.add_error(node.args[0])
return super(CheckOptRegistrationArgs, self).generic_visit(node)

View File

@@ -179,6 +179,7 @@ class HackingTestCase(test.TestCase):
checker = checks.CheckOptRegistrationArgs
code = """
CONF.register_opts([opt1, opt2, opt3])
CONF.register_opts((opt4, opt5))
CONF.register_opt(lonely_opt)
CONF.register_opts([OPT1, OPT2], group="group_of_opts")
CONF.register_opt(single_opt, group=blah)
@@ -187,14 +188,15 @@ class HackingTestCase(test.TestCase):
code = """
CONF.register_opt([opt4, opt5, opt6])
CONF.register_opt((opt7, opt8))
CONF.register_opts(lonely_opt)
CONF.register_opt((an_opt, another_opt))
"""
for method in checker.register_methods:
self._assert_has_errors(code.format(method), checker,
expected_errors=[(1, 18, 'C311'),
(2, 19, 'C311'),
(3, 19, 'C311')])
self._assert_has_errors(code, checker,
expected_errors=[(1, 18, 'C311'),
(2, 19, 'C311'),
(3, 19, 'C311'),
(4, 19, 'C311')])
code = """
CONF.register_opt(single_opt)