Skip to content

Fix #260 __getitem__ returning None on falsy parts #261

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 26 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f1d7cde
Add __getitem__ method to allow easy access to version parts
tlaferriere Aug 16, 2019
ba5899e
Add unit tests for index access to version parts
tlaferriere Aug 19, 2019
e88e0ba
Remove whitespace on blank line
tlaferriere Aug 19, 2019
c30d38f
Merge remote-tracking branch 'kbx/master'
tlaferriere Oct 1, 2019
26a7e03
Merge upstream into fork and fix conflicts
tlaferriere Oct 8, 2019
80da20c
Fix stash conflicts and create more test vectors. Also add private ut…
tlaferriere Oct 8, 2019
a610c22
Use simpler implementation that allows negative indices and remove no…
tlaferriere Oct 8, 2019
95e79d8
Add param and raises to __getitem__ docstring
tlaferriere Oct 10, 2019
ab35e38
Merge branch 'master' of github.com:k-bx/python-semver
tlaferriere Nov 2, 2019
27632b7
Reiterate the logic for __getitem__ and update tests to follow the ac…
tlaferriere Nov 20, 2019
976a4ee
Update __getitem__ docstring
tlaferriere Nov 20, 2019
cf65b3d
Fix lint issues
tlaferriere Nov 20, 2019
dabebd9
Fix lint issues
tlaferriere Nov 20, 2019
847083b
Merge remote-tracking branch 'origin/master'
May 13, 2020
c358a33
Add documentation to VersionInfo.next_version and return only Version…
May 13, 2020
85f7a88
Add :class:
May 13, 2020
a32de10
Add 2.10.1 to changelog
tlaferriere May 13, 2020
d5a7f62
Update PR number in changelog
tlaferriere May 13, 2020
bb4464d
Update CHANGELOG.rst
tomschr May 13, 2020
9c44e03
Fix #260 and add tests for these special cases and fix IndexError not…
Jun 9, 2020
c68de24
Merge remote-tracking branch 'upstream/master'
Jun 9, 2020
b6f1e20
Remove comment (suggestion)
tlaferriere Jun 9, 2020
ac6ece5
Add tests, remove redundant if clause and fix logic for negative inde…
Jun 9, 2020
8ddc7aa
Merge remote-tracking branch 'origin/master'
Jun 9, 2020
f6a17b3
Update CHANGELOG.rst
Jun 9, 2020
aeae24a
Apply suggestion to combine ifs then raise into one
tlaferriere Jun 9, 2020
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
28 changes: 28 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ All notable changes to this code base will be documented in this file,
in every released version.


Version 2.10.2 (WIP)
====================

:Released: 2020-xx-yy
:Maintainer:

Features
--------

n/a

Bug Fixes
---------

:gh:`260` (:pr:`261`): Fixed ``__getitem__`` returning None on wrong parts


Additions
---------

n/a

Removals
--------

n/a


Version 2.10.1
==============

Expand Down
9 changes: 4 additions & 5 deletions semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,17 +548,16 @@ def __getitem__(self, index):

if (
isinstance(index, slice)
and (index.start is None or index.start < 0)
and (index.stop is None or index.stop < 0)
and (index.start is not None and index.start < 0)
or (index.stop is not None and index.stop < 0)
):
raise IndexError("Version index cannot be negative")

# Could raise IndexError:
part = tuple(filter(None, self.to_tuple()[index]))
part = tuple(filter(lambda p: p is not None, self.to_tuple()[index]))

if len(part) == 1:
part = part[0]
if not part:
elif not part:
raise IndexError("Version part undefined")
return part

Expand Down
35 changes: 28 additions & 7 deletions test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ def test_should_be_able_to_use_integers_as_prerelease_build():
("1.2.3", 0, 1),
("1.2.3", 1, 2),
("1.2.3", 2, 3),
# Special cases
("1.0.2", 1, 0),
],
)
def test_version_info_should_be_accessed_with_index(version, index, expected):
Expand Down Expand Up @@ -801,6 +803,7 @@ def test_version_info_should_be_accessed_with_index(version, index, expected):
("1.2.3-rc.0+build.0", slice(0, 5, 2), (1, 3, "build.0")),
("1.2.3-rc.0+build.0", slice(None, 5, 2), (1, 3, "build.0")),
("1.2.3-rc.0+build.0", slice(5, 0, -2), ("build.0", 3)),
("1.2.0-rc.0+build.0", slice(3), (1, 2, 0)),
],
)
def test_version_info_should_be_accessed_with_slice_object(
Expand All @@ -813,19 +816,37 @@ def test_version_info_should_be_accessed_with_slice_object(
@pytest.mark.parametrize(
"version, index",
[
("1.2.3-rc.0+build.0", -1),
("1.2.3-rc.0", -1),
("1.2.3-rc.0", 4),
("1.2.3", -1),
("1.2.3", 3),
("1.2.3", slice(3, 4)),
("1.2.3", 4),
("1.2.3", 10),
("1.2.3", slice(-3)),
("1.2.3", slice(4, 5)),
("1.2.3", 5),
("1.2.3", slice(5, 6)),
("1.2.3-rc.0", 5),
("1.2.3-rc.0", slice(5, 6)),
("1.2.3-rc.0", 6),
("1.2.3-rc.0", slice(6, 7)),
],
)
def test_version_info_should_throw_index_error(version, index):
version_info = VersionInfo.parse(version)
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=r"Version part undefined"):
version_info[index]


@pytest.mark.parametrize(
"version, index",
[
("1.2.3", -1),
("1.2.3", -2),
("1.2.3", slice(-2, 2)),
("1.2.3", slice(2, -2)),
("1.2.3", slice(-2, -2)),
],
)
def test_version_info_should_throw_index_error_when_negative_index(version, index):
version_info = VersionInfo.parse(version)
with pytest.raises(IndexError, match=r"Version index cannot be negative"):
version_info[index]


Expand Down