Skip to content

Commit bc41390

Browse files
committed
Fix comparison with subclasses
1 parent bd97cfc commit bc41390

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/semver/version.py

Lines 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

Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy