PythiloLesson 5 of 15 · progress kept in this browser

Linting with pyflakes

A linter reads your code without running it and reports mistakes that are almost always bugs or clutter: names that were never defined, imports nobody uses, variables assigned and then ignored. It finds them before a test or a user does.

import ast
from pyflakes.checker import Checker

source = (
    "import os\n"
    "def area(r):\n"
    "    unused = 1\n"
    "    return pi * r * r\n"
)
tree = ast.parse(source)
for m in Checker(tree, "demo.py").messages:
    print(m)
Your task

Write lint(source) returning a sorted list of (line, kind) pairs, where kind is the pyflakes message class name such as "UnusedImport". If the source does not parse, return [(line, "SyntaxError")] with the line number from the error.

This lesson uses pyflakes, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.