alexmojaki / futurecoder

100% free and interactive Python course for beginners
https://futurecoder.io/
MIT License
1.27k stars 132 forks source link

Ruff fixes #465

Closed cclauss closed 4 months ago

cclauss commented 4 months ago

https://docs.astral.sh/ruff is the modern linter and formatter for Python code.

This pull request applies a few lint rules to help teach good practices to new coders.

% ruff --statistics

42  F405    [ ] `AnimationGroup` may be undefined, or defined from star imports
 6  F401    [*] `core.text.search_ast` imported but unused
 5  F541    [*] f-string without any placeholders
 2  E402    [ ] Module level import not at top of file
 2  F841    [*] Local variable `french` is assigned to but never used
 1  E701    [ ] Multiple statements on one line (colon)
 1  F403    [ ] `from manim import *` used; unable to detect undefined names

% ruff rule F401

unused-import (F401)

Derived from the Pyflakes linter.

Fix is sometimes available.

What it does

Checks for unused imports.

Why is this bad?

Unused imports add a performance overhead at runtime, and risk creating import cycles. They also increase the cognitive load of reading the code.

If an import statement is used to check for the availability or existence of a module, consider using importlib.util.find_spec instead.

If an import statement is used to re-export a symbol as part of a module's public interface, consider using a "redundant" import alias, which instructs Ruff (and other tools) to respect the re-export, and avoid marking it as unused, as in:

from module import member as member

Example

import numpy as np  # unused import

def area(radius):
    return 3.14 * radius**2

Use instead:

def area(radius):
    return 3.14 * radius**2

To check the availability of a module, use importlib.util.find_spec:

from importlib.util import find_spec

if find_spec("numpy") is not None:
    print("numpy is installed")
else:
    print("numpy is not installed")

Options

References