Skip to content
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

raise InvalidMarker for invalid marker #569

Merged
merged 2 commits into from
Apr 1, 2023
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
14 changes: 9 additions & 5 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -313,12 +314,12 @@ 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)
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:
Expand Down Expand Up @@ -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
Expand All @@ -357,7 +356,12 @@ 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 for '{name}': {original_constraint_string}"
) from e

super().__init__(name, parsed_constraint)

Expand Down
25 changes: 25 additions & 0 deletions tests/packages/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 (
Expand Down