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

Skip to content

Support matching 'equal' when no operator is provided #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/pr362.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make :meth:`.Version.match` accept a bare version string as match expression, defaulting to
equality testing.
13 changes: 13 additions & 0 deletions docs/usage/compare-versions-through-expression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ That gives you the following possibilities to express your condition:
True
>>> semver.match("1.0.0", ">1.0.0")
False

If no operator is specified, the match expression is interpreted as a
version to be compared for equality. This allows handling the common
case of version compatibility checking through either an exact version
or a match expression very easy to implement, as the same code will
handle both cases:

.. code-block:: python

>>> semver.match("2.0.0", "2.0.0")
True
>>> semver.match("1.0.0", "3.5.1")
False
7 changes: 6 additions & 1 deletion src/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def match(self, match_expr: str) -> bool:
"""
Compare self to match a match expression.

:param match_expr: operator and version; valid operators are
:param match_expr: optional operator and version; valid operators are
``<``` smaller than
``>`` greater than
``>=`` greator or equal than
Expand All @@ -535,13 +535,18 @@ def match(self, match_expr: str) -> bool:
True
>>> semver.Version.parse("1.0.0").match(">1.0.0")
False
>>> semver.Version.parse("4.0.4").match("4.0.4")
True
"""
prefix = match_expr[:2]
if prefix in (">=", "<=", "==", "!="):
match_version = match_expr[2:]
elif prefix and prefix[0] in (">", "<"):
prefix = prefix[0]
match_version = match_expr[1:]
elif match_expr and match_expr[0] in "0123456789":
prefix = "=="
match_version = match_expr
else:
raise ValueError(
"match_expr parameter should be in format <op><ver>, "
Expand Down
14 changes: 13 additions & 1 deletion tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ def test_should_match_not_equal(left, right, expected):
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",
[
Expand All @@ -49,7 +61,7 @@ def test_should_raise_value_error_for_unexpected_match_expression(left, right):


@pytest.mark.parametrize(
"left,right", [("1.0.0", ""), ("1.0.0", "!"), ("1.0.0", "1.0.0")]
"left,right", [("1.0.0", ""), ("1.0.0", "!")]
)
def test_should_raise_value_error_for_invalid_match_expression(left, right):
with pytest.raises(ValueError):
Expand Down