Skip to content

Latest commit

 

History

History
110 lines (85 loc) · 4.13 KB

File metadata and controls

110 lines (85 loc) · 4.13 KB

Python Formatting and Linting

TL;DR: Use Black for code formatting, isort for import sorting, and Flake8 for linting. This combination ensures consistent, readable code across all Python projects.

Overview

Different developers have different coding habits -- variable naming, indentation style, import ordering, and more. Python's PEP 8 standard defines a comprehensive set of rules for consistent code styling. You do not need to memorize PEP 8; tooling handles enforcement automatically.

Recommended Toolchain

Tool Purpose Customizability
Black PEP 8-compliant code formatter Minimal (by design -- ensures all code looks the same)
isort Import sorter (stdlib, third-party, application) Moderate (supports Black-compatible formatting)
Flake8 Linter for PEP 8 compliance and code quality High
YAPF PEP 8-compliant formatter by Google Very high (multiple base styles)
autopep8 PEP 8 auto-fixer Moderate

Why Black?

Black's strict, opinionated formatting means all code formatted by Black looks identical. This consistency reduces cognitive overhead during code reviews and makes unfamiliar codebases immediately readable. Black is also trivial to set up -- there are no configuration knobs to tune.

YAPF (Google-style formatting):

records = get_records(args.key, feed, args.output, args.range,
                      args.field, args.extra, args.expiration)

Black formatting:

records = get_records(
    args.key,
    feed,
    args.output,
    args.range,
    args.field,
    args.extra,
    args.expiration,
)

Import Ordering: isort

isort organizes imports into three sections (standard library, third-party, and application-specific), sorts them lexicographically and case-insensitively, and supports Black-compatible formatting:

import argparse
import math
import multiprocessing
from typing import Type, Union

import cv2
import ffmpeg
from loguru import logger
from rich.progress import (
    BarColumn,
    Progress,
    ProgressColumn,
)
from rich.text import Text

from . import __version__
from .decoder import VideoDecoder
from .encoder import VideoEncoder

Editor Configuration

Visual Studio Code

Recommended extensions and settings:

{
    "python.formatting.provider": "black",
    "python.languageServer": "Pylance",
    "python.linting.flake8Enabled": true,
    "python.analysis.typeCheckingMode": "strict"
}

NeoVim

Recommended plugins:

ALE configuration for Black:

let g:ale_fixers = {
\ '*': ['trim_whitespace', 'prettier'],
\ 'python': ['black']
\}
let g:ale_completion_enabled = 1

References