Skip to Content
Course content

9: Using a Linter: flake8 and pylint

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you've just finished writing a first draft of a novel. You're proud of the plot, and the story works, but if you sent it straight to a publisher, they'd send it back covered in red ink. Not because the story is bad, but because you have inconsistent capitalization, weird spacing between paragraphs, and maybe a few characters who appear in chapter one and then vanish forever without explanation. An editor doesn't rewrite your story for you; they just point out the technical sloppiness that makes the story harder to read.

In Python, a linter is that editor with the red pen. It doesn't run your code to see if it works (that's what testing is for); instead, it scans your source code for "smells"—patterns that are technically legal in Python but are considered bad practice, confusing to other developers, or likely to cause bugs down the road.

Flake8: The Fast Style Guard

I usually start my projects with Flake8. Think of it as a lightweight spell-checker. It's incredibly fast and focuses primarily on PEP 8, which is the official style guide for Python. It's mostly concerned with how your code looks.

For instance, look at this snippet:

def calculate_area(radius):
    import math
    return math.pi*radius**2

If you ran Flake8 on this, it would probably yell at you for a few things: the import is inside the function instead of at the top of the file, and there's a lack of whitespace around the multiplication operator. It's not "broken"—the math is correct—but it's "noisy." Flake8 helps you strip away that noise so that when another engineer looks at your code, they aren't distracted by weird formatting.

Pylint: The Deep-Dive Critic

Now, Pylint is a different beast entirely. If Flake8 is a spell-checker, Pylint is a full developmental editor. It doesn't just care about spaces; it cares about the logic and the architecture. Pylint actually analyzes the flow of your code.

Consider this scenario:

def process_data(items):
    result = []
    for item in items:
        temp_var = item * 2 # I forgot to use this later
        result.append(item)
    return result

Flake8 might see this and say, "Looks great!" But Pylint will flag temp_var as an unused variable. It'll ask you: "Why is this here? Are you forgetting to do something, or is this just leftover junk?" It will also nag you about missing docstrings or functions that are becoming too complex (too many nested loops). I'll be honest: Pylint can feel like a micromanager at first, but it catches the kind of subtle bugs that usually take three hours to find during a midnight debugging session.

Taming the Noise

Here is a bit of professional advice: don't let the linters paralyze you. If you're in the "flow state" and just trying to prove a concept, turn them off or ignore them. Once you're ready to commit your code to a repository or share it with a teammate, that's when you bring in the red pen.

You'll also find that you don't always agree with the linter. There are times when a specific "violation" is actually the most readable way to write a line. In those cases, you can use comments like # noqa (for Flake8) or # pylint: disable=variable-name to tell the tool, "I know what I'm doing, leave this line alone." Use this sparingly, though—otherwise, you're just hiding the mess under the rug.




📋 Practical Task

Cleaning up the Messy Inventory Script

Below is a piece of code that functions perfectly—it calculates the total value of a warehouse inventory. However, it is a nightmare to read and would fail almost any professional code review. Your task is to use a linter (or your knowledge of PEP 8 and Pylint's logic) to fix the following:

  • Move all imports to the top of the file.
  • Fix the naming conventions (Python uses snake_case for functions and variables).
  • Correct the whitespace around operators and after commas.
  • Remove the unused variable.
  • Ensure there are two blank lines before the function definition.
import math
def GetInventoryValue(items,tax_rate):
    total=0
    for i in items:
        temp_calculation = i['price'] * i['quantity'] # This variable is never used
        total += i['price']*i['quantity']
    return total * (1+tax_rate)

# Data for testing
my_items = [{'name': 'Laptop', 'price': 1000, 'quantity': 5}, {'name': 'Mouse', 'price': 25, 'quantity': 10}]
print(GetInventoryValue(my_items,0.07))
Rating
0 0

There are no comments for now.

to be the first to leave a comment.