Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_complexity_number(snippet, strio, max=0):
"""Get the complexity number from the printed string."""
# Report from the lowest complexity number.
get_code_complexity(snippet, max)
strio_val = strio.getvalue()
if strio_val:
return int(strio_val.split()[-1].strip("()"))
else:
return None
def setUp(self):
self.original_complexity = mccabe.McCabeChecker.max_complexity
def test_max_complexity_is_always_an_int(self):
"""Ensure bug #32 does not regress."""
class _options(object):
max_complexity = None
options = _options()
options.max_complexity = '16'
self.assertEqual(0, mccabe.McCabeChecker.max_complexity)
mccabe.McCabeChecker.parse_options(options)
self.assertEqual(16, mccabe.McCabeChecker.max_complexity)
def tearDown(self):
mccabe.McCabeChecker.max_complexity = self.original_complexity
def test_print_message(self):
get_code_complexity(sequential, 0)
printed_message = self.strio.getvalue()
self.assertEqual(printed_message,
"stdin:1:1: C901 'f' is too complex (1)\n")
def test_max_complexity_is_always_an_int(self):
"""Ensure bug #32 does not regress."""
class _options(object):
max_complexity = None
options = _options()
options.max_complexity = '16'
self.assertEqual(0, mccabe.McCabeChecker.max_complexity)
mccabe.McCabeChecker.parse_options(options)
self.assertEqual(16, mccabe.McCabeChecker.max_complexity)
def extract_mccabe() -> Dict[str, str]:
from mccabe import McCabeChecker
code, message = McCabeChecker._error_tmpl.split(' ', maxsplit=1)
return {code: message}
def process(py_source, max_complexity):
code = py_source.text()
tree = compile(code, py_source, "exec", ast.PyCF_ONLY_AST)
visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)
for graph in visitor.graphs.values():
if graph.complexity() > max_complexity:
text = "{}:{}:{} {} {}"
return text.format(py_source, graph.lineno, graph.column, graph.entity,
graph.complexity())
def pyls_lint(config, document):
threshold = config.plugin_settings('mccabe').get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)
try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None
visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)
diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})
return diags