Code metrics can be produced by static code analysis tools to determine complexity and non-standard practices.
Code metrics allow developers to find problematic codebase areas that may need refactoring. In addition, some metrics such as technical debt assist developers in communicating to non-technical audiences why issues with a system are occurring.
Radon is a tool for obtaining raw metrics on line counts, Cyclomatic Complexity, Halstead metrics and maintainability metrics.
Pylint contains checkers for PEP8 code style compliance, design, exceptions and many other source code analysis tools.
PyFlakes parses source files for errors and reports on them.
Pyntch is a static code analyzer that attempts to detect runtime errors. It does not perform code style checking.
Prospector inspects Python source code files to give data on type and location of classes, methods and other related source information.
Flake8 is a code format style guideline enforcer. Its goal is not to gather metrics but ensure a consistent style in all of your Python programs for maximum readability. The rules for Flask8 are all defined in this list, which rolls up the Flake8 dependencies of pycodestyle, pyflakes and McCabe.
Black is a Python code formatter with strong, uncompromising assumptions about how your code must be formatted.
dlint is a linter for secure coding practices.
pylintdb puts pylint results into a SQLite database for programmatic access and searching. Ned Batchelder coded it and wrote about how he uses the program in this bite-sized command line tools: pylintdb post.
The following tools are ready to use by going to the service, punching in the URL to your site, letting them perform their analysis and then reading the results.
Coveralls shows code coverage from test suites and other metrics to help developers improve the quality of their code.
webhint, formerly Sonarwhal scans your website for accessibility, speed and security. There is both an online version that you can point at an arbitrary URL as well as a command-line version for running yourself.
Codecov hooks into GitHub, BitBucket or GitLab and reports code coverage on your code repositories.
Code metrics are really useful when you have a team working on a project for awhile and want to keep the code quality from degrading. However, you can easily go overboard instrumenting everything and overanalyzing the results. The following resources will introduce code metrics topics to you as well as give perspective when metrics are useful to the point of diminishing returns.
How do Ruby & Python profilers work? explains the difference between sampling and tracing profilers then digs into how they work and their advantages and disadvantages.
Moving Fast With High Code Quality provides Quora's code quality goals and how they handle code reviews with their internal tools.
Lessons from Building Static Analysis Tools at Google is a fantastic in-depth read that explains why workflow integration and adapting to developer feedback are critical for static analysis tools adoption in development environments.
Static Code Analizers for Python is an older article but goes over the basics of what Python static code analyzers do.
Getting Started with Pylint goes over setting up Pylint, generating the .pylintrc file and what's in the configuration.
Linting as Lightweight Defect Detection for Python presents a straightforward code example of how linters can detect certain classes of errors in code that especially in dynamically-typed languages are not caught at compile time. The post then shows how to use Flake8 in your own code reviews.
This /r/Python poll on what linters the community uses provides some input on using PyCharm just for its linting features as well as some other approaches.
How to use Flask8 explains what Flask8 is, its usage and expected output.
Pylint false positives is a walkthrough of issues that Pylint detects in an example project, which ones cannot be fixed and the ones where the tool was incorrect. The author concludes that with all of the false positives that were found the signal to noise ratio was not useful enough to use the tool on a typical project. However, on a brand new project without many dependencies it might be helpful to keep your code in a pristine state before the code base grows beyond the nosiness false positives threshold.
What is Flake8 and why we should use it? covers why using a linting tool like Flake8 can improve the quality of your Python code and how to install and configure it for your environment.
Which Python static analysis tools should I use? covers Pylint, PyFlakes and mypy with a short description of the advantages and disadvantage for each one.
This Stack Overflow question on Python static code analysis tools contains comparison discussions of PyLint, PyChecker and PyFlakes.
Consistent Python code with Black covers how to use Black and add it as a pre-commit hook in Git to ensure consistency in repository updates.
Format Python however you like with Black is a super short introduction on what Black does to keep your code formatting consistent.
Intro to Black – The Uncompromising Python Code Formatter contains some introductory examples for using Black and shows how to use it on your source files.
Python static analysis tools is a quick overview of the features for Black, Pylint, Pyflakes, Mypy, Bandit and Prospector.