Python Standard
Python Standard
Non-Negotiables
- PEP 8 + Black define formatting. No exceptions.
- Type hints are not optional for new/changed code.
- Explicit is better than implicit. Avoid cleverness.
- 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
typingconsistently; new/changed functions must be annotated. - Run a type checker (e.g. mypy or pyright) in CI.
- Avoid
Anyunless 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
- PEP 8: https://peps.python.org/pep-0008/
- PEP 484 (type hints): https://peps.python.org/pep-0484/
- Black: https://black.readthedocs.io/en/stable/
- Ruff: https://docs.astral.sh/ruff/
- pytest: https://docs.pytest.org/