Skip to content

Commit f42cf38

Browse files
committed
Fix the evaluation of in/not in markers
Resolves python-poetry/poetry#4402
1 parent 5812d6d commit f42cf38

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

poetry/core/packages/constraints/base_constraint.py

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77

88
class BaseConstraint(object):
9+
def allows(self, other: "ConstraintTypes") -> bool:
10+
raise NotImplementedError()
11+
912
def allows_all(self, other: "ConstraintTypes") -> bool:
1013
raise NotImplementedError()
1114

poetry/core/packages/constraints/empty_constraint.py

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def matches(self, _: "ConstraintTypes") -> bool:
1717
def is_empty(self) -> bool:
1818
return True
1919

20+
def allows(self, other: "ConstraintTypes") -> bool:
21+
return False
22+
2023
def allows_all(self, other: "ConstraintTypes") -> bool:
2124
return True
2225

poetry/core/version/markers.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,17 @@ def __init__(self, name: str, constraint: Union[str, "VersionTypes"]) -> None:
224224
else:
225225
self._constraint = self._parser(self._constraint_string)
226226
else:
227-
self._constraint = self._parser(self._constraint_string)
227+
# if we have a in/not in operator we split the constraint
228+
# into a union/multi-constraint of single constraint
229+
constraint_string = self._constraint_string
230+
if self._operator in {"in", "not in"}:
231+
op, glue = ("==", " || ") if self._operator == "in" else ("!=", ", ")
232+
values = re.split("[ ,]+", self._value)
233+
constraint_string = glue.join(
234+
("{} {}".format(op, value) for value in values)
235+
)
236+
237+
self._constraint = self._parser(constraint_string)
228238

229239
@property
230240
def name(self) -> str:

tests/version/test_markers.py

+48
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ def test_single_marker():
3636
assert m.constraint_string == "not in 2.7, 3.0, 3.1"
3737
assert str(m.constraint) == "<2.7.0 || >=2.8.0,<3.0.0 || >=3.2.0"
3838

39+
m = parse_marker(
40+
"platform_machine in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'"
41+
)
42+
43+
assert isinstance(m, SingleMarker)
44+
assert m.name == "platform_machine"
45+
assert (
46+
m.constraint_string
47+
== "in x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32"
48+
)
49+
assert str(m.constraint) == (
50+
"x86_64 || X86_64 || aarch64 || AARCH64 || ppc64le || PPC64LE || amd64 || AMD64 || win32 || WIN32"
51+
)
52+
53+
m = parse_marker(
54+
"platform_machine not in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'"
55+
)
56+
57+
assert isinstance(m, SingleMarker)
58+
assert m.name == "platform_machine"
59+
assert (
60+
m.constraint_string
61+
== "not in x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32"
62+
)
63+
assert str(m.constraint) == (
64+
"!=x86_64, !=X86_64, !=aarch64, !=AARCH64, !=ppc64le, !=PPC64LE, !=amd64, !=AMD64, !=win32, !=WIN32"
65+
)
66+
3967

4068
def test_single_marker_intersect():
4169
m = parse_marker('sys_platform == "darwin"')
@@ -476,6 +504,26 @@ def test_multi_marker_removes_duplicates():
476504
{"python_version": "2.7"},
477505
False,
478506
),
507+
(
508+
"platform_machine in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'",
509+
{"platform_machine": "foo"},
510+
False,
511+
),
512+
(
513+
"platform_machine in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'",
514+
{"platform_machine": "x86_64"},
515+
True,
516+
),
517+
(
518+
"platform_machine not in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'",
519+
{"platform_machine": "foo"},
520+
True,
521+
),
522+
(
523+
"platform_machine not in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'",
524+
{"platform_machine": "x86_64"},
525+
False,
526+
),
479527
],
480528
)
481529
def test_validate(marker_string, environment, expected):

0 commit comments

Comments
 (0)