Skip to content

Commit

Permalink
Lazily evaluate markers to mitigate non-PEP 440 version errors
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardpeek-crown-public committed Sep 19, 2024
1 parent cf2cbe2 commit 9167919
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

import functools
import operator
import os
import platform
Expand Down Expand Up @@ -202,13 +203,14 @@ def _normalize(*values: str, key: str) -> tuple[str, ...]:


def _evaluate_markers(markers: MarkerList, environment: dict[str, str]) -> bool:
groups: list[list[bool]] = [[]]
# Lazy evaluation to mitigate https://github.com/pypa/packaging/issues/774
groups: list[list[Callable[[], bool]]] = [[]]

for marker in markers:
assert isinstance(marker, (list, tuple, str))

if isinstance(marker, list):
groups[-1].append(_evaluate_markers(marker, environment))
groups[-1].append(functools.partial(_evaluate_markers, marker, environment))
elif isinstance(marker, tuple):
lhs, op, rhs = marker

Expand All @@ -222,13 +224,13 @@ def _evaluate_markers(markers: MarkerList, environment: dict[str, str]) -> bool:
rhs_value = environment[environment_key]

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(functools.partial(_eval_op, lhs_value, op, rhs_value))
else:
assert marker in ["and", "or"]
if marker == "or":
groups.append([])

return any(all(item) for item in groups)
return any(all(expr() for expr in group) for group in groups)


def format_full_version(info: sys._version_info) -> str:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ def test_environment_with_extra_none(self):
{"extra": "different__punctuation_is_EQUAL"},
True,
),
(
"sys_platform == 'foo_os' and platform_release >= '4.5.6'",
{"sys_platform": "bar_os", "platform_release": "1.2.3-invalid"},
False,
)
],
)
def test_evaluates(self, marker_string, environment, expected):
Expand Down

0 comments on commit 9167919

Please sign in to comment.