Python: Use pylint to make better code!

pylint is a Python linter which checks your code for readability and consistency! its an awesome tool that can enhance your code! lets see…

Python: Use pylint to make better code!
Photo by Jonas Jaeken on Unsplash

pylint is a Python linter which checks your code for readability and consistency! its an awesome tool that can enhance your code! lets see how it works

Installing pylint

To install pylint we can use the pip3 commnd as usual

$ pip3 install pylint

How to use pylint

To use pylint is very easy, its basic syntax is

$ pylint <python_file.py>

Examples

Save the following file as example.py

def add_numbers(a, b): 
    c = 0 
    return a + b   
 
if __name__ == '__main__': 
    d = 0 
    result = add_numbers(10, "20")  # Intentional type mismatch 
    print(result)

Now lets run example.py against pylint, it produces the following output

pylint example.py 
************* Module example 
example.py:3:16: C0303: Trailing whitespace (trailing-whitespace) 
example.py:1:0: C0114: Missing module docstring (missing-module-docstring) 
example.py:1:0: C0116: Missing function or method docstring (missing-function-docstring) 
example.py:2:4: W0612: Unused variable 'c' (unused-variable) 
example.py:6:4: C0103: Constant name "d" doesn't conform to UPPER_CASE naming style (invalid-name) 
 
------------------------------------------------------------------ 
Your code has been rated at 2.86/10 (previous run: 2.86/10, +0.00)

We can see that pylint detected some issues we can fix!, the Trailing whitespace error is because i have insterted whitespaces after return a +b , After fixing this issue and re-ruing pylint we can see that the code rating has been increased from 2.86 to 4.29

% pylint example.py 
************* Module example 
example.py:1:0: C0114: Missing module docstring (missing-module-docstring) 
example.py:1:0: C0116: Missing function or method docstring (missing-function-docstring) 
example.py:2:4: W0612: Unused variable 'c' (unused-variable) 
example.py:6:4: C0103: Constant name "d" doesn't conform to UPPER_CASE naming style (invalid-name) 
 
------------------------------------------------------------------ 
Your code has been rated at 4.29/10 (previous run: 2.86/10, +1.43)

Lets try to fix the rest of the issues, modify code to this

""" Example Code """ 
 
def add_numbers(a, b): 
    """ Add Numbers """ 
    return a + b 
 
if __name__ == '__main__': 
    D = 0 
    result = add_numbers(10, "20")  # Intentional type mismatch 
    print(result)

Now re-run pylint

(venv) kpatronas@192 python % pylint example.py 
 
------------------------------------------------------------------- 
Your code has been rated at 10.00/10 (previous run: 4.29/10, +5.71)

We can see that issues disappeared! but here are some “traps” i have intentionally made

  • D : did not issued this as an non used variable
  • add_numbers(10,”20"): Here is an obvious error since the function is called add_numbers.. but pylint did not raise this as an issue and its completely right! pyling is a linter not a testing tool.. it cannot guess by the name if what you try to do is intented or not.. so.. always test :)

Conclusion

pylint is a powerful tool that can make your code better! use it as possible you can, its very simple and will make your code look professional.

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go: