Automated checks on pyproject.toml
powered by JSON Schema definitions
Important
This project is experimental and under active development. Issue reports and contributions are very welcome.
With the approval of PEP 517 and PEP 518, the Python community shifted towards a strong focus on standardisation for packaging software, which allows more freedom when choosing tools during development and make sure packages created using different technologies can interoperate without the need for custom installation procedures.
This shift became even more clear when PEP 621 was also approved, as a standardised way of specifying project metadata and dependencies.
validate-pyproject
was born in this context, with the mission of validating
pyproject.toml
files, and make sure they are compliant with the standards
and PEPs. Behind the scenes, validate-pyproject
relies on JSON Schema
files, which, in turn, are also a standardised way of checking if a given data
structure complies with a certain specification.
The easiest way of using validate-pyproject
is via CLI.
To get started, you need to install the package, which can be easily done
using pipx
:
$ pipx install 'validate-pyproject[all]'
Now you can use validate-pyproject
as a command line tool:
# in you terminal
$ validate-pyproject --help
$ validate-pyproject path/to/your/pyproject.toml
You can also use validate-pyproject
in your Python scripts or projects:
# in your python code
from validate_pyproject import api, errors
# let's assume that you have access to a `loads` function
# responsible for parsing a string representing the TOML file
# (you can check the `toml` or `tomli` projects for that)
pyproject_as_dict = loads(pyproject_toml_str)
# now we can use validate-pyproject
validator = api.Validator()
try:
validator(pyproject_as_dict)
except errors.ValidationError as ex:
print(f"Invalid Document: {ex.message}")
To do so, don't forget to add it to your virtual environment or specify it as a project or library dependency.
Note
When you install validate-pyproject[all]
, the packages tomli
,
packaging
and trove-classifiers
will be automatically pulled as
dependencies. tomli
is a lightweight parser for TOML, while
packaging
and trove-classifiers
are used to validate aspects of PEP
621.
If you are only interested in using the Python API and wants to keep the
dependencies minimal, you can also install validate-pyproject
(without the [all]
extra dependencies group).
If you don't install trove-classifiers
, validate-pyproject
will
try to download a list of valid classifiers directly from PyPI
(to prevent that, set the environment variable
NO_NETWORK
or VALIDATE_PYPROJECT_NO_NETWORK
).
On the other hand, if validate-pyproject
cannot find a copy of
packaging
in your environment, the validation will fail.
More details about validate-pyproject
and its Python API can be found in
our docs, which includes a description of the used JSON schemas,
instructions for using it in a pre-compiled way and information about
extending the validation with your own plugins.
Tip
If you consider contributing to this project, have a look on our contribution guides.
validate-pyproject
can be installed as a pre-commit hook:
---
repos:
- repo: https://github.com/abravalheri/validate-pyproject
rev: main
hooks:
- id: validate-pyproject
By default, this pre-commit
hook will only validate the pyproject.toml
file at the root of the project repository.
You can customize that by defining a custom regular expression pattern using
the files
parameter.
This project and its sister project ini2toml were initially created in the
context of PyScaffold, with the purpose of helping migrating existing projects
to PEP 621-style configuration when it is made available on setuptools
.
For details and usage information on PyScaffold see https://pyscaffold.org/.