Skip to content

Commit 3e16582

Browse files
committed
Addressing invert and __str__
1 parent 6827712 commit 3e16582

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/poetry/core/constraints/generic/parser.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
BASIC_CONSTRAINT = re.compile(r"^(!?==?)?\s*([^\s]+?)\s*$")
19+
STR_CMP_CONSTRAINT = re.compile(r"^(?P<op>(in|not in))(?P<value>(.*))$")
1920

2021

2122
@functools.lru_cache(maxsize=None)
@@ -27,7 +28,7 @@ def parse_constraint(constraints: str) -> BaseConstraint:
2728
or_groups = []
2829
for constraints in or_constraints:
2930
and_constraints = re.split(
30-
r"(?<!^)(?<![=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints
31+
r"(?<!^)(?<![=>< ,(in|not in)]) *(?<!-)[, ](?!-) *(?!,|$)", constraints
3132
)
3233
constraint_objects = []
3334

@@ -53,9 +54,13 @@ def parse_constraint(constraints: str) -> BaseConstraint:
5354

5455

5556
def parse_single_constraint(constraint: str) -> Constraint:
57+
if m := STR_CMP_CONSTRAINT.match(constraint):
58+
op = m.group("op")
59+
value = m.group("value").strip()
60+
return Constraint(value, op)
61+
5662
# Basic comparator
57-
m = BASIC_CONSTRAINT.match(constraint)
58-
if m:
63+
if m := BASIC_CONSTRAINT.match(constraint):
5964
op = m.group(1)
6065
if op is None:
6166
op = "=="

src/poetry/core/version/markers.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ def __init__(
336336
parser: Callable[[str], BaseConstraint | VersionConstraint]
337337
original_constraint_string = constraint_string = str(constraint)
338338

339+
self._str_cmp = str_cmp
339340
# Extract operator and value
340341
m = self._CONSTRAINT_RE.match(constraint_string)
341342
if m is None:
@@ -348,9 +349,11 @@ def __init__(
348349
self._value = m.group(2)
349350
parser = parse_generic_constraint
350351

351-
if name in self._VERSION_LIKE_MARKER_NAME and not (
352-
name == "platform_release" and str_cmp
353-
):
352+
# platform_release has non-version constraints
353+
# when string comparison flag is set
354+
if name == "platform_release" and str_cmp:
355+
pass
356+
elif name in self._VERSION_LIKE_MARKER_NAME:
354357
parser = parse_marker_version_constraint
355358

356359
if self._operator in {"in", "not in"}:
@@ -441,8 +444,10 @@ def invert(self) -> BaseMarker:
441444
else:
442445
# We should never go there
443446
raise RuntimeError(f"Invalid marker operator '{self._operator}'")
444-
445-
return parse_marker(f"{self._name} {operator} '{self._value}'")
447+
constraint = f"{self._name} {operator} '{self._value}'"
448+
if self._str_cmp:
449+
constraint = f'"{self._value}" {operator} {self._name}'
450+
return parse_marker(constraint)
446451

447452
def __eq__(self, other: object) -> bool:
448453
if not isinstance(other, SingleMarker):
@@ -454,6 +459,8 @@ def __hash__(self) -> int:
454459
return hash(self._key)
455460

456461
def __str__(self) -> str:
462+
if self._str_cmp:
463+
return f'"{self._value}" {self._operator} {self._name}'
457464
return f'{self._name} {self._operator} "{self._value}"'
458465

459466
def validate(self, environment: dict[str, Any] | None) -> bool:

tests/version/test_markers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,11 @@ def test_multi_marker_removes_duplicates() -> None:
924924
False,
925925
),
926926
('"tegra" not in platform_release', {"platform_release": "5.10.120"}, True),
927+
(
928+
"platform_machine == 'aarch64' and 'tegra' in platform_release",
929+
{"platform_release": "5.10.120-tegra", "platform_machine": "aarch64"},
930+
True,
931+
),
927932
(
928933
"platform_release != '4.9.253-tegra'",
929934
{"platform_release": "4.9.254-tegra"},

0 commit comments

Comments
 (0)