Skip to content

Fix #210: how to deal with invalid versions #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy