Skip to content

Commit 09fa2f0

Browse files
authored
raise InvalidMarker for invalid marker (#569)
1 parent 62f5708 commit 09fa2f0

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/poetry/core/version/markers.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from poetry.core.constraints.generic import MultiConstraint
2020
from poetry.core.constraints.generic import UnionConstraint
2121
from poetry.core.constraints.version import VersionConstraint
22+
from poetry.core.constraints.version.exceptions import ParseConstraintError
2223
from poetry.core.version.grammars import GRAMMAR_PEP_508_MARKERS
2324
from poetry.core.version.parser import Parser
2425

@@ -313,12 +314,12 @@ def __init__(
313314

314315
parsed_constraint: BaseConstraint | VersionConstraint
315316
parser: Callable[[str], BaseConstraint | VersionConstraint]
316-
constraint_string = str(constraint)
317+
original_constraint_string = constraint_string = str(constraint)
317318

318319
# Extract operator and value
319320
m = self._CONSTRAINT_RE.match(constraint_string)
320321
if m is None:
321-
raise InvalidMarker(f"Invalid marker '{constraint_string}'")
322+
raise InvalidMarker(f"Invalid marker for '{name}': {constraint_string}")
322323

323324
self._operator = m.group(1)
324325
if self._operator is None:
@@ -346,9 +347,7 @@ def __init__(
346347
if self._operator == "in":
347348
glue = " || "
348349

349-
parsed_constraint = parser(glue.join(versions))
350-
else:
351-
parsed_constraint = parser(constraint_string)
350+
constraint_string = glue.join(versions)
352351
else:
353352
# if we have a in/not in operator we split the constraint
354353
# into a union/multi-constraint of single constraint
@@ -357,7 +356,12 @@ def __init__(
357356
values = re.split("[ ,]+", self._value)
358357
constraint_string = glue.join(f"{op} {value}" for value in values)
359358

359+
try:
360360
parsed_constraint = parser(constraint_string)
361+
except ParseConstraintError as e:
362+
raise InvalidMarker(
363+
f"Invalid marker for '{name}': {original_constraint_string}"
364+
) from e
361365

362366
super().__init__(name, parsed_constraint)
363367

tests/packages/test_dependency.py

+25
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
from poetry.core.constraints.version.exceptions import ParseConstraintError
88
from poetry.core.packages.dependency import Dependency
9+
from poetry.core.version.markers import InvalidMarker
910
from poetry.core.version.markers import parse_marker
11+
from poetry.core.version.requirements import InvalidRequirement
1012

1113

1214
@pytest.mark.parametrize(
@@ -184,6 +186,29 @@ def test_to_pep_508_combination() -> None:
184186
assert dependency.to_pep_508() == "foo (>=1.2,<1.3,!=1.2.5)"
185187

186188

189+
@pytest.mark.parametrize(
190+
"requirement",
191+
[
192+
"enum34; extra == ':python_version < \"3.4\"'",
193+
"enum34; extra == \":python_version < '3.4'\"",
194+
],
195+
)
196+
def test_to_pep_508_with_invalid_marker(requirement: str) -> None:
197+
with pytest.raises(InvalidMarker):
198+
_ = Dependency.create_from_pep_508(requirement)
199+
200+
201+
@pytest.mark.parametrize(
202+
"requirement",
203+
[
204+
'enum34; extra == ":python_version < "3.4""',
205+
],
206+
)
207+
def test_to_pep_508_with_invalid_requirement(requirement: str) -> None:
208+
with pytest.raises(InvalidRequirement):
209+
_ = Dependency.create_from_pep_508(requirement)
210+
211+
187212
def test_complete_name() -> None:
188213
assert Dependency("foo", ">=1.2.3").complete_name == "foo"
189214
assert (

0 commit comments

Comments
 (0)