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

Fix #291: Disallow negative numbers in VersionInfo #292

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2020
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
30 changes: 30 additions & 0 deletions 30 CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@ Change Log
All notable changes to this code base will be documented in this file,
in every released version.

Version 2.12.0
==============

:Released:
:Maintainer: Tom Schraitle

Features
--------

n/a


Bug Fixes
---------

* :gh:`291` (:pr:`292`): Disallow negative numbers of
major, minor, and patch for ``semver.VersionInfo``


Additions
---------

n/a


Deprecations
------------

n/a


Version 2.11.0
==============
Expand Down
8 changes: 8 additions & 0 deletions 8 docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ A :class:`semver.VersionInfo` instance can be created in different ways:
>>> semver.VersionInfo(**d)
VersionInfo(major=3, minor=4, patch=5, prerelease='pre.2', build='build.4')

Keep in mind, the ``major``, ``minor``, ``patch`` parts has to
be positive.

>>> semver.VersionInfo(-1)
Traceback (most recent call last):
...
ValueError: 'major' is negative. A version can only be positive.

As a minimum requirement, your dictionary needs at least the ``major``
key, others can be omitted. You get a ``TypeError`` if your
dictionary contains invalid keys.
Expand Down
21 changes: 18 additions & 3 deletions 21 semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,24 @@ class VersionInfo(object):
)

def __init__(self, major, minor=0, patch=0, prerelease=None, build=None):
self._major = int(major)
self._minor = int(minor)
self._patch = int(patch)
# Build a dictionary of the arguments except prerelease and build
version_parts = {
"major": major,
"minor": minor,
"patch": patch,
}

for name, value in version_parts.items():
value = int(value)
version_parts[name] = value
if value < 0:
raise ValueError(
"{!r} is negative. A version can only be positive.".format(name)
)

self._major = version_parts["major"]
self._minor = version_parts["minor"]
self._patch = version_parts["patch"]
self._prerelease = None if prerelease is None else str(prerelease)
self._build = None if build is None else str(build)

Expand Down
14 changes: 14 additions & 0 deletions 14 test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ def test_fordocstrings(func):
assert func.__doc__, "Need a docstring for function %r" % func.__name


@pytest.mark.parametrize(
"ver",
[
{"major": -1},
{"major": 1, "minor": -2},
{"major": 1, "minor": 2, "patch": -3},
{"major": 1, "minor": -2, "patch": 3},
],
)
def test_should_not_allow_negative_numbers(ver):
with pytest.raises(ValueError, match=".* is negative. .*"):
VersionInfo(**ver)


@pytest.mark.parametrize(
"version,expected",
[
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.