Static Code Analysis in Python involves examining Python source code without executing it to identify potential issues such as bugs, stylistic errors, security vulnerabilities, and adherence to coding standards like PEP 8, ultimately improving code quality and reliability early in the development process.
We will be using Pylint. Pylint is a widely used static code analysis tool in Python that examines code without executing it to identify potential errors, enforce coding standards (primarily PEP 8), detect code smells, suggest refactoring opportunities, and provide detailed reports on code quality, ultimately aiming to improve the reliability and maintainability of Python projects.
The first step, is to intall the pylink package:
pip3 install pylint==2.11.1
We can then run Static Code Analysis on a python program:
pylint sample.py
Check the compliance score of a python program. After pylint outputs the recommended changes, a score will be presented similar to the following:
Your code has been rated at 5.00/10 (previous run: 3.33/10, +1.67)