Python versions hook is a pre-commit hook to maintain the Python versions in sync.
Support:
- Poetry as version provider and update classifiers in
pyproject.toml. - Mypy in
pyporoject.tomland in.prospector.yaml. - Pyupgrade as pre-commit hook.
- Ruff in
pyporoject.tomland in.prospector.yaml. - Prospector in
.prospector.yamlfor Mypy and Ruff. - PEP-621 in
pyproject.toml. - Pyenv in
.python-version. - pre-commit in
.pre-commit-config.yaml. - jsonschema-gentypes in
jsonschema-gentypes.yaml.
repos:
- repo: https://github.com/sbrunner/python-versions-hook
rev: <version> # Use the ref you want to point at
hooks:
- id: python-versionsThis hook automatically keeps your Python version settings consistent across various configuration files in your project. When you run the hook, it:
- Detects the Python versions your project supports from your Poetry dependencies or PEP-621
requires-pythonfield - Updates the following based on the supported versions:
- Python classifiers in
pyproject.toml - Mypy's
python_versionsetting in bothpyproject.tomland.prospector.yaml - Ruff's
target-versionsetting in bothpyproject.tomland.prospector.yaml - Black's
target-versionsetting inpyproject.toml - Pyupgrade's
--py{version}-plusargument in.pre-commit-config.yaml - Python version in
jsonschema-gentypes.yaml
- Python classifiers in
Once installed as a pre-commit hook, it will run automatically when you commit changes to your repository.
You can also run it manually with:
pre-commit run python-versions --all-filesThe options are stored in the pyproject.toml file under the [tool.python-versions-hook] section.
keep-requires-python: Controls whether the hook modifies therequires-pythonfield in thepyproject.tomlfile.- When
false(default): The hook will automatically update therequires-pythonfield. - When
true: The hook will not modify therequires-pythonfield (preserves its existing value).
- When
This project can also be used as a replacement of the Poetry plugin tweak dependencies version project.
The configuration is like that:
[tool.poetry-plugin-tweak-dependencies-version]
default = "(present|major|minor|patch|full)" # Default to `full`
"<package>" = "(present|major|minor|patch|full|<alternate version>)"And he will fill the PEP 631 project.dependencies and project.optional-dependencies section from the
Poetry section with values that respect the configuration:
present=> Just add the package as dependency.major=> Just fix the major version.minor=> Just fix the major and minor version.patch=> Just fix the major, minor and patch version.full=> Get the full version from the Poetry section.<alternate version>=> use is as a version.