Skip to content

Pylint Usage Guide

Maksym Derkach edited this page Dec 13, 2024 · 1 revision

Pylint Usage Guide

Introduction

Pylint is a static code analysis tool for Python that helps maintain code quality. This guide explains how to run Pylint locally and interpret the results.

Running Pylint Locally

  1. Open the terminal in the root directory of your Django project.

  2. Run Pylint on your application folder (replace your_django_app_folder):

    pylint --load-plugins pylint_django your_django_app_folder
  3. View the output: Pylint will display issues, including errors, warnings, and suggestions.

Interpreting Pylint Results

Message Types

  • Error (E): Critical issues, e.g., syntax errors.
  • Warning (W): Potential issues, e.g., code style violations.
  • Refactor (R): Suggestions for simplifying code.
  • Convention (C): Naming or formatting issues.

Common Issues

  • Missing Docstrings: Add docstrings to functions/classes.
  • No Member: Warning for dynamic attributes (common in Django). Can be ignored.
  • Invalid Naming: Ensure variable names follow conventions (e.g., snake_case for variables).

Example Output

************* Module forum.views
forum/views.py:5:0: C0301: Line too long (130/120)
forum/views.py:10:0: W0201: Attribute 'db_field' defined outside __init__
forum/views.py:15:0: C0103: Variable name "foo" doesn't conform to snake_case

Fixing Issues

  • Refactor Code: Simplify functions or methods.
  • Follow Naming Conventions: Use snake_case for variables/functions, PascalCase for classes.
  • Add Docstrings: Document functions/classes.
  • Ignore False Positives: Modify .pylintrc to disable specific warnings (e.g., dynamic attributes).

Disable Warnings in .pylintrc

[MESSAGES CONTROL]
disable=missing-docstring,no-member,invalid-name

Clone this wiki locally