Detect and fix missing source-exclude patterns in Python projects built with uv_build.
py-lean is a Python counterpart to cargo-lean. It checks the source-distribution configuration in
pyproject.toml so repository-only files don't unnecessarily end up in published archives.
A Python package's source repository often contains more than consumers need to install and use it. Tests, CI workflows, development configuration, and other repository-specific files can add unnecessary noise and size to source distributions.
py-lean helps make distribution contents intentional by identifying common repository artefacts that are missing from
tool.uv.build-backend.source-exclude. It can also add the missing patterns to pyproject.toml.
Your working repository remains intact: py-lean configures what is excluded from the built source distribution; it does
not delete those files from your project.
- Lint — Detect common repository artefacts that are not listed in
source-exclude. - Create — Add a
source-excludeconfiguration for detected artefacts. - Update — Add missing exclusion patterns to an existing configuration.
- Preview changes — Use
--dry-runand--diffto review proposed edits before applying them. - Machine-readable output — Emit lint results as JSON.
- Python 3.11 or newer
- uv and a project using the
uv_buildbuild backend
py-lean manages tool.uv.build-backend.source-exclude, which is a uv_build configuration. The target project must
therefore use uv_build as its build backend for this setting to apply.
For example, the target project's pyproject.toml should include:
[build-system]
requires = ["uv_build"]
build-backend = "uv_build"If the project uses a different build backend, py-lean's source-exclude configuration will not control that backend's
source-distribution contents.
Install with uv:
uv tool install py-lean-packageNote
The PyPI package is named py-lean-package due to PyPI naming restrictions that prevent publishing packages with
names too similar to existing ones. The CLI command remains py-lean.
Or install from source:
git clone https://github.com/raphaelstolt/py-lean.git
cd py-lean
uv sync
uv run --project . py-lean --helpCheck for missing patterns in source-exclude:
py-lean lintOptions:
| Flag | Description |
|---|---|
-m, --manifest-path <PATH> |
Path to pyproject.toml (default: pyproject.toml) |
--format <text|json> |
Output format (default: text) |
-p, --patterns <PATTERNS> |
Space-separated artefact patterns to check, e.g., "tests/ benchmarks/" |
--dry-run |
Show what would be done without making changes |
--diff |
Show a diff of the proposed changes |
Examples:
# Lint with JSON output
py-lean lint --format json
# Use a custom manifest path
py-lean lint -m path/to/pyproject.toml
# Check selected artefact patterns
py-lean lint -p "tests/ benchmarks/"Create a tool.uv.build-backend.source-exclude configuration containing common repository artefacts:
py-lean createThis scans the project for common artefacts, such as tests/, docs/, and .git/, and adds the detected patterns to
pyproject.toml.
Options:
| Flag | Description |
|---|---|
--dry-run |
Preview changes without modifying files |
--diff |
Show a unified diff of the proposed changes |
Add missing patterns to an existing source-exclude configuration:
py-lean updateOptions:
| Flag | Description |
|---|---|
--dry-run |
Preview changes without modifying files |
--diff |
Show a unified diff of the proposed changes |
Examples:
# Preview changes
py-lean update --dry-run --diff
# Apply changes
py-lean updatepy-lean determines which artefact patterns to check using the following precedence:
- CLI
--patterns— highest priority; replaces other pattern sources. tool.uv.build-backend.source-excludeinpyproject.toml— patterns already configured for the build backend.- Built-in defaults — used when neither of the above is configured.
Use --patterns to override the default patterns:
py-lean lint -p "tests/ benchmarks/"By default, py-lean checks for these 20 patterns:
tests/, docs/, dist/, build/, htmlcov/,
.venv/, __pycache__/, .tox/, .mypy_cache/, .pytest_cache/, .github/,
.idea/, .vscode/, .vs/, .ruff_cache/,
*.egg-info, .DS_Store, *.pyc, *.pyo, .coverage
Note: .git is not included as it is automatically excluded by build backends.
Override them with --patterns:
py-lean lint -p ".idea/ __pycache__/"Add py-lean to your CI pipeline to ensure that source-exclude patterns are kept up to date:
- name: Install py-lean
run: uv tool install py-lean-package
- name: Check source-exclude patterns
run: py-lean lintFailing the build when patterns are missing prevents repository artefacts from ending up in published distributions.
This package is developed with the assistance of Artificial Intelligence (AI). AI tools are used throughout the development process, including code generation, documentation, testing, and maintenance.
While reasonable care is taken to review and validate AI-generated contributions, AI-generated code is not inherently correct, secure, or free of defects. All contributions remain subject to human review and project quality standards.
As with any software, use this package at your own risk. No guarantee is made regarding its fitness for a particular purpose, and the maintainers assume no liability for damages resulting from its use.
See CONTRIBUTING.md for development instructions.
This project is licensed under the MIT License. See LICENSE.md for details.
- lean-package-validator — The
PHPequivalent - cargo-lean — The
Rustequivalent.