Library API
High-Level Functions and Classes
- versioningit.get_version(project_dir: str | Path = '.', config: dict | None = None, write: bool = False, fallback: bool = True) str[source]
Determine the version for the project at
project_dir.If
configisNone, thenproject_dirmust contain aversioningit.tomlorpyproject.tomlfile; if both files are present, theversioningit.tomlfile is used. This file must then contain either a[tool.versioningit]table or a[tool.hatch.version]table with thesourcekey set to"versioningit"; if it does not, aNoConfigSectionErroris raised. Ifconfigis notNone, then any configuration files inproject_dirwill be ignored, and the configuration will be taken fromconfiginstead. See “Passing Explicit Configuration”.If
writeis true, then the file specified in thewritesubtable of the versioningit configuration, if any, will be updated.If
fallbackis true, then ifproject_diris not under version control (or if the VCS executable is not installed),versioningitwill assume that the directory is an unpacked sdist and will read the version from thePKG-INFOfile.Changed in version 3.2.0: A
versioningit.tomlfile can now be read in place ofpyproject.toml- Raises:
NotVCSError – if
fallbackis false andproject_diris not under version controlNotSdistError – if
fallbackis true,project_diris not under version control, and there is noPKG-INFOfile inproject_dirNoConfigFileError – if
configisNoneandproject_dirdoes not contain apyproject.tomlorversioningit.tomlfileNoConfigSectionError – if
configisNoneand the configuration file does not contain a versioningit configuration tableConfigError – if any of the values in
configare not of the correct typeMethodError – if a method returns a value of the wrong type
- versioningit.get_next_version(project_dir: str | Path = '.', config: dict | None = None) str[source]
Added in version 0.3.0.
Determine the next version after the current VCS-tagged version for
project_dir.If
configisNone, thenproject_dirmust contain aversioningit.tomlorpyproject.tomlfile; if both files are present, theversioningit.tomlfile is used. This file must then contain either a[tool.versioningit]table or a[tool.hatch.version]table with thesourcekey set to"versioningit"; if it does not, aNoConfigSectionErroris raised. Ifconfigis notNone, then any configuration files inproject_dirwill be ignored, and the configuration will be taken fromconfiginstead. See “Passing Explicit Configuration”.Changed in version 3.2.0: A
versioningit.tomlfile can now be read in place ofpyproject.toml- Raises:
NotVCSError – if
project_diris not under version controlNoConfigFileError – if
configisNoneandproject_dirdoes not contain apyproject.tomlorversioningit.tomlfileNoConfigSectionError – if
configisNoneand the configuration file does not contain a versioningit configuration tableConfigError – if any of the values in
configare not of the correct typeMethodError – if a method returns a value of the wrong type
- versioningit.get_cmdclasses(bases: dict[str, type[Command]] | None = None) dict[str, type[Command]][source]
Added in version 1.1.0.
Return a
dictof custom setuptoolsCommandclasses, suitable for passing to thecmdclassargument ofsetuptools.setup(), that run theonbuildstep for the project when building an sdist or wheel. Specifically, thedictcontains a subclass ofsetuptools.command.sdist.sdistat the"sdist"key and a subclass ofsetuptools.command.build_py.build_pyat the"build_py"key.A
dictof alternative base classes can optionally be supplied; if thedictcontains an"sdist"entry, that entry will be used as the base class for the customizedsdistcommand, and likewise for"build_py". All other classes in the inputdictare passed through unchanged.
- class versioningit.cmdclass.sdist
Added in version 2.2.0.
A custom subclass of
setuptools.command.sdist.sdistthat runs theonbuildstep when building an sdist. This class is equivalent toget_cmdclasses()["sdist"], except that it can also be used in the[options]cmdclassfield insetup.cfg.
- class versioningit.cmdclass.build_py
Added in version 2.2.0.
A custom subclass of
setuptools.command.build_py.build_pythat runs theonbuildstep when building a wheel. This class is equivalent toget_cmdclasses()["build_py"], except that it can also be used in the[options]cmdclassfield insetup.cfg.
Note
When importing or referring to the sdist and build_py command
classes, the .cmdclass submodule needs to be specified; unlike the rest
of the library API, they are not importable directly from versioningit.
[options]
cmdclass =
# Right!
sdist = versioningit.cmdclass.sdist
build_py = versioningit.cmdclass.build_py
[options]
cmdclass =
# Wrong!
sdist = versioningit.sdist
build_py = versioningit.build_py
Low-Level Class
- class versioningit.Versioningit[source]
A class for getting a version-controlled project’s current version based on its most recent tag and the difference therefrom
- classmethod from_project_dir(project_dir: str | Path = '.', config: dict | None = None) Versioningit[source]
Construct a
Versioningitobject for the project rooted atproject_dir(default: the current directory).If
configisNone, thenproject_dirmust contain aversioningit.tomlorpyproject.tomlfile; if both files are present, theversioningit.tomlfile is used. This file must then contain either a[tool.versioningit]table or a[tool.hatch.version]table with thesourcekey set to"versioningit"; if it does not, aNoConfigSectionErroris raised. Ifconfigis notNone, then any configuration files inproject_dirwill be ignored, and the configuration will be taken fromconfiginstead. See “Passing Explicit Configuration”.Changed in version 3.2.0: A
versioningit.tomlfile can now be read in place ofpyproject.toml- Raises:
NoConfigFileError – if
configisNoneandproject_dirdoes not contain apyproject.tomlorversioningit.tomlfileNoConfigSectionError – if
configisNoneand the configuration file does not contain a versioningit configuration tableConfigError – if the configuration object/table or any of its subfields are not of the correct type
- get_version(write: bool = False, fallback: bool = True) str[source]
Determine the version for the project.
If
writeis true, then the file specified in thewritesubtable of the versioningit configuration, if any, will be updated.If
fallbackis true, then ifproject_diris not under version control (or if the VCS executable is not installed),versioningitwill assume that the directory is an unpacked sdist and will read the version from thePKG-INFOfile.Changed in version 2.0.0:
writeandfallbackarguments added- Raises:
NotVCSError – if
fallbackis false andproject_diris not under version controlNotSdistError – if
fallbackis true,project_diris not under version control, and there is noPKG-INFOfile inproject_dirConfigError – if any of the values in
configare not of the correct typeMethodError – if a method returns a value of the wrong type
- run(write: bool = False, fallback: bool = True) Report | FallbackReport[source]
Added in version 2.0.0.
Run all of the steps for the project — aside from “onbuild” and, optionally, “write” — and return an object containing the final version and intermediate values.
If
writeis true, then the file specified in thewritesubtable of the versioningit configuration, if any, will be updated.If
fallbackis true, then ifproject_diris not under version control (or if the VCS executable is not installed),versioningitwill assume that the directory is an unpacked sdist and will read the version from thePKG-INFOfile, returning aFallbackReportinstance instead of aReport.- Raises:
NotVCSError – if
fallbackis false andproject_diris not under version controlNotSdistError – if
fallbackis true,project_diris not under version control, and there is noPKG-INFOfile inproject_dirConfigError – if any of the values in
configare not of the correct typeMethodError – if a method returns a value of the wrong type
- do_vcs() VCSDescription[source]
Run the
vcsstep- Raises:
MethodError – if the method does not return a
VCSDescription
- do_tag2version(tag: str) str[source]
Run the
tag2versionstep- Raises:
MethodError – if the method does not return a
str
- do_next_version(version: str, branch: str | None) str[source]
Run the
next-versionstep- Raises:
MethodError – if the method does not return a
str
- do_format(description: VCSDescription, base_version: str, next_version: str) str[source]
Run the
formatstepChanged in version 2.0.0: The
versionargument was renamed tobase_version.- Raises:
MethodError – if the method does not return a
str
- do_template_fields(version: str, description: VCSDescription | None, base_version: str | None, next_version: str | None) dict[source]
Added in version 2.0.0.
Run the
template_fieldsstep- Raises:
MethodError – if the method does not return a
dict
- do_write(template_fields: dict[str, Any]) None[source]
Run the
writestepChanged in version 2.0.0:
versionargument replaced withtemplate_fields
- do_onbuild(file_provider: OnbuildFileProvider, is_source: bool, template_fields: dict[str, Any]) None[source]
Added in version 1.1.0.
Run the
onbuildstepChanged in version 2.0.0:
versionargument replaced withtemplate_fieldsChanged in version 3.0.0:
build_dirargument replaced withfile_provider
Exceptions
- exception versioningit.ConfigError[source]
Bases:
Error,ValueErrorRaised when the
versioningitconfiguration contain invalid settings
- exception versioningit.InvalidTagError[source]
Bases:
Error,ValueErrorRaised by
tag2versionmethods when passed a tag that they cannot work with
- exception versioningit.InvalidVersionError[source]
Bases:
Error,ValueErrorRaised by
next-versionandtemplate-fieldsmethods when passed a version that they cannot work with
- exception versioningit.MethodError[source]
Bases:
ErrorRaised when a method is invalid or returns an invalid value
- exception versioningit.NoTagError[source]
Bases:
ErrorRaised when a tag cannot be found in version control
- exception versioningit.NotSdistError[source]
Bases:
ErrorRaised when attempting to read a
PKG-INFOfile from a directory that doesn’t have one
- exception versioningit.NotVCSError[source]
Bases:
ErrorRaised when
versioningitis run in a directory that is not under version control or when the relevant VCS program is not installed
- exception versioningit.NotVersioningitError[source]
Bases:
ErrorRaised when
versioningitis used on a project that does not haveversioningitenabled
- exception versioningit.NoConfigFileError(project_dir: Path)[source]
Bases:
NotVersioningitErrorAdded in version 3.2.0.
Raised when
versioningitis used on a project that does not contain aversioningit.tomlorpyproject.tomlfile
- exception versioningit.NoConfigSectionError(config_path: Path)[source]
Bases:
NotVersioningitErrorAdded in version 3.2.0.
Raised when
versioningitis used on a project whoseversioningit.tomlorpyproject.tomlfile does not contain aversioningitconfiguration table
Utilities
- class versioningit.VCSDescription(tag: str, state: str, branch: str | None, fields: dict[str, Any])[source]
A description of the state of a version control repository
- branch: str | None
The name of the repository’s current branch, or
Noneif it cannot be determined or does not apply
- fields: dict[str, Any]
A
dictof additional information about the repository state to make available to theformatmethod. Customvcsmethods are advised to adhere closely to the set of fields used by the built-in methods.
- state: str
The relationship of the repository’s current state to the tag. If the repository state is exactly the tagged state, this field should equal
"exact"; otherwise, it will be a string that will be used as a key in theformatsubtable of the versioningit configuration. Recommended values are"distance","dirty", and"distance-dirty".
- class versioningit.Report(version: str, description: VCSDescription | None, base_version: str | None, next_version: str | None, template_fields: dict[str, Any], using_default_version: bool)[source]
Added in version 2.0.0.
A report of the intermediate & final values calculated during a
versioningitrun- base_version: str | None
A version string extracted from the VCS tag;
Noneif the “tag2version” step or a previous step failed
- description: VCSDescription | None
A description of the state of the version control repository;
Noneif the “vcs” step failed
- next_version: str | None
A “next version” calculated by the “next-version” step;
Noneif the step or a previous one failed
- template_fields: dict[str, Any]
A
dictof fields for use in templating by the “write” and “onbuild” steps
- class versioningit.FallbackReport(version: str)[source]
Added in version 2.0.0.
A report of the version extracted from a
PKG-INFOfile in an sdist
- versioningit.get_version_from_pkg_info(project_dir: str | Path) str[source]
Return the Version field from the
PKG-INFOfile inproject_dir- Raises:
NotSdistError – if there is no
PKG-INFOfileValueError – if the
PKG-INFOfile does not contain a Version field
- versioningit.run_onbuild(*, build_dir: str | Path, is_source: bool, template_fields: dict[str, Any], project_dir: str | Path = '.', config: dict | None = None) None[source]
Added in version 1.1.0.
Run the
onbuildstep for the given setuptools project.This function is intended to be used by custom setuptools command classes that are used in place of
versioningit’s command classes but still need to be able to run theonbuildstep. Thetemplate_fieldsvalue can be obtained by passing a command class’sdistributionattribute toget_template_fields_from_distribution(); if this returnsNone, then we are building from an sdist, andrun_onbuild()should not be called.If
configisNone, thenproject_dirmust contain aversioningit.tomlorpyproject.tomlfile; if both files are present, theversioningit.tomlfile is used. This file must then contain either a[tool.versioningit]table or a[tool.hatch.version]table with thesourcekey set to"versioningit"; if it does not, aNoConfigSectionErroris raised. Ifconfigis notNone, then any configuration files inproject_dirwill be ignored, and the configuration will be taken fromconfiginstead. See “Passing Explicit Configuration”.Changed in version 2.0.0:
versionargument replaced withtemplate_fieldsChanged in version 3.2.0: A
versioningit.tomlfile can now be read in place ofpyproject.toml- Parameters:
- Raises:
NoConfigFileError – if
configisNoneandproject_dirdoes not contain apyproject.tomlorversioningit.tomlfileNoConfigSectionError – if
configisNoneand the configuration file does not contain a versioningit configuration tableConfigError – if any of the values in
configare not of the correct typeMethodError – if a method returns a value of the wrong type
- versioningit.get_template_fields_from_distribution(dist: Distribution) dict[str, Any] | None[source]
Extract the template fields (calculated by the “template-fields” step) that were stashed on the
setuptools.Distributionbyversioningit’s setuptools hook, for passing to the “onbuild” step. If setuptools is building from an sdist instead of a repository, no template fields will have been calculated, andNonewill be returned, indicating that the “onbuild” step should not be run.
Passing Explicit Configuration
The functions & methods that take a path to a project directory normally read
the project’s configuration from the versioningit.toml or
pyproject.toml file therein, but they can also be passed a config
argument to take the configuration from instead, in which case any
configuration files will be ignored and need not even exist.
A config argument must be a dict whose structure mirrors the structure of
the [tool.versioningit] table in pyproject.toml. For example, the
following TOML configuration:
[tool.versioningit.vcs]
method = "git"
match = ["v*"]
[tool.versioningit.next-version]
method = { module = "setup", value = "my_next_version" }
[tool.versioningit.format]
distance = "{next_version}.dev{distance}+{vcs}{rev}"
dirty = "{base_version}+dirty"
distance-dirty = "{next_version}.dev{distance}+{vcs}{rev}.dirty"
corresponds to the following Python config value:
{
"vcs": {
"method": "git",
"match": ["v*"],
},
"next-version": {
"method": {
"module": "setup",
"value": "my_next_version",
},
},
"format": {
"distance": "{next_version}.dev{distance}+{vcs}{rev}",
"dirty": "{base_version}+dirty",
"distance-dirty": "{next_version}.dev{distance}+{vcs}{rev}.dirty",
},
}
This is the same structure that you would get by reading from the
pyproject.toml file like so:
import tomli
with open("pyproject.toml", "rb") as fp:
config = tomli.load(fp)["tool"]["versioningit"]
When passing versioningit configuration as a config argument, an
alternative way to specify methods becomes available: in place of a method
specification, one can pass a callable object directly.