Why is style checking important?

Style checking is certainly an annoying step of programming, and it often feels like a waste of time, but it’s actually quite useful. Much like checkstyle in CS 124, the point of style checking is to enforce good habits and a baseline level of readability. When you start working in a job, research lab, or even open source projects, having good style is expected without saying. It helps others understand and contribute as well. It even helps you, since style checkers are based on conventions that help programmers avoid mistakes. So if you use good style, it will reduce your chances of bugs as well.

That being said, don’t treat style checkers as the only part of your code style process. There is a lot of good code on the internet that does poorly with style checkers, and a lot of bad code that passes style checkers perfectly. It’s just a good baseline rule of thumb, and as you get more experience you can start disabling rules you don’t agree with. When starting out, try to follow almost all the rules enforced by style checkers.

What is pylint?

Pylint is a tool that checks for errors in Python code. It is useful because it tries to enforce a coding standard. It displays some messages about warnings and errors found in the file and gives your code an overall score.

However, pylint isn’t the gospel, and it may sometimes warn you about things that you did intentionally.

How to use Pylint

  • First, make sure you have Pylint installed. To check if you already have it installed:
    • pip list will give you a list of all of your installed Python libraries.
    • pip show pylint will show the details of your installed pylint package if you already have it installed.

If you do not have it installed, install it by running one of the following, depending on your machine:

  $ pip install pylint
  $ pip3 install pylint

Pylint is meant to be called from the command line. The usage is

  $ pylint [options] modules_or_packages

for example if you want to style check potatoes.py, run

  $ pylint potatoes.py

A module is just a file consisting of python code. Then Pylint outputs a reports of errors like in the example below and gives you a code analysis score.

Example output

  myModule.py:144:0: C0103: Function name "isPalindrome" doesn't conform to snake_case naming style (invalid-name)
  myModule.py:144:0: C0116: Missing function or method docstring (missing-function-docstring)
  myModule.py:157:0: C0116: Missing function or method docstring (missing-function-docstring)
  myModule.py:172:4: W0107: Unnecessary pass statement (unnecessary-pass)
  Your code has been rated at 4.51/10 

The message type can be:

  • [I]nformational messages that Pylint emits (do not contribute to your analysis score)
  • [R]efactor for a “good practice” metric violation
  • [C]onvention for coding standard violation
  • [W]arning for stylistic problems or minor programming issues
  • [E]rror for important programming issues (i.e. probably a bug)
  • [F]atal for errors which prevented further processing
  • Info: Pylint output — Pylint 2.8.3.dev49+g0978e091 documentation

Lastly, fix your errors and rerun Pylint until you get a 10/10.

Using Pylint Example

Example code

  def characterCount(phrase):
    totalCount = 0
    for character in phrase:
      totalCount +=1  #totalCount = totalCount+1

  def main():
    characterCount(input("Enter a phrase to count the characters of: "))



  ************* Module test
  test.py:10:0: C0304: Final newline missing (missing-final-newline)
  test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
  test.py:1:0: C0103: Function name "characterCount" doesn't conform to snake_case naming style (invalid-name)
  test.py:1:0: C0116: Missing function or method docstring (missing-function-docstring)
  test.py:2:4: C0103: Variable name "totalCount" doesn't conform to snake_case naming style (invalid-name)
  test.py:4:8: C0103: Variable name "totalCount" doesn't conform to snake_case naming style (invalid-name)
  test.py:3:8: W0612: Unused variable 'character' (unused-variable)
  test.py:7:0: C0116: Missing function or method docstring (missing-function-docstring)
  Your code has been rated at 0.00/10

The author feels really sad that they have gotten a 0.00/10. Please fix the above code.

  • Save the code above as a file on your computer called pylint_test.py.
  • Tips for Pylint
    • Python uses snake_case (underscores) instead of camelCase (first letter of the first word is lower case, while the first letter of every subsequent word is uppercase).
    • Beware of too long lines and trailing whitespaces
    • Don’t include unused variables

Disabling Pylint:

After we have fixed the style issues from the above code, we should have:

  """This module contains some functions that we want to test."""

  def character_count(phrase):
    """This function counts the number of characters in a phrase"""
    total_count = 0
    for character in phrase:
      total_count +=1  #totalCount = totalCount+1

  def main():
    """This module tests the above modules"""
    character_count(input("Enter a phrase to count the characters of: "))


However, we still get this error message (pertaining to the for loop that we use to count each character in the inputted phrase.)

  test.py:6:8: W0612: Unused variable 'character' (unused-variable)

If you want to disable this error message. Add this line to your python file.

  # pylint: disable=W0612

Or you can disable it while running it. Type this into the command line.

  $ pylint --disable=W0612 test.py


Contributors: Alex, Harsh, Maaheen, Nehal