Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 11.6 — Linting and Formatting with Ruff
Python · Example 2 of 2

The bug behind rule B006: a mutable default argument

The first function is what a linter flags. The second uses None and creates a new list per call. This is the fix Ruff's rule suggests.

Ruff is a very fast tool that checks Python code for likely bugs and style problems (ruff check) and rewrites its layout into one consistent style (ruff format). One tool replaces Flake8, isort, Black and several plugins.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a function with a mutable default argument, run it twice to show the bug, then fix it with None.

02

Using the ast approach from the example, write a check that reports every function whose name is not lower case with underscores (for example loadData). Use ast.FunctionDef and str.islower().

03

Write the [tool.ruff.lint] configuration that turns on errors, Pyflakes, import sorting and bugbear, and ignores line length.

Explain it without notes

01

What is the difference between a linter and a formatter?

02

What tools does Ruff replace, and why would a team move to it?

03

What does ruff check --fix change, and what does --unsafe-fixes add?

04

How do you configure Ruff for a project, and how do you silence one warning properly?

05

Why can a linter find a bug without running the program?

The bug behind rule B006: a mutable default argument
Sign in to run this example in your browser.

Expected output

first call:  ['apple']
second call: ['apple', 'banana']
default now: (['apple', 'banana'],)
fixed first:  ['apple']
fixed second: ['banana']