Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37733f3

Browse files
Add examples from Raphael
Co-authored-by: Raphael Krupinski <[email protected]>
1 parent edb81ba commit 37733f3

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

docs/usage/check-compatible-semver-version.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Checking for a Compatible Semver Version
44
In case you need to check if a semver version is compatible
55
with another semver version, use :meth:`Version.is_compatible <semver.Version.is_compatible>`.
66

7-
A version `a` is compatible with another `b` if:
7+
A version `a` is compatible with another version `b` if:
88

9+
* Both are of type :class:`Version <semver.version.Version>`.
910
* If the two majors parts are different, it's incompatible.
1011
* If the two major parts are equal, but the minor of `b` is
1112
lower than `a`, it's incompatible.
@@ -14,7 +15,6 @@ A version `a` is compatible with another `b` if:
1415

1516
Keep in mind, the method *does not* check patches!
1617

17-
For example,
1818

1919
.. code-block:: python
2020
@@ -33,3 +33,23 @@ For example,
3333
>>> b = Version(1, 0, 0) # This is another "B"
3434
>>> a.is_compatible(b)
3535
False
36+
37+
# Checking compatibility between release and pre-release:
38+
>>> b = Version(1,0,0,'rc1.')
39+
>>> a.is_compatible(b)
40+
False
41+
42+
# But pre-release and release:
43+
>>> b.is_compatible(a)
44+
True
45+
46+
All major zero versions are incompatible with anything but itself:
47+
48+
.. code-block:: python
49+
50+
>>> Version(0,1,0).is_compatible(Version(0,1,1))
51+
False
52+
53+
# Only identical versions are compatible for major zero versions:
54+
>>> Version(0,1,0).is_compatible(Version(0,1,0))
55+
True

src/semver/version.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,11 @@ def is_compatible(self, new: 'Version') -> bool:
684684
raise TypeError(
685685
f"Expected a Version type but got {type(new)}"
686686
)
687+
688+
# All major-0 versions should be incompatible with anything but itself
689+
if (0 == self.major == new.major) and (self[:3] != new[:3]):
690+
return False
691+
687692
return (
688693
(self.major == new.major)
689694
and (new.minor >= self.minor)

tests/test_semver.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def test_versioninfo_compare_should_raise_when_passed_invalid_value():
8989
((1, 2, 3), (1, 2, 4)),
9090
((1, 2, 4), (1, 2, 3)),
9191
((1, 2, 3, "rc.0"), (1, 2, 4, "rc.0")),
92-
# ((0, 1, 0, "rc.1"), (0, 0, 0, "rc.2")),
92+
((0, 1, 0), (0, 1, 0)),
93+
# ((1, 0, 0, "rc1"), (1, 0, 0)),
9394
],
9495
)
9596
def test_should_succeed_compatible_match(old, new):
@@ -105,11 +106,14 @@ def test_should_succeed_compatible_match(old, new):
105106
((2, 0, 0), (1, 5, 0)),
106107
((1, 2, 3, "rc.1"), (1, 2, 3, "rc.0")),
107108
((1, 2, 3, "rc.1"), (1, 2, 4, "rc.0")),
109+
((0, 1, 0), (0, 1, 1)),
110+
((1, 0, 0), (1, 0, 0, "rc1")),
108111
]
109112
)
110113
def test_should_fail_compatible_match(old, new):
111114
old = Version(*old)
112-
assert not old.is_compatible(Version(*new))
115+
new = Version(*new)
116+
assert not old.is_compatible(new)
113117

114118

115119
@pytest.mark.parametrize(
@@ -121,6 +125,6 @@ def test_should_fail_compatible_match(old, new):
121125
]
122126
)
123127
def test_should_fail_with_incompatible_type_for_compatible_match(wrongtype):
124-
with pytest.raises(TypeError):
128+
with pytest.raises(TypeError, match="Expected a Version type .*"):
125129
v = Version(1, 2, 3)
126130
v.is_compatible(wrongtype)

0 commit comments

Comments
 (0)