Skip to content

Commit

Permalink
Fix #210: how to deal with invalid versions (#215)
Browse files Browse the repository at this point in the history
* Document how to deal with invalid versions
* Use coerce(version) as an example
* Update CHANGELOG.rst

Co-authored-by: scls19fr <[email protected]>
  • Loading branch information
tomschr and scls19fr committed Jan 19, 2020
1 parent 5755f9a commit b5ccad6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Features
* :gh:`201` (:pr:`202`): Reformatted source code with black
* :gh:`208` (:pr:`209`): Introduce new function :func:`semver.VersionInfo.isvalid`
and extend :command:`pysemver` with :command:`check` subcommand
* :gh:`210` (:pr:`215`): Document how to deal with invalid versions
* :pr:`212`: Improve docstrings according to PEP257

Bug Fixes
Expand Down
72 changes: 72 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,75 @@ Getting Minimum and Maximum of two Versions
'2.0.0'
>>> semver.min_ver("1.0.0", "2.0.0")
'1.0.0'
Dealing with Invalid Versions
-----------------------------

As semver follows the semver specification, it cannot parse version
strings which are considered "invalid" by that specification. The semver
library cannot know all the possible variations so you need to help the
library a bit.

For example, if you have a version string ``v1.2`` would be an invalid
semver version.
However, "basic" version strings consisting of major, minor,
and patch part, can be easy to convert. The following function extract this
information and returns a tuple with two items:

.. code-block:: python
import re
BASEVERSION = re.compile(
r"""[vV]?
(?P<major>0|[1-9]\d*)
(\.
(?P<minor>0|[1-9]\d*)
(\.
(?P<patch>0|[1-9]\d*)
)?
)?
""",
re.VERBOSE,
)
def coerce(version):
"""
Convert an incomplete version string into a semver-compatible VersionInfo
object
* Tries to detect a "basic" version string (``major.minor.patch``).
* If not enough components can be found, missing components are
set to zero to obtain a valid semver version.
:param str version: the version string to convert
:return: a tuple with a :class:`VersionInfo` instance (or ``None``
if it's not a version) and the rest of the string which doesn't
belong to a basic version.
:rtype: tuple(:class:`VersionInfo` | None, str)
"""
match = BASEVERSION.search(version)
if not match:
return (None, version)
ver = {
key: 0 if value is None else value
for key, value in match.groupdict().items()
}
ver = semver.VersionInfo(**ver)
rest = match.string[match.end() :]
return ver, rest
The function returns a *tuple*, containing a :class:`VersionInfo`
instance or None as the first element and the rest as the second element.
The second element (the rest) can be used to make further adjustments.

For example:

.. code-block:: python
>>> coerce("v1.2")
(VersionInfo(major=1, minor=2, patch=0, prerelease=None, build=None), '')
>>> coerce("v2.5.2-bla")
(VersionInfo(major=2, minor=5, patch=2, prerelease=None, build=None), '-bla')

0 comments on commit b5ccad6

Please sign in to comment.