Formatter for pyproject.toml files
PyPI Version LICENSE Python versions Supported platforms Pipeline status pre-commit.ci status codecov
This package enforces consistent formatting of pyproject.toml files, reducing merge request conflicts and saving time otherwise spent on manual formatting. It also contributes to a cleaner git history and more readable code; enhancing overall project organisation and maintainability. Experience a streamlined workflow, reduced errors, and improved code readability with pyprojectsort.
- Alphanumerically sorts pyproject.toml by:
- section
- section key
- list value
- Reformats pyproject.toml to a standardised style
- line per list value
- double quotations
- trailing commas
- indentation
- end of file newline
pyprojectsort is available via PyPI:
pip install pyprojectsortUsing pyprojectsort with pre-commit
To use as an automated git hook, add this to your .pre-commit-config.yaml:
- repo: https://github.com/kieran-ryan/pyprojectsort rev: v0.4.0 hooks: - id: pyprojectsort
With the following pyproject.toml:
[tool.ruff] ignore = ["G004", "T201", "ANN" ] [project] name = 'pyprojectsort' authors = [ { name = "Kieran Ryan" }, "Author Name <author@email.com>", {name="Author Name"} ] [tool.radon] show_mi = true exclude = "tests/*,venv/*" total_average = true show_complexity = true [build-system] build-backend = "flit.buildapi" requires = ["flit"]
Run the package from within its directory:
pyprojectsortThe configuration will be reformatted as follows:
[build-system] build-backend = "flit.buildapi" requires = [ "flit", ] [project] authors = [ "Author Name <author@email.com>", { name = "Author Name" }, { name = "Kieran Ryan" }, ] name = "pyprojectsort" [tool.radon] exclude = "tests/*,venv/*" show_complexity = true show_mi = true total_average = true [tool.ruff] ignore = [ "ANN", "G004", "T201", ]
The pyproject file path can alternatively be specified:
pyprojectsort ../pyproject.tomlThe --check option can be used to determine whether your file would be reformatted.
pyprojectsort --checkIf the file needs reformatting, the program exits with an error code. This is useful for pipeline integration as it prevents writing back changes so that a clean repository is maintained for subsequent jobs.
The --diff option provides similar functionality but also displays any changes that would be made.
pyprojectsort --diffThe diff of an alphabetically reordered array will appear as follows:
@@ -6,8 +6,8 @@ [project] authors = [ + { name = "Author Name" }, { name = "Kieran Ryan" }, - { name = "Author Name" }, ]
Contributions are welcome for pyprojectsort, and can be made by raising issues or pull requests.
Using uv for package and project management is encouraged when developing with the project - though not required. You will typically want to use the below commands within the project during development.
| Command | Purpose |
|---|---|
| uv run pytest | π§ͺ Run the tests |
| uv run pre-commit | π Run the linting checks on staged changes |
| uv run pre-commit install | π΅οΈββοΈ Run the linting checks on commit |
| uv run mkdocs serve | π Build the documentation |
| uv build | π¦ Build the package |
pyprojectsort is licensed under the MIT License.