Tools III: Linters

Overview

Teaching: 10 min
Exercises: 5 min
Questions
  • How to make the editor pro-actively find errors and code-smells

Objectives
  • Remember how to install and use a linter in vscode without sweat

What is linting?

Linters enforce style rules on your code such as:

Consistent styles make a code more consistent an easier to read, whether or not you agree with the style. Using an automated linter avoids bike-shedding since the linter is the final arbiter.

Why does linting matter?

Rules for choosing linters

  1. Choose a few
  2. Stick with them

We chose:

Exercise

Setup VS Code:

  1. Open Settings (see previous exercise if you’re not sure how):
    • Search for “linting enable” and check the box for “Python > Linting: Enabled”.
    • Search for “pylint enabled” and check the box for “Python > Linting: Pylint Enabled” (you may need to scroll down for this one).
    • Search for “pylint use” and uncheck the box for “Python > Linting: Pylint Use Minimal Checkers”.
    • Search for “flake8 enabled” and check the box for “Python > Linting: Flake8 Enabled”.
    • Search for “mypy enable” and check the box for “Python > Linting: Mypy Enabled”.
  2. Create a file unlinted.py with the following code and save it:

    from typing import List
    
    
    class printer:
        pass
    
    
    def ActionatePrinters(printers: List[printer]):
        # pylint: disable=missing-docstring
        printing_actions = []
        for p in printers:
    
            if p == None:
                continue
    
            def action():
                print(p)
    
            printing_actions.append(action)
    
            p = "something"
            print(p)
    
        for action in printing_actions:
            action()
    
    
    ActionatePrinters([1, 2, 2])
    
  3. Check the current errors (click on errors in status bar at the bottom)
  4. Try and correct them
  5. Alternatively, try and disable them (but remember: with great power…). We’ve already disabled-one at the function scope level. Check what happens if you move it to the top of the file at the module level.

Key Points

  • Linting is about discovering errors and code-smells before running the code

  • It shortcuts the “edit-run-debug and repeat” workflow

  • Almost all editors and IDEs have some means to setup automatic linting

  • 5 minutes to setup a linter is redeemed across the time of the project i.e. the cost is close to nothing