~/python/python-pylint-errors-solution.md

Understanding and Solving pylint errors for Python 3

3 min readDecember 02, 2018by Gorav Singal#python#python-3#pylint
// TL;DR

Fix common pylint errors like missing docstrings (C0111), too many arguments (R0913), and broad exceptions (W0703) by following Python best practices and using pylint disable comments where appropriate.

Understanding and Solving pylint errors for Python 3

Pylint is an excellent tool to have good quality code. This post is not about how pylint works. Lets see most common pylint errors:

pylint C0111:Missing module docstring

When you are defining methods in python, its necessary to give them some text help what the function is all about. Example:

def say_hello():
  print("Hello")

Running pylint on this method will give you this error. To solve this:

def say_hello():
  """
  This function prints hello
  """
  print("Hello")

pylint: Method could be a function (no-self-use)

This error happens in class methods. You have a class and some methods, but in this method you are not using ‘self’ object which is usually a reference to the current instance of the class.

class Hello:
    def hey(self):
        print('hey')

In above method, ‘self’ is unused. And, you will get this error: pylint: Method could be a function (no-self-use)

There are two solutions to this:

  • Either use ‘self’ in this method. May be a logging object or something.
  • Remove ‘self’. But, then you have to make some more changes to method to something like below:
class Hello:
    @classmethod
    def hey(cls):
        print('hey')

Unable to import for custom module

You have some custom modules and trying to run pylint. Pylint complains about unknown modules. What to do? To solve this, you need to edit your .pylintrc file,

  • Open .pylintrc file

  • Look for [MASTER] section

  • In init-hook, append your module paths as:

    init-hook='import sys; sys.path.append('./modules/hardening_base');sys.path.append('./modules/script_collector');

    Where, modules/hardening_base is path of one of my module from root folder of project.

Now run pylint, and error is gone.

pylint: Constant name doesn’t conform to UPPER_CASE naming style

In your main file, you are probably initializing an object without a function. Define a method.

def my_method():
    print('hello')

if __name__ == "__main__":
    my_method()

Do not use len(args) to determine if a sequence is empty

args is an array and I want to put an if condition to check if length is greater than 0.

if len(args) > 0:
    # do something

In python 3, this can be re-written as:

if args:
    # do something

Line too long (147/100) (line-too-long)

Your source code line is this much character long. And, recommended configuration says you should have max of 100 character long line. There are two solution:

  1. Break your line to multiple lines
arr = [a, b, c]

to

arr = [
    a, 
    b, 
    c
]

Break string to multiple lines

a = 'abc abc abc abc'

to

a = 'abc abc '\
  'abc abc'

Break function call into multiple lines

a.call(a, b, c, d)

to

a.call(
    a,
    b,
    c,
    d)
  1. Change your .pylintrc (Not recommended way)

Edit your .pylintrc file: look for [FORMAT] section

See setting:

max-line-length=100

change it to some greater value. But, this is not recommended way. You should break your lines into multiple lines.

There is one setting which can disable certain checks that pylint runs. You can disable few checks as per your need.

Edit your .pylintrc file: Look for [MESSAGES CONTROL]

See disable setting

disable=print-statement,
        parameter-unpacking,
        unpacking-in-except,
        old-raise-syntax,
        backtick,

You can include the pylint error here, which mentions name of check, and run pylint again.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Python 3 - Magical Methods and Tricks with extremely useful methods

Python 3 - Magical Methods and Tricks with extremely useful methods

This post will show some really awesome tricks in python. Get the power of a…

Python 3 - Fun with Python String

Python 3 - Fun with Python String

This post some useful tips of using strings, and some issues while dealing with…

Python 3 - Format String fun

Python 3 - Format String fun

This post is dedicated for cases where we intend to append a variable value in a…

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

Introduction It is very important to introduce few process so that your code and…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…