From 85dcf07a45d430f3d5baa23814749bdddb402917 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Tue, 28 Mar 2023 21:05:50 +0100 Subject: [PATCH 1/2] raise InvalidMarker for invalid marker --- src/poetry/core/version/markers.py | 10 ++++++---- tests/packages/test_dependency.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index 7666c797a..9935ac0db 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -19,6 +19,7 @@ from poetry.core.constraints.generic import MultiConstraint from poetry.core.constraints.generic import UnionConstraint from poetry.core.constraints.version import VersionConstraint +from poetry.core.constraints.version.exceptions import ParseConstraintError from poetry.core.version.grammars import GRAMMAR_PEP_508_MARKERS from poetry.core.version.parser import Parser @@ -313,7 +314,7 @@ def __init__( parsed_constraint: BaseConstraint | VersionConstraint parser: Callable[[str], BaseConstraint | VersionConstraint] - constraint_string = str(constraint) + original_constraint_string = constraint_string = str(constraint) # Extract operator and value m = self._CONSTRAINT_RE.match(constraint_string) @@ -346,9 +347,7 @@ def __init__( if self._operator == "in": glue = " || " - parsed_constraint = parser(glue.join(versions)) - else: - parsed_constraint = parser(constraint_string) + constraint_string = glue.join(versions) else: # if we have a in/not in operator we split the constraint # into a union/multi-constraint of single constraint @@ -357,7 +356,10 @@ def __init__( values = re.split("[ ,]+", self._value) constraint_string = glue.join(f"{op} {value}" for value in values) + try: parsed_constraint = parser(constraint_string) + except ParseConstraintError as e: + raise InvalidMarker(f"Invalid marker: {original_constraint_string}") from e super().__init__(name, parsed_constraint) diff --git a/tests/packages/test_dependency.py b/tests/packages/test_dependency.py index b218444c5..4aac41092 100644 --- a/tests/packages/test_dependency.py +++ b/tests/packages/test_dependency.py @@ -6,7 +6,9 @@ from poetry.core.constraints.version.exceptions import ParseConstraintError from poetry.core.packages.dependency import Dependency +from poetry.core.version.markers import InvalidMarker from poetry.core.version.markers import parse_marker +from poetry.core.version.requirements import InvalidRequirement @pytest.mark.parametrize( @@ -184,6 +186,29 @@ def test_to_pep_508_combination() -> None: assert dependency.to_pep_508() == "foo (>=1.2,<1.3,!=1.2.5)" +@pytest.mark.parametrize( + "requirement", + [ + "enum34; extra == ':python_version < \"3.4\"'", + "enum34; extra == \":python_version < '3.4'\"", + ], +) +def test_to_pep_508_with_invalid_marker(requirement: str) -> None: + with pytest.raises(InvalidMarker): + _ = Dependency.create_from_pep_508(requirement) + + +@pytest.mark.parametrize( + "requirement", + [ + 'enum34; extra == ":python_version < "3.4""', + ], +) +def test_to_pep_508_with_invalid_requirement(requirement: str) -> None: + with pytest.raises(InvalidRequirement): + _ = Dependency.create_from_pep_508(requirement) + + def test_complete_name() -> None: assert Dependency("foo", ">=1.2.3").complete_name == "foo" assert ( From 964791ee38abd368c74337fc5b30707e5cc18baf Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 1 Apr 2023 14:36:17 +0100 Subject: [PATCH 2/2] improved error message --- src/poetry/core/version/markers.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/poetry/core/version/markers.py b/src/poetry/core/version/markers.py index 9935ac0db..99c10f106 100644 --- a/src/poetry/core/version/markers.py +++ b/src/poetry/core/version/markers.py @@ -319,7 +319,7 @@ def __init__( # Extract operator and value m = self._CONSTRAINT_RE.match(constraint_string) if m is None: - raise InvalidMarker(f"Invalid marker '{constraint_string}'") + raise InvalidMarker(f"Invalid marker for '{name}': {constraint_string}") self._operator = m.group(1) if self._operator is None: @@ -359,7 +359,9 @@ def __init__( try: parsed_constraint = parser(constraint_string) except ParseConstraintError as e: - raise InvalidMarker(f"Invalid marker: {original_constraint_string}") from e + raise InvalidMarker( + f"Invalid marker for '{name}': {original_constraint_string}" + ) from e super().__init__(name, parsed_constraint)