Giter Club home page Giter Club logo

flake8-cognitive-complexity's Introduction

flake8-cognitive-complexity

Build Status Maintainability Test Coverage PyPI version PyPI - Python Version

An extension for flake8 that validates cognitive functions complexity.

Cognitive complexity is analog of cyclomatic complexity, that measure how difficult to understand piece of code. Introduced by G. Ann Campbell and currently used by SonarSource, CodeClimate and others. You can find more readings about cognitive complexity in cognitive-complexity readme file.

Default complexity is 7, can be configured via --max-cognitive-complexity option.

Installation

pip install flake8-cognitive-complexity

Example

def f(a, b):
    if a:
        for i in range(b):
            if b:
                return 1

Usage:

$ flake8 --max-cognitive-complexity=3 test.py
text.py:1:5: CCR001 Cognitive complexity is too high (6 > 3)

Tested on Python 3.7.x and flake8 3.7.8.

Error codes

Error code Description
CCR001 Cognitive complexity is too high (X > Y)

Contributing

We would love you to contribute to our project. It's simple:

  • Create an issue with bug you found or proposal you have. Wait for approve from maintainer.
  • Create a pull request. Make sure all checks are green.
  • Fix review comments if any.
  • Be awesome.

Here are useful tips:

flake8-cognitive-complexity's People

Contributors

kyleking avatar languitar avatar melevir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

flake8-cognitive-complexity's Issues

Project still alive?

Hi,

just a simple question: Is this project still alive? I asking, since there are 6 open issues w/ no comments by the maintainer. Also there was no commit since 2 years and no release since 3 years.

cognitive complexity calculation is incorrect

Computed Cognitive complexity is 12 (I believe correct) for this:

class GildedRose:
    def update_quality(self):
        for item in self.items:  # + 1
            if (
                item.name != "Aged Brie"
                and item.name != "Backstage passes to a TAFKAL80ETC concert"
             ):  # + 2
             if item.quality > 0:  # + 3
                 if item.name != "Sulfuras, Hand of Ragnaros":  # + 4
                     item.quality = item.quality - 1
            else:
                pass

However, 10 is incorrect (less than before???) for following code:
Note complexity should be higher than before since code is the same with an added 'if' condition after the 'else'):

class GildedRose:
    def update_quality(self):
        for item in self.items:  # + 1
            if (
                item.name != "Aged Brie"
                and item.name != "Backstage passes to a TAFKAL80ETC concert"
             ):  # + 2
             if item.quality > 0:  # + 3
                 if item.name != "Sulfuras, Hand of Ragnaros":  # + 4
                     item.quality = item.quality - 1
            else:
                #pass
                if item.quality < 50:  # + 3
                    item.quality = item.quality

Plugin is not working on Python <3.7, but this is not mentioned in readme

Python starts from 3.7? Seems, plugin fails on Python 3.6.8 on line:
from __future__ import annotations
Is it possible to add information about requirements in readme?

/home/andy/PROJECTS/events-registration/.env/bin/flake8 --max-complexity 10 --max-cognitive-complexity=3 /home/andy/PROJECTS/events-registration/backend/backend
Traceback (most recent call last):
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/plugins/manager.py", line 158, in load_plugin
    self._load()
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/plugins/manager.py", line 135, in _load
    self._plugin = self.entry_point.load()
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/entrypoints.py", line 82, in load
    mod = import_module(self.module_name)
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 781, in get_code
  File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8_cognitive_complexity/checker.py", line 1
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/andy/PROJECTS/events-registration/.env/bin/flake8", line 8, in <module>
    sys.exit(main())
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/main/cli.py", line 18, in main
    app.run(argv)
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/main/application.py", line 393, in run
    self._run(argv)
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/main/application.py", line 380, in _run
    self.initialize(argv)
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/main/application.py", line 363, in initialize
    self.find_plugins()
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/main/application.py", line 199, in find_plugins
    self.check_plugins.load_plugins()
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/plugins/manager.py", line 410, in load_plugins
    plugins = list(self.manager.map(load_plugin))
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/plugins/manager.py", line 297, in map
    yield func(self.plugins[name], *args, **kwargs)
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/plugins/manager.py", line 408, in load_plugin
    return plugin.load_plugin()
  File "/home/andy/PROJECTS/events-registration/.env/lib/python3.6/site-packages/flake8/plugins/manager.py", line 165, in load_plugin
    raise failed_to_load
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "CCR" due to future feature annotations is not defined (checker.py, line 1).

Process finished with exit code 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.