Skip to content

Latest commit

 

History

History
269 lines (166 loc) · 4.77 KB

2.6.md

File metadata and controls

269 lines (166 loc) · 4.77 KB

Linters i formatery / Linters and Formatters


Why it matters / Dlaczego to ważne

Automated tools catch errors and style issues you'd miss manually.

Automatyczne narzędzia wychwytują błędy i problemy stylistyczne które przegapisz ręcznie.

They enforce consistency across team projects — everyone's code looks the same.

Wymuszają spójność w projektach zespołowych — kod każdego wygląda tak samo.

There's debate here — some want formatting control, others prefer automated consistency.

Jest tu debata — niektórzy chcą kontroli nad formatowaniem, inni preferują automatyczną spójność.

In practice, consistency helps more than personal style preferences.

W praktyce, spójność pomaga bardziej niż osobiste preferencje stylu.

Documentation:

Ruff Documentation


Ruff — all-in-one tool / Ruff — narzędzie wszystko-w-jednym

Ruff is extremely fast linter and formatter written in Rust.

Ruff to ekstremalnie szybki linter i formater napisany w Rust.

It replaces Black, Flake8, isort and more — all in one.

Zastępuje Black, Flake8, isort i więcej — wszystko w jednym.

Install:

Zainstaluj:

uv add --dev ruff

Using Ruff as linter / Używanie Ruff jako linter

Create messy_code.py with issues:

Utwórz messy_code.py z problemami:

import os
import sys

def calculate(x,y):
    result=x+y
    return result

unused_variable = 42

Run linter:

Uruchom linter:

uv run ruff check messy_code.py

Fix automatically:

Napraw automatycznie:

uv run ruff check --fix messy_code.py

Unused imports are gone!

Nieużywane importy zniknęły!


Using Ruff as formatter / Używanie Ruff jako formater

uv run ruff format messy_code.py

Now spacing is fixed:

Teraz odstępy są naprawione:

def calculate(x, y):
    result = x + y
    return result


unused_variable = 42

Recommended configuration / Zalecana konfiguracja

Create pyproject.toml with production-ready settings:

Utwórz pyproject.toml z ustawieniami produkcyjnymi:

[tool.ruff]
line-length = 120
target-version = "py313"

[tool.ruff.lint]
select = [
    "E",     # pycodestyle errors
    "W",     # pycodestyle warnings
    "F",     # pyflakes
    "I",     # isort (import sorting)
    "B",     # flake8-bugbear (common bugs)
    "UP",    # pyupgrade (modern Python syntax)
    "S",     # flake8-bandit (security issues)
    "C4",    # flake8-comprehensions
    "SIM",   # flake8-simplify
]

[tool.ruff.lint.per-file-ignores]
"tests/**/*.py" = ["S101"]  # Allow assert in tests

[tool.ruff.format]
quote-style = "double"
indent-style = "space"

[tool.mypy]
python_version = "3.13"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true

Run checks:

Uruchom sprawdzenia:

uv run ruff check .
uv run ruff format .
uv run mypy .

What this catches / Co to wyłapuje:

  • S (bandit) - hardcoded passwords, SQL injection, unsafe yaml.load()
  • B (bugbear) - mutable default arguments, unused loop variables
  • UP (pyupgrade) - outdated syntax like List[str] instead of list[str]
  • SIM (simplify) - overcomplicated code that can be simplified

mypy — type checker / mypy — sprawdzacz typów

mypy verifies type hints.

mypy weryfikuje type hints.

Install:

Zainstaluj:

uv add --dev mypy

Create type_demo.py:

Utwórz type_demo.py:

def add(a: int, b: int) -> int:
    return a + b

result = add(5, 3)
wrong = add("hello", "world")  # Type error!

Run:

Uruchom:

uv run mypy type_demo.py

Output:

Wynik:

type_demo.py:5: error: Argument 1 to "add" has incompatible type "str"; expected "int"

mypy caught the error!

mypy złapał błąd!


IDE integration / Integracja z IDE

VS Code:

Install Ruff extension (charliermarsh.ruff).

Zainstaluj rozszerzenie Ruff (charliermarsh.ruff).

Add to .vscode/settings.json:

Dodaj do .vscode/settings.json:

{
    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.formatOnSave": true
    }
}

PyCharm:

Settings → Tools → File Watchers → Add Ruff


Podsumowanie / Summary

After this section you can:

Po tej sekcji potrafisz:

  • use Ruff to lint and format code

    używać Ruff do lintowania i formatowania kodu

  • automatically fix common issues

    automatycznie naprawiać powszechne problemy

  • verify type hints with mypy

    weryfikować type hints z mypy

  • integrate tools into IDE

    integrować narzędzia z IDE

  • maintain code quality consistently

    utrzymywać jakość kodu spójnie


Next: 2.7 — Mini-projekt