Skip to content
Closed
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
8 changes: 6 additions & 2 deletions src/packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ._tokenizer import ParserSyntaxError
from .specifiers import InvalidSpecifier, Specifier
from .utils import canonicalize_name
from .version import Version

__all__ = [
"EvaluateContext",
Expand Down Expand Up @@ -177,13 +178,16 @@ def _format_marker(
}


def _eval_op(lhs: str, op: Op, rhs: str | AbstractSet[str]) -> bool:
def _eval_op(lhs: str, op: Op, rhs: str | AbstractSet[str], *, key: str) -> bool:
# This is here to avoid a behavior change while the spec is being debated.
if isinstance(rhs, str):
try:
spec = Specifier(f"{op.serialize()}{rhs}")
except InvalidSpecifier:
pass
else:
if key == "platform_release":
Version(lhs)
return spec.contains(lhs, prereleases=True)

oper: Operator | None = _operators.get(op.serialize())
Expand Down Expand Up @@ -236,7 +240,7 @@ def _evaluate_markers(
rhs_value = environment[environment_key]
assert isinstance(lhs_value, str), "lhs must be a string"
lhs_value, rhs_value = _normalize(lhs_value, rhs_value, key=environment_key)
groups[-1].append(_eval_op(lhs_value, op, rhs_value))
groups[-1].append(_eval_op(lhs_value, op, rhs_value, key=environment_key))
else:
assert marker in ["and", "or"]
if marker == "or":
Expand Down
2 changes: 1 addition & 1 deletion src/packaging/specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Specifier(BaseSpecifier):
)
|
(?:
# All other operators only allow a sub set of what the
# All other operators only allow a subset of what the
# (non)equality operators do. Specifically they do not allow
# local versions to be specified nor do they allow the prefix
# matching wild cards.
Expand Down
17 changes: 15 additions & 2 deletions tests/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
default_environment,
format_full_version,
)
from packaging.version import InvalidVersion

VARIABLES = [
"extra",
Expand Down Expand Up @@ -308,6 +309,11 @@ def test_environment_with_extra_none(self) -> None:
{"os_name": "other", "python_version": "2.7.4"},
False,
),
(
"platform_release >= '6'",
{"platform_release": "6.1-foobar"},
InvalidVersion,
),
("extra == 'security'", {"extra": "quux"}, False),
("extra == 'security'", {"extra": "security"}, True),
("extra == 'SECURITY'", {"extra": "security"}, True),
Expand All @@ -321,10 +327,17 @@ def test_environment_with_extra_none(self) -> None:
],
)
def test_evaluates(
self, marker_string: str, environment: dict[str, str] | None, expected: bool
self,
marker_string: str,
environment: dict[str, str] | None,
expected: bool | type[Exception],
) -> None:
args = () if environment is None else (environment,)
assert Marker(marker_string).evaluate(*args) == expected
if isinstance(expected, bool):
assert Marker(marker_string).evaluate(*args) == expected
else:
with pytest.raises(expected):
Marker(marker_string).evaluate(*args)

@pytest.mark.parametrize(
"marker_string",
Expand Down
Loading