We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2aeb61b commit b5317afCopy full SHA for b5317af
4 files changed
changelog.d/pr362.feature.rst
@@ -0,0 +1,2 @@
1
+Make :meth:`.Version.match` accept a bare version string as match expression, defaulting to
2
+equality testing.
docs/usage/compare-versions-through-expression.rst
@@ -24,3 +24,16 @@ That gives you the following possibilities to express your condition:
24
True
25
>>> semver.match("1.0.0", ">1.0.0")
26
False
27
+
28
+If no operator is specified, the match expression is interpreted as a
29
+version to be compared for equality. This allows handling the common
30
+case of version compatibility checking through either an exact version
31
+or a match expression very easy to implement, as the same code will
32
+handle both cases:
33
34
+.. code-block:: python
35
36
+ >>> semver.match("2.0.0", "2.0.0")
37
+ True
38
+ >>> semver.match("1.0.0", "3.5.1")
39
+ False
src/semver/version.py
@@ -522,7 +522,7 @@ def match(self, match_expr: str) -> bool:
522
"""
523
Compare self to match a match expression.
524
525
- :param match_expr: operator and version; valid operators are
+ :param match_expr: optional operator and version; valid operators are
526
``<``` smaller than
527
``>`` greater than
528
``>=`` greator or equal than
@@ -535,13 +535,18 @@ def match(self, match_expr: str) -> bool:
535
536
>>> semver.Version.parse("1.0.0").match(">1.0.0")
537
538
+ >>> semver.Version.parse("4.0.4").match("4.0.4")
539
540
541
prefix = match_expr[:2]
542
if prefix in (">=", "<=", "==", "!="):
543
match_version = match_expr[2:]
544
elif prefix and prefix[0] in (">", "<"):
545
prefix = prefix[0]
546
match_version = match_expr[1:]
547
+ elif match_expr and match_expr[0] in "0123456789":
548
+ prefix = "=="
549
+ match_version = match_expr
550
else:
551
raise ValueError(
552
"match_expr parameter should be in format <op><ver>, "
tests/test_match.py
@@ -23,6 +23,18 @@ def test_should_match_not_equal(left, right, expected):
23
assert match(left, right) is expected
+@pytest.mark.parametrize(
+ "left,right,expected",
+ [
+ ("2.3.7", "2.3.7", True),
+ ("2.3.6", "2.3.6", True),
+ ("2.3.7", "4.3.7", False),
+ ],
+)
+def test_should_match_equal_by_default(left, right, expected):
+ assert match(left, right) is expected
@pytest.mark.parametrize(
"left,right,expected",
40
[
@@ -49,7 +61,7 @@ def test_should_raise_value_error_for_unexpected_match_expression(left, right):
49
61
50
62
51
63
52
- "left,right", [("1.0.0", ""), ("1.0.0", "!"), ("1.0.0", "1.0.0")]
64
+ "left,right", [("1.0.0", ""), ("1.0.0", "!")]
53
65
)
54
66
def test_should_raise_value_error_for_invalid_match_expression(left, right):
55
67
with pytest.raises(ValueError):
0 commit comments