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 bc41390

Browse filesBrowse files
committed
Fix comparison with subclasses
1 parent bd97cfc commit bc41390
Copy full SHA for bc41390

File tree

2 files changed

+19
-1
lines changed
Filter options

2 files changed

+19
-1
lines changed

‎src/semver/version.py

Copy file name to clipboardExpand all lines: src/semver/version.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _comparator(operator: Comparator) -> Comparator:
4141
@wraps(operator)
4242
def wrapper(self: "Version", other: Comparable) -> bool:
4343
comparable_types = (
44-
Version,
44+
type(self),
4545
dict,
4646
tuple,
4747
list,

‎tests/test_subclass.py

Copy file name to clipboardExpand all lines: tests/test_subclass.py
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from semver import Version
2+
import pytest
23

34

45
def test_subclass_from_versioninfo():
@@ -51,3 +52,20 @@ def __str__(self) -> str:
5152
dev_version = version.replace(prerelease="dev.0")
5253

5354
assert str(dev_version) == "v1.1.0-dev.0"
55+
56+
57+
def test_compare_with_subclass():
58+
class SemVerSubclass(Version):
59+
pass
60+
61+
with pytest.raises(TypeError):
62+
SemVerSubclass.parse("1.0.0").compare(Version.parse("1.0.0"))
63+
assert Version.parse("1.0.0").compare(SemVerSubclass.parse("1.0.0")) == 0
64+
65+
assert (
66+
SemVerSubclass.parse("1.0.0").__eq__(Version.parse("1.0.0")) is NotImplemented
67+
)
68+
assert Version.parse("1.0.0").__eq__(SemVerSubclass.parse("1.0.0")) is True
69+
70+
assert SemVerSubclass.parse("1.0.0") == Version.parse("1.0.0")
71+
assert Version.parse("1.0.0") == SemVerSubclass.parse("1.0.0")

0 commit comments

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