Back to directory
sourcery-ai avatar
sourcery-ai / sourcery-analytics

sourcery-analytics

`sourcery-analytics` is a command line tool and library for statically analyzing Python code quality.

18

Stars

0

Forks

2

Watchers

License

Sourcery Analytics

PyPI build, test, and publish docs code style docs


sourcery-analytics is a command line tool and library for statically analyzing Python code quality.

Get started by installing using pip:

pip install sourcery-analytics

This will install sourcery-analytics as a command-line tool.

To identify code quality issues:

sourcery-analytics assess path/to/file.py

Example:

sourcery-analytics assess sourcery_analytics/metrics
sourcery_analytics/metrics/cyclomatic_complexity.py:47: error: working_memory of cyclomatic_complexity is 34 exceeding threshold of 20
Found 1 errors.

To analyze a single Python file, use the analyze subcommand:

sourcery-analytics analyze path/to/file.py

Example:

sourcery-analytics analyze sourcery_analytics/analysis.py
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓
┃ Method                                      ┃ length ┃ cyclomatic_complexity ┃ cognitive_complexity ┃ working_memory ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩
│ sourcery_analytics.analysis.analyze         │      5 │                     1 │                    0 │              8 │
│ sourcery_analytics.analysis.analyze_methods │      4 │                     1 │                    1 │             12 │
└─────────────────────────────────────────────┴────────┴───────────────────────┴──────────────────────┴────────────────┘

Alternatively, import and run analysis using the library:

from sourcery_analytics import analyze_methods
source = """
    def cast_spell(self, spell):
        if self.power < spell.power:
            raise InsufficientPower
        print(f"{self.name} cast {spell.name}!")
"""
analyze_methods(source)
# [{'method_qualname': '.cast_spell', 'method_length': 3, 'method_cyclomatic_complexity': 1, 'method_cognitive_complexity': 1, 'method_working_memory': 6}]

For more, see the docs.

Repoanalysis.com

You can see how hundreds of top projects measure across different code quality metrics and see how your priojects compare at repoanalysis.com

Developed by Sourcery

Sourcery Analytics was originally developed by the team at Sourcery. Sourcery is an automated coding assistant to help Python developers review and improve their code while they work. Sourcery has a built in library of 100+ core rules and you can extend it further to create custom rules for any scenario.

Releases

Sep 26, 2022

Download .zip

Dependency updates including core dependency astroid. What's Changed

fix(deps): update dependency more-itertools to v8.14.0 by @renovate in #24 fix(deps): update dependency pydantic to v1.9.2 by @ren...

Jul 27, 2022

Download .zip

Main Change

files containing syntax errors are now skipped during any analysis, logging a warning

What's Changed

feat: skip syntax errors by @bm424 in #17 ci: tooling and styles by @bm424 in #18 fi...

Jun 15, 2022

Download .zip

Main change

sourcery-analytics assess command added to analyze files and directories compared against configurable thresholds.

All Changes

Added Sourcery & RepoAnalysis.com references by @angrycapt...

Added

Steps for release in the developer guide

Fixed

Specify Compounder type using a protocol to avoid mypy_extensions import