Skip to content

Commit caa142f

Browse files
committed
raise InvalidMarker for invalid marker
1 parent e33e9d1 commit caa142f

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/poetry/core/version/markers.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import Iterable
1313

1414
from poetry.core.constraints.version import VersionConstraint
15+
from poetry.core.constraints.version.exceptions import ParseConstraintError
1516
from poetry.core.version.grammars import GRAMMAR_PEP_508_MARKERS
1617
from poetry.core.version.parser import Parser
1718

@@ -212,12 +213,12 @@ def __init__(
212213
self._constraint: BaseConstraint | VersionConstraint
213214
self._parser: Callable[[str], BaseConstraint | VersionConstraint]
214215
self._name = ALIASES.get(name, name)
215-
constraint_string = str(constraint)
216+
original_constraint_string = constraint_string = str(constraint)
216217

217218
# Extract operator and value
218219
m = self._CONSTRAINT_RE.match(constraint_string)
219220
if m is None:
220-
raise InvalidMarker(f"Invalid marker '{constraint_string}'")
221+
raise InvalidMarker(f"Invalid marker: {constraint_string}")
221222

222223
self._operator = m.group(1)
223224
if self._operator is None:
@@ -245,9 +246,7 @@ def __init__(
245246
if self._operator == "in":
246247
glue = " || "
247248

248-
self._constraint = self._parser(glue.join(versions))
249-
else:
250-
self._constraint = self._parser(constraint_string)
249+
constraint_string = glue.join(versions)
251250
else:
252251
# if we have a in/not in operator we split the constraint
253252
# into a union/multi-constraint of single constraint
@@ -256,7 +255,10 @@ def __init__(
256255
values = re.split("[ ,]+", self._value)
257256
constraint_string = glue.join(f"{op} {value}" for value in values)
258257

258+
try:
259259
self._constraint = self._parser(constraint_string)
260+
except ParseConstraintError as e:
261+
raise InvalidMarker(f"Invalid marker: {original_constraint_string}") from e
260262

261263
@property
262264
def name(self) -> str:

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)