Skip to content

Commit b5317af

Browse files
authored
Support matching 'equal' when no operator is provided (#362)
* Add tests for new default equality match behavior * Change documentation and add changelog
1 parent 2aeb61b commit b5317af

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

changelog.d/pr362.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,16 @@ That gives you the following possibilities to express your condition:
2424
True
2525
>>> semver.match("1.0.0", ">1.0.0")
2626
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def match(self, match_expr: str) -> bool:
522522
"""
523523
Compare self to match a match expression.
524524
525-
:param match_expr: operator and version; valid operators are
525+
:param match_expr: optional operator and version; valid operators are
526526
``<``` smaller than
527527
``>`` greater than
528528
``>=`` greator or equal than
@@ -535,13 +535,18 @@ def match(self, match_expr: str) -> bool:
535535
True
536536
>>> semver.Version.parse("1.0.0").match(">1.0.0")
537537
False
538+
>>> semver.Version.parse("4.0.4").match("4.0.4")
539+
True
538540
"""
539541
prefix = match_expr[:2]
540542
if prefix in (">=", "<=", "==", "!="):
541543
match_version = match_expr[2:]
542544
elif prefix and prefix[0] in (">", "<"):
543545
prefix = prefix[0]
544546
match_version = match_expr[1:]
547+
elif match_expr and match_expr[0] in "0123456789":
548+
prefix = "=="
549+
match_version = match_expr
545550
else:
546551
raise ValueError(
547552
"match_expr parameter should be in format <op><ver>, "

tests/test_match.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ def test_should_match_not_equal(left, right, expected):
2323
assert match(left, right) is expected
2424

2525

26+
@pytest.mark.parametrize(
27+
"left,right,expected",
28+
[
29+
("2.3.7", "2.3.7", True),
30+
("2.3.6", "2.3.6", True),
31+
("2.3.7", "4.3.7", False),
32+
],
33+
)
34+
def test_should_match_equal_by_default(left, right, expected):
35+
assert match(left, right) is expected
36+
37+
2638
@pytest.mark.parametrize(
2739
"left,right,expected",
2840
[
@@ -49,7 +61,7 @@ def test_should_raise_value_error_for_unexpected_match_expression(left, right):
4961

5062

5163
@pytest.mark.parametrize(
52-
"left,right", [("1.0.0", ""), ("1.0.0", "!"), ("1.0.0", "1.0.0")]
64+
"left,right", [("1.0.0", ""), ("1.0.0", "!")]
5365
)
5466
def test_should_raise_value_error_for_invalid_match_expression(left, right):
5567
with pytest.raises(ValueError):

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