How to use mccabe - 10 common examples

To help you get started, we’ve selected a few mccabe examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github marslo / myvim / Configurations / Offline_Packages / bundle / python-mode / submodules / mccabe / test_mccabe.py View on Github external
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
github marslo / myvim / Configurations / Offline_Packages / bundle / python-mode / submodules / mccabe / test_mccabe.py View on Github external
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)
github marslo / myvim / Configurations / Offline_Packages / bundle / python-mode / submodules / mccabe / test_mccabe.py View on Github external
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")
github marslo / myvim / Configurations / Offline_Packages / bundle / python-mode / submodules / mccabe / test_mccabe.py View on Github external
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)
github life4 / flakehell / flakehell / _logic / _extractors.py View on Github external
def extract_mccabe() -> Dict[str, str]:
    from mccabe import McCabeChecker

    code, message = McCabeChecker._error_tmpl.split(' ', maxsplit=1)
    return {code: message}
github dmerejkowsky / pycp / run-mccabe.py View on Github external
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())
github palantir / python-language-server / pyls / plugins / mccabe_lint.py View on Github external
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