Skip to content

Commit

Permalink
Merge pull request #189 from python-poetry/1.0-fix-evaluation-of-in-m…
Browse files Browse the repository at this point in the history
…arkers

[1.0] Fix the evaluation of `in/not in` markers
  • Loading branch information
sdispater authored Aug 19, 2021
2 parents 992f86f + f69efc6 commit a2f1ab8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install and set up Poetry
run: |
curl -fsS -o get-poetry.py https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py
python get-poetry.py --preview -y
python get-poetry.py -y
- name: Build distributions
run: |
source $HOME/.poetry/env
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
shell: bash
run: |
curl -fsS -o get-poetry.py https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py
python get-poetry.py --preview -y
python get-poetry.py -y
echo "$HOME/.poetry/bin" >> $GITHUB_PATH
- name: Configure poetry
Expand Down
3 changes: 3 additions & 0 deletions poetry/core/packages/constraints/base_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@


class BaseConstraint(object):
def allows(self, other): # type: ("ConstraintTypes") -> bool
raise NotImplementedError

def allows_all(self, other): # type: ("ConstraintTypes") -> bool
raise NotImplementedError()

Expand Down
3 changes: 3 additions & 0 deletions poetry/core/packages/constraints/empty_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def matches(self, _): # type: ("ConstraintTypes") -> bool
def is_empty(self): # type: () -> bool
return True

def allows(self, other): # type: ("ConstraintTypes") -> bool
return False

def allows_all(self, other): # type: ("ConstraintTypes") -> bool
return True

Expand Down
12 changes: 11 additions & 1 deletion poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,17 @@ def __init__(
else:
self._constraint = self._parser(self._constraint_string)
else:
self._constraint = self._parser(self._constraint_string)
# if we have a in/not in operator we split the constraint
# into a union/multi-constraint of single constraint
constraint_string = self._constraint_string
if self._operator in {"in", "not in"}:
op, glue = ("==", " || ") if self._operator == "in" else ("!=", ", ")
values = re.split("[ ,]+", self._value)
constraint_string = glue.join(
("{} {}".format(op, value) for value in values)
)

self._constraint = self._parser(constraint_string)

@property
def name(self): # type: () -> str
Expand Down
48 changes: 48 additions & 0 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ def test_single_marker():
assert m.constraint_string == "not in 2.7, 3.0, 3.1"
assert str(m.constraint) == "<2.7.0 || >=2.8.0,<3.0.0 || >=3.2.0"

m = parse_marker(
"platform_machine in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'"
)

assert isinstance(m, SingleMarker)
assert m.name == "platform_machine"
assert (
m.constraint_string
== "in x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32"
)
assert str(m.constraint) == (
"x86_64 || X86_64 || aarch64 || AARCH64 || ppc64le || PPC64LE || amd64 || AMD64 || win32 || WIN32"
)

m = parse_marker(
"platform_machine not in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'"
)

assert isinstance(m, SingleMarker)
assert m.name == "platform_machine"
assert (
m.constraint_string
== "not in x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32"
)
assert str(m.constraint) == (
"!=x86_64, !=X86_64, !=aarch64, !=AARCH64, !=ppc64le, !=PPC64LE, !=amd64, !=AMD64, !=win32, !=WIN32"
)


def test_single_marker_intersect():
m = parse_marker('sys_platform == "darwin"')
Expand Down Expand Up @@ -476,6 +504,26 @@ def test_multi_marker_removes_duplicates():
{"python_version": "2.7"},
False,
),
(
"platform_machine in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'",
{"platform_machine": "foo"},
False,
),
(
"platform_machine in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'",
{"platform_machine": "x86_64"},
True,
),
(
"platform_machine not in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'",
{"platform_machine": "foo"},
True,
),
(
"platform_machine not in 'x86_64 X86_64 aarch64 AARCH64 ppc64le PPC64LE amd64 AMD64 win32 WIN32'",
{"platform_machine": "x86_64"},
False,
),
],
)
def test_validate(marker_string, environment, expected):
Expand Down

0 comments on commit a2f1ab8

Please sign in to comment.