Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fba756e

Browse filesBrowse files
committed
Fix #291: Disallow negative numbers in VersionInfo
Disallow negative numbers in major, minor, and patch in semver.VersionInfo. Reason: a version can only contain positive numbers
1 parent dd110f1 commit fba756e
Copy full SHA for fba756e

File tree

2 files changed

+32
-3
lines changed
Filter options

2 files changed

+32
-3
lines changed

‎semver.py

Copy file name to clipboardExpand all lines: semver.py
+18-3Lines 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

Copy file name to clipboardExpand all lines: test_semver.py
+14Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.