pax_global_header00006660000000000000000000000064137112011610014503gustar00rootroot0000000000000052 comment=a17a433aa270a81796091a9459508618d158788c Melevir-flake8-cognitive-complexity-a17a433/000077500000000000000000000000001371120116100207635ustar00rootroot00000000000000Melevir-flake8-cognitive-complexity-a17a433/.editorconfig000066400000000000000000000004231371120116100234370ustar00rootroot00000000000000# http://editorconfig.org # Inspired by Django .editorconfig file root = true [*] indent_style = space indent_size = 4 insert_final_newline = true trim_trailing_whitespace = true end_of_line = lf charset = utf-8 [*.py] max_line_length = 100 [Makefile] indent_style = tab Melevir-flake8-cognitive-complexity-a17a433/.gitignore000066400000000000000000000023151371120116100227540ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ # VS code .vscode .idea Melevir-flake8-cognitive-complexity-a17a433/.travis.yml000066400000000000000000000006641371120116100231020ustar00rootroot00000000000000language: python python: - "3.6" - "3.7" install: - gem install mdl - pip install -r requirements_dev.txt - pip install -e . before_script: - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter - chmod +x ./cc-test-reporter - ./cc-test-reporter before-build script: - make check after_script: - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT Melevir-flake8-cognitive-complexity-a17a433/LICENSE000066400000000000000000000020551371120116100217720ustar00rootroot00000000000000MIT License Copyright (c) 2020 Ilya Lebedev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Melevir-flake8-cognitive-complexity-a17a433/Makefile000066400000000000000000000001551371120116100224240ustar00rootroot00000000000000check: flake8 . mypy . python -m pytest --cov=flake8_cognitive_complexity --cov-report=xml mdl README.md Melevir-flake8-cognitive-complexity-a17a433/README.md000066400000000000000000000035631371120116100222510ustar00rootroot00000000000000# flake8-cognitive-complexity [![Build Status](https://travis-ci.org/Melevir/flake8-cognitive-complexity.svg?branch=master)](https://travis-ci.org/Melevir/flake8-cognitive-complexity) [![Maintainability](https://api.codeclimate.com/v1/badges/579738d149e489c631a6/maintainability)](https://codeclimate.com/github/Melevir/flake8-cognitive-complexity/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/579738d149e489c631a6/test_coverage)](https://codeclimate.com/github/Melevir/flake8-cognitive-complexity/test_coverage) [![PyPI version](https://badge.fury.io/py/flake8-cognitive-complexity.svg)](https://badge.fury.io/py/flake8-cognitive-complexity) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/flake8-cognitive-complexity) 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](https://github.com/ganncamp) and currently used by SonarSource, CodeClimate and others. You can find more readings about cognitive complexity in [cognitive-complexity readme file](https://github.com/Melevir/cognitive_complexity/blob/master/README.md#what-is-cognitive-complexity). Default complexity is 7, can be configured via `--max-cognitive-complexity` option. ## Installation ```terminal pip install flake8-cognitive-complexity ``` ## Example ```python def f(a, b): if a: for i in range(b): if b: return 1 ``` Usage: ```terminal $ 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) | Melevir-flake8-cognitive-complexity-a17a433/flake8_cognitive_complexity/000077500000000000000000000000001371120116100264615ustar00rootroot00000000000000Melevir-flake8-cognitive-complexity-a17a433/flake8_cognitive_complexity/__init__.py000066400000000000000000000000261371120116100305700ustar00rootroot00000000000000__version__ = '0.1.0' Melevir-flake8-cognitive-complexity-a17a433/flake8_cognitive_complexity/checker.py000066400000000000000000000027761371120116100304530ustar00rootroot00000000000000import ast from typing import Generator, Tuple from cognitive_complexity.api import get_cognitive_complexity from flake8_cognitive_complexity import __version__ as version class CognitiveComplexityChecker: DEFAULT_MAX_COGNITIVE_COMPLEXITY = 7 name = 'flake8-cognitive-complexity' version = version max_cognitive_complexity = DEFAULT_MAX_COGNITIVE_COMPLEXITY def __init__(self, tree, filename: str): self.filename = filename self.tree = tree @classmethod def add_options(cls, parser) -> None: parser.add_option( '--max-cognitive-complexity', type=int, default=cls.DEFAULT_MAX_COGNITIVE_COMPLEXITY, parse_from_config=True, ) @classmethod def parse_options(cls, options) -> None: cls.max_cognitive_complexity = int(options.max_cognitive_complexity) def run(self) -> Generator[Tuple[int, int, str, type], None, None]: funcdefs = ( n for n in ast.walk(self.tree) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) ) for funcdef in funcdefs: complexity = get_cognitive_complexity(funcdef) if complexity > self.max_cognitive_complexity: yield ( funcdef.lineno, funcdef.col_offset, f'CCR001 Cognitive complexity is too high ' f'({complexity} > {self.max_cognitive_complexity})', type(self), ) Melevir-flake8-cognitive-complexity-a17a433/requirements_dev.txt000066400000000000000000000011001371120116100250750ustar00rootroot00000000000000mypy==0.740 mypy-extensions==0.4.1 typing==3.6.4 typing-extensions==3.7.4 flake8==3.7.8 flake8-annotations-complexity==0.0.2 flake8-blind-except==0.1.1 flake8-broken-line==0.1.1 flake8-bugbear==19.8.0 flake8-builtins==1.4.1 flake8-commas==2.0.0 flake8-comprehensions==3.0.0 flake8-debugger==3.2.0 flake8-docstrings==1.5.0 flake8-functions==0.0.1 flake8-polyfill==1.0.2 flake8-print==3.1.1 flake8-quotes==2.1.0 flake8-string-format==0.2.3 flake8-todo==0.7 flake8-variables-names==0.0.2 pytest==4.2.0 pytest-cov==2.6.1 attrs==19.1.0 astpretty==1.6.0 cognitive_complexity==0.0.3 Melevir-flake8-cognitive-complexity-a17a433/setup.cfg000066400000000000000000000003731371120116100226070ustar00rootroot00000000000000[flake8] ignore = D, W503 max-line-length = 100 use_class_attributes_order_strict_mode = True max_function_length = 50 max-complexity = 6 per-file-ignores = tests/test_files/*: VNE, B [mypy] ignore_missing_imports = True allow_redefinition = True Melevir-flake8-cognitive-complexity-a17a433/setup.py000066400000000000000000000033631371120116100225020ustar00rootroot00000000000000from typing import Optional from setuptools import setup, find_packages package_name = 'flake8_cognitive_complexity' def get_version() -> Optional[str]: with open('flake8_cognitive_complexity/__init__.py', 'r') as f: lines = f.readlines() for line in lines: if line.startswith('__version__'): return line.split('=')[-1].strip().strip("'") return None def get_long_description() -> str: with open('README.md') as f: return f.read() setup( name=package_name, description='An extension for flake8 that validates cognitive functions complexity', classifiers=[ 'Environment :: Console', 'Framework :: Flake8', 'Operating System :: OS Independent', 'Topic :: Software Development :: Documentation', 'Topic :: Software Development :: Libraries :: Python Modules', 'Topic :: Software Development :: Quality Assurance', 'Programming Language :: Python', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', ], long_description=get_long_description(), long_description_content_type='text/markdown', packages=find_packages(), python_requires='>=3.6', include_package_data=True, keywords='flake8', version=get_version(), author='Ilya Lebedev', author_email='melevir@gmail.com', install_requires=['setuptools', 'cognitive_complexity'], entry_points={ 'flake8.extension': [ 'CCR = flake8_cognitive_complexity.checker:CognitiveComplexityChecker', ], }, url='https://github.com/Melevir/flake8-cognitive-complexity', license='MIT', py_modules=[package_name], zip_safe=False, ) Melevir-flake8-cognitive-complexity-a17a433/tests/000077500000000000000000000000001371120116100221255ustar00rootroot00000000000000Melevir-flake8-cognitive-complexity-a17a433/tests/conftest.py000066400000000000000000000013011371120116100243170ustar00rootroot00000000000000import ast import os from flake8_cognitive_complexity.checker import CognitiveComplexityChecker def run_validator_for_test_file( filename: str, max_cognitive_complexity: int = None, ignore_django_orm_queries: bool = True, ): test_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'test_files', filename, ) with open(test_file_path, 'r') as file_handler: raw_content = file_handler.read() tree = ast.parse(raw_content) checker = CognitiveComplexityChecker(tree=tree, filename=filename) if max_cognitive_complexity: checker.max_cognitive_complexity = max_cognitive_complexity return list(checker.run()) Melevir-flake8-cognitive-complexity-a17a433/tests/test_complexity.py000066400000000000000000000002761371120116100257400ustar00rootroot00000000000000from conftest import run_validator_for_test_file def test_fails(): errors = run_validator_for_test_file('complex_functions.py', max_cognitive_complexity=3) assert len(errors) == 1 Melevir-flake8-cognitive-complexity-a17a433/tests/test_files/000077500000000000000000000000001371120116100242665ustar00rootroot00000000000000Melevir-flake8-cognitive-complexity-a17a433/tests/test_files/complex_functions.py000066400000000000000000000001351371120116100303760ustar00rootroot00000000000000def f(a, b): if a: for i in range(b): if b: return 1