From e6113357f724b29016dde73237a80a7ebe9d4f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Miguel=20S=C3=A1nchez=20Garc=C3=ADa?= Date: Sun, 3 Jul 2022 09:58:47 +0000 Subject: [PATCH 1/3] Support matching 'equal' when no operator is provided --- src/semver/version.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/semver/version.py b/src/semver/version.py index 096acdf2..34eb51e0 100644 --- a/src/semver/version.py +++ b/src/semver/version.py @@ -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 @@ -535,6 +535,8 @@ 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 (">=", "<=", "==", "!="): @@ -542,6 +544,9 @@ def match(self, match_expr: str) -> bool: 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 , " From b1363b996c1a9bcea42508370b889124af9c4740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Miguel=20S=C3=A1nchez=20Garc=C3=ADa?= Date: Sun, 3 Jul 2022 10:23:17 +0000 Subject: [PATCH 2/3] Add tests for new default equality match behavior --- tests/test_match.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_match.py b/tests/test_match.py index b4cc50cc..2476eb01 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -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", [ @@ -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): From 5a3f23dae297a4b69ac2fa984074bf7ed7fbd79c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Miguel=20S=C3=A1nchez=20Garc=C3=ADa?= Date: Sun, 3 Jul 2022 19:35:43 +0000 Subject: [PATCH 3/3] Change documentation and add changelog --- changelog.d/pr362.feature.rst | 2 ++ docs/usage/compare-versions-through-expression.rst | 13 +++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 changelog.d/pr362.feature.rst diff --git a/changelog.d/pr362.feature.rst b/changelog.d/pr362.feature.rst new file mode 100644 index 00000000..1b7cc120 --- /dev/null +++ b/changelog.d/pr362.feature.rst @@ -0,0 +1,2 @@ +Make :meth:`.Version.match` accept a bare version string as match expression, defaulting to +equality testing. diff --git a/docs/usage/compare-versions-through-expression.rst b/docs/usage/compare-versions-through-expression.rst index 43a5a182..28fad671 100644 --- a/docs/usage/compare-versions-through-expression.rst +++ b/docs/usage/compare-versions-through-expression.rst @@ -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 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