Skip to content

Navigation Menu

Sign in
Appearance settings

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 f332326

Browse filesBrowse files
tlaferrieretomschr
andauthored
Fix python-semver#260 __getitem__ returning None on falsy parts
* Fix python-semver#260 and add tests for these special cases * Fix IndexError not being thrown every time it should * Update CHANGELOG.rst Co-authored-by: Tom Schraitle <tomschr@users.noreply.github.com>
1 parent 69d1cf2 commit f332326
Copy full SHA for f332326

File tree

3 files changed

+60
-12
lines changed
Filter options

3 files changed

+60
-12
lines changed

‎CHANGELOG.rst

Copy file name to clipboardExpand all lines: CHANGELOG.rst
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ All notable changes to this code base will be documented in this file,
77
in every released version.
88

99

10+
Version 2.10.2 (WIP)
11+
====================
12+
13+
:Released: 2020-xx-yy
14+
:Maintainer:
15+
16+
Features
17+
--------
18+
19+
n/a
20+
21+
Bug Fixes
22+
---------
23+
24+
:gh:`260` (:pr:`261`): Fixed ``__getitem__`` returning None on wrong parts
25+
26+
27+
Additions
28+
---------
29+
30+
n/a
31+
32+
Removals
33+
--------
34+
35+
n/a
36+
37+
1038
Version 2.10.1
1139
==============
1240

‎semver.py

Copy file name to clipboardExpand all lines: semver.py
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -548,17 +548,16 @@ def __getitem__(self, index):
548548

549549
if (
550550
isinstance(index, slice)
551-
and (index.start is None or index.start < 0)
552-
and (index.stop is None or index.stop < 0)
551+
and (index.start is not None and index.start < 0)
552+
or (index.stop is not None and index.stop < 0)
553553
):
554554
raise IndexError("Version index cannot be negative")
555555

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

559558
if len(part) == 1:
560559
part = part[0]
561-
if not part:
560+
elif not part:
562561
raise IndexError("Version part undefined")
563562
return part
564563

‎test_semver.py

Copy file name to clipboardExpand all lines: test_semver.py
+28-7Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ def test_should_be_able_to_use_integers_as_prerelease_build():
774774
("1.2.3", 0, 1),
775775
("1.2.3", 1, 2),
776776
("1.2.3", 2, 3),
777+
# Special cases
778+
("1.0.2", 1, 0),
777779
],
778780
)
779781
def test_version_info_should_be_accessed_with_index(version, index, expected):
@@ -801,6 +803,7 @@ def test_version_info_should_be_accessed_with_index(version, index, expected):
801803
("1.2.3-rc.0+build.0", slice(0, 5, 2), (1, 3, "build.0")),
802804
("1.2.3-rc.0+build.0", slice(None, 5, 2), (1, 3, "build.0")),
803805
("1.2.3-rc.0+build.0", slice(5, 0, -2), ("build.0", 3)),
806+
("1.2.0-rc.0+build.0", slice(3), (1, 2, 0)),
804807
],
805808
)
806809
def test_version_info_should_be_accessed_with_slice_object(
@@ -813,19 +816,37 @@ def test_version_info_should_be_accessed_with_slice_object(
813816
@pytest.mark.parametrize(
814817
"version, index",
815818
[
816-
("1.2.3-rc.0+build.0", -1),
817-
("1.2.3-rc.0", -1),
818-
("1.2.3-rc.0", 4),
819-
("1.2.3", -1),
820819
("1.2.3", 3),
820+
("1.2.3", slice(3, 4)),
821821
("1.2.3", 4),
822-
("1.2.3", 10),
823-
("1.2.3", slice(-3)),
822+
("1.2.3", slice(4, 5)),
823+
("1.2.3", 5),
824+
("1.2.3", slice(5, 6)),
825+
("1.2.3-rc.0", 5),
826+
("1.2.3-rc.0", slice(5, 6)),
827+
("1.2.3-rc.0", 6),
828+
("1.2.3-rc.0", slice(6, 7)),
824829
],
825830
)
826831
def test_version_info_should_throw_index_error(version, index):
827832
version_info = VersionInfo.parse(version)
828-
with pytest.raises(IndexError):
833+
with pytest.raises(IndexError, match=r"Version part undefined"):
834+
version_info[index]
835+
836+
837+
@pytest.mark.parametrize(
838+
"version, index",
839+
[
840+
("1.2.3", -1),
841+
("1.2.3", -2),
842+
("1.2.3", slice(-2, 2)),
843+
("1.2.3", slice(2, -2)),
844+
("1.2.3", slice(-2, -2)),
845+
],
846+
)
847+
def test_version_info_should_throw_index_error_when_negative_index(version, index):
848+
version_info = VersionInfo.parse(version)
849+
with pytest.raises(IndexError, match=r"Version index cannot be negative"):
829850
version_info[index]
830851

831852

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.