Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_missing_autospec(self, call_node):
def find_autospec_keyword(keyword_node):
for keyword_obj in keyword_node:
keyword = keyword_obj.arg
# If they have defined autospec or new then it is okay
if keyword in self.spec_keywords:
return True
return False
if isinstance(call_node, ast.Call):
func_info = FunctionNameFinder(self.filename)
func_info.visit(call_node)
# We are only looking at our patchers
if func_info.function_name not in self.patchers:
return
min_args = self.patchers[func_info.function_name]
if not find_autospec_keyword(call_node.keywords):
if len(call_node.args) < min_args:
self.messages.append(
(call_node.lineno, call_node.col_offset,
"H210 Missing 'autospec' or 'spec_set' keyword in "
"mock.patch/mock.patch.object", MockCheckVisitor)
)
def visit(self, node):
# If we get called with an ast.Call node, then work on the 'node.func',
# as we want the function name.
if isinstance(node, ast.Call):
return super(FunctionNameFinder, self).visit(node.func)
return super(FunctionNameFinder, self).visit(node)