Python Standard

Python Standard

Non-Negotiables

  1. PEP 8 + Black define formatting. No exceptions.
  2. Type hints are not optional for new/changed code.
  3. Explicit is better than implicit. Avoid cleverness.
  4. Errors must be actionable. No silent failures.

Minimum Requirements (Industry Standard Baseline)

Formatting & Style

  • Format with Black.
  • Lint with Ruff (or equivalent) and keep it clean.
  • Sort imports with isort (or Ruff’s import rules).

Typing

  • Use typing consistently; new/changed functions must be annotated.
  • Run a type checker (e.g. mypy or pyright) in CI.
  • Avoid Any unless there is a documented reason.

Errors

  • Do not use bare except:.
  • Never write except Exception: pass.
  • Raise domain-specific exceptions at service boundaries.
  • Add context when re-raising (raise X(...) from e).

Code Structure

  • No business logic in module-level side effects.
  • Prefer pure functions; minimize hidden state.
  • Dependencies must be explicit (constructor params, function args).

Testing

  • Use pytest.
  • Tests must be deterministic and isolated.
  • Mock boundaries, not internals.

Security

  • Don’t build SQL with string concatenation.
  • Don’t deserialize untrusted data unsafely.
  • Secrets must not appear in logs.

References