Code review is importance in the software development process as it serves multiple critical purposes. It helps the programmers to identify and rectify bugs, logical errors, and vulnerabilities before they manifest in the production environment. It is a proactive approach to software development that improves code reliability and enhances the overall quality of the end product. One useful tool for ensuring coding standards and improving code quality is Pylint.
Pylint is a popular
Pylint requires the filename of the Python code we wish to review as input. This input is provided using the following command:
pylint filename.py
To enforce .pylintrc
configuration file. The --rcfile
flag tells Pylint to use the settings in the specified .pylintrc
file (filename.pylintrc
) instead of the default settings.
pylint --rcfile=filename.pylintrc filename.py
The structure of the configuration file is organized under the [MESSAGES CONTROL]
section.
[MESSAGES CONTROL]disable = W0311
[MESSAGES CONTROL]
is used to control which types of messages Pylint should enable or disable. These MESSAGES
categorize the issues into 6 main categories:
Information (I). Displays informational messages.
Convention (C). Displays when the code does not follow standard conventions.
Warning (W). Displays when the code has Python specific problems.
Refactor (R). Displays when bad
Error (E). Displays when bugs are present in the code.
Fatal (F). Displays when errors, preventing Pylint from further processing, are present.
The CONTROL
allows us to customize how to handle these message categories. This involves enabling or disabling specific MESSAGES
as required.
disable = W0311
disables a specific Pylint warning. Here, W0311
corresponds to the warning about "bad indentation," meaning that Pylint will not report issues related to inconsistent or incorrect indentation in the code.
Here's the coding example to use Pylint to review code.
import subprocess# Function to perform code analysis using pylintdef code_analysis(file_path):try:result = subprocess.run(['pylint', '--rcfile', 'config.pylintrc', file_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)analysis_result = result.stdoutexcept Error:analysis_result = f"Some error has occurred {Error}"return analysis_resultdef main():print("Welcome to the AI-Powered Code Review Assistant!")file_path_bad = 'bad_code.py'file_path_good = 'good_code.py'code_bad = open(file_path_bad).readlines()code_good = open(file_path_good).readlines()analysis_result_bad = code_analysis(file_path_bad)analysis_result_good = code_analysis(file_path_good)analysis_code = code_analysis('wrong_file.py')print("Analysis of bad_Code.py",analysis_result_bad)print('Analysis of non_existent file is ',analysis_code)print("Analysis of good_code.py",analysis_result_good)main()
Explanation of main.py
Line 1: Imports the subprocess
module to interact with the system's shell or command-line interface from within a Python script.
Lines 4–5: Defines a function code_analysis
that takes a file_path
as its parameter and uses try/except
block for unexpected errors.
Line 6: subprocess.run()
function executes the pylint
command with provided options and arguments, capturing the standard output and standard error as text and storing the result in the result
variable.
Line 8: Assigns the standard output from the result
object containing the output of the executed command to the variable analysis_result
.
Line 10–11: Raises an exception and prints the error message.
Line 13: Returns the analysis_result
.
Line 16–18: Defines a function main
and prints a welcome message.
Line 20–21: Assign the names of the Python files that we intend to review to the variables file_path_bad
and file_path_good
.
Line 23–24: Read the contents of two Python files and storing their contents as lists of lines in the variables code_bad
and code_good
.
Line 26–28: Calls code_analysis
function on three different Python files: file_path_bad
, file_path_good
, and 'wrong_file.py'
.
Line 30–33: Prints the results after analyzing all the files.
Explanation of config.pylintrc
Line 1: Represents the section headers.
Line 2: The msg_id
we want to disable. This configuration disables the bad indentation found in our code.
The code we want to review is written in the files bad_code.py
and good_code.py
.
After analyzing the Python code, Pylint generates the output in the following format:
Analysis of file_path.py{file_path}:{line_no}:{column_no}: {message_id}: {message} ({symbol})
{file_path}:{line_no}:{column_no}
. The specific position within the file during a code analysis process.
{message_id}
. The id
or code associated with a specific message. The id
tells about the category of the message.
{message}
. The content of the message. The message text provides detailed information and context about the message.
{symbol}
. The symbolic name of the message.
Pylint provides a global evaluation for the code's overall quality, represented on a scale of 0 to 10. A higher score indicates a higher level of the code quality. This score is displayed in all categories except in Fatal message category. The evaluation score is presented for all message categories, with the exception of the Fatal category, which halts Pylint's further processing due to critical errors.
Your code has been rated at 10.00/10
The output of bad_code.py
, wrong_file.py
and good_code.py
file is as follows:
Analysis of bad_code.py ************* Module bad_codebad_code.py:12:0: C0303: Trailing whitespace (trailing-whitespace)bad_code.py:1:0: C0114: Missing module docstring (missing-module-docstring)bad_code.py:1:0: C0103: Module name "bad_code" doesn't conform to snake_case naming style (invalid-name)bad_code.py:3:4: E0213: Method 'print' should have "self" as first argument (no-self-argument)bad_code.py:1:0: R0903: Too few public methods (1/2) (too-few-public-methods)bad_code.py:7:0: C0116: Missing function or method docstring (missing-function-docstring)bad_code.py:7:0: C0103: Function name "my_Function" doesn't conform to snake_case naming style (invalid-name)bad_code.py:8:12: E0602: Undefined variable 'i' (undefined-variable)bad_code.py:9:14: E0602: Undefined variable 'i' (undefined-variable)bad_code.py:10:0: C0116: Missing function or method docstring (missing-function-docstring)bad_code.py:10:8: W0613: Unused argument 'fruit_name' (unused-argument)-----------------------------------Your code has been rated at 0.00/10
The bad_code.py
file has received a rating of 0.00 due to numerous issues identified within the code that need to be addressed to improve its quality and adherence to coding standards. After resolving all these issues in the good_code.py
file, it has achieved a perfect rating of 10.00. The wrong_file.py
creates a fatal message because the specified file does not exist.
Free Resources