Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0309c63

Browse files
authored
Fix #291: Disallow negative numbers in VersionInfo (#292)
Disallow negative numbers in major, minor, and patch in semver.VersionInfo. Reason: a version can only contain positive numbers according to the semver.org specification: "A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative integers, ..."
1 parent dd110f1 commit 0309c63

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

CHANGELOG.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@ Change Log
66
All notable changes to this code base will be documented in this file,
77
in every released version.
88

9+
Version 2.12.0
10+
==============
11+
12+
:Released:
13+
:Maintainer: Tom Schraitle
14+
15+
Features
16+
--------
17+
18+
n/a
19+
20+
21+
Bug Fixes
22+
---------
23+
24+
* :gh:`291` (:pr:`292`): Disallow negative numbers of
25+
major, minor, and patch for ``semver.VersionInfo``
26+
27+
28+
Additions
29+
---------
30+
31+
n/a
32+
33+
34+
Deprecations
35+
------------
36+
37+
n/a
38+
939

1040
Version 2.11.0
1141
==============

docs/usage.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ A :class:`semver.VersionInfo` instance can be created in different ways:
6363
>>> semver.VersionInfo(**d)
6464
VersionInfo(major=3, minor=4, patch=5, prerelease='pre.2', build='build.4')
6565

66+
Keep in mind, the ``major``, ``minor``, ``patch`` parts has to
67+
be positive.
68+
69+
>>> semver.VersionInfo(-1)
70+
Traceback (most recent call last):
71+
...
72+
ValueError: 'major' is negative. A version can only be positive.
73+
6674
As a minimum requirement, your dictionary needs at least the ``major``
6775
key, others can be omitted. You get a ``TypeError`` if your
6876
dictionary contains invalid keys.

semver.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,24 @@ class VersionInfo(object):
243243
)
244244

245245
def __init__(self, major, minor=0, patch=0, prerelease=None, build=None):
246-
self._major = int(major)
247-
self._minor = int(minor)
248-
self._patch = int(patch)
246+
# Build a dictionary of the arguments except prerelease and build
247+
version_parts = {
248+
"major": major,
249+
"minor": minor,
250+
"patch": patch,
251+
}
252+
253+
for name, value in version_parts.items():
254+
value = int(value)
255+
version_parts[name] = value
256+
if value < 0:
257+
raise ValueError(
258+
"{!r} is negative. A version can only be positive.".format(name)
259+
)
260+
261+
self._major = version_parts["major"]
262+
self._minor = version_parts["minor"]
263+
self._patch = version_parts["patch"]
249264
self._prerelease = None if prerelease is None else str(prerelease)
250265
self._build = None if build is None else str(build)
251266

test_semver.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ def test_fordocstrings(func):
7373
assert func.__doc__, "Need a docstring for function %r" % func.__name
7474

7575

76+
@pytest.mark.parametrize(
77+
"ver",
78+
[
79+
{"major": -1},
80+
{"major": 1, "minor": -2},
81+
{"major": 1, "minor": 2, "patch": -3},
82+
{"major": 1, "minor": -2, "patch": 3},
83+
],
84+
)
85+
def test_should_not_allow_negative_numbers(ver):
86+
with pytest.raises(ValueError, match=".* is negative. .*"):
87+
VersionInfo(**ver)
88+
89+
7690
@pytest.mark.parametrize(
7791
"version,expected",
7892
[

0 commit comments

Comments
 (0)