summaryrefslogtreecommitdiff
path: root/semantic_version
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2019-08-18 17:41:33 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2019-08-24 15:13:09 +0200
commita5cc0fb509b2c515ae73c85f9a4d426a3f9100e3 (patch)
tree2beb1ad109f28f995b293a548da4ef76818b7bc3 /semantic_version
parentfdef1e9cdae901d095d8e8c9cd6fa6adcfe02074 (diff)
downloadsemantic-version-a5cc0fb509b2c515ae73c85f9a4d426a3f9100e3.tar.gz
Allow Version(major=1, ...).
Eases the creation of version objects from existing versions. We still validate the type and structure of each component.
Diffstat (limited to 'semantic_version')
-rw-r--r--semantic_version/base.py45
1 files changed, 43 insertions, 2 deletions
diff --git a/semantic_version/base.py b/semantic_version/base.py
index 4ddaf05..49c3fb5 100644
--- a/semantic_version/base.py
+++ b/semantic_version/base.py
@@ -73,8 +73,30 @@ class Version(object):
version_re = re.compile(r'^(\d+)\.(\d+)\.(\d+)(?:-([0-9a-zA-Z.-]+))?(?:\+([0-9a-zA-Z.-]+))?$')
partial_version_re = re.compile(r'^(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:-([0-9a-zA-Z.-]*))?(?:\+([0-9a-zA-Z.-]*))?$')
- def __init__(self, version_string, partial=False):
- major, minor, patch, prerelease, build = self.parse(version_string, partial)
+ def __init__(
+ self,
+ version_string=None,
+ *,
+ major=None,
+ minor=None,
+ patch=None,
+ prerelease=None,
+ build=None,
+ partial=False,
+ ):
+ has_text = version_string is not None
+ has_parts = not (major is minor is patch is prerelease is build is None)
+ if not has_text ^ has_parts:
+ raise ValueError("Call either Version('1.2.3') or Version(major=1, ...).")
+
+ if has_text:
+ major, minor, patch, prerelease, build = self.parse(version_string, partial)
+ else:
+ # Convenience: allow to omit prerelease/build.
+ if not partial:
+ prerelease = prerelease or ()
+ build = build or ()
+ self._validate_kwargs(major, minor, patch, prerelease, build, partial)
self.major = major
self.minor = minor
@@ -254,6 +276,25 @@ class Version(object):
if item[0] == '0' and item.isdigit() and item != '0' and not allow_leading_zeroes:
raise ValueError("Invalid leading zero in identifier %r" % item)
+ @classmethod
+ def _validate_kwargs(cls, major, minor, patch, prerelease, build, partial):
+ if (
+ major != int(major)
+ or minor != cls._coerce(minor, partial)
+ or patch != cls._coerce(patch, partial)
+ or prerelease is None and not partial
+ or build is None and not partial
+ ):
+ raise ValueError(
+ "Invalid kwargs to Version(major=%r, minor=%r, patch=%r, "
+ "prerelease=%r, build=%r, partial=%r" % (
+ major, minor, patch, prerelease, build, partial
+ ))
+ if prerelease is not None:
+ cls._validate_identifiers(prerelease, allow_leading_zeroes=False)
+ if build is not None:
+ cls._validate_identifiers(build, allow_leading_zeroes=True)
+
def __iter__(self):
return iter((self.major, self.minor, self.patch, self.prerelease, self.build))