15. Tooling, Quality, and Packaging
Centralize configs in pyproject.toml
, enforce quality pre-commit, and build reproducible artifacts.
Question: What is the role of
pyproject.toml
in a modern Python project?
Answer: pyproject.toml
(defined in PEP 518) is a standardized configuration file that centralizes the configuration for build tools, formatters, linters, and type checkers. It replaces the need for multiple separate config files (like setup.py
, requirements.txt
, .black.toml
), creating a single source of truth for project metadata and tooling.
Explanation: Tools like ruff
, black
, mypy
, and pytest
can all be configured within the [tool.*]
sections of pyproject.toml
. This simplifies project setup and ensures consistency.
[tool.ruff]
line-length = 100
target-version = "py311"
[tool.mypy]
python_version = "3.11"
strict = true
Question: How do you enforce code quality pre-commit?
Answer: Use pre-commit
to run ruff
, black
, mypy
, and tests on staged changes.
Explanation: Prevents bad code from entering the repo.
# .pre-commit-config.yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8
hooks: [ { id: ruff }, { id: ruff-format } ]