Skip to content

Commit

Permalink
use intersect() and union() at marker parsing
Browse files Browse the repository at this point in the history
Markers are now always parsed into DNF, and as simplified as we can
manage
  • Loading branch information
dimbleby committed Nov 29, 2022
1 parent 984ebfa commit 0a6d286
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 deletions.
36 changes: 13 additions & 23 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,18 +706,18 @@ def parse_marker(marker: str) -> BaseMarker:
def _compact_markers(tree_elements: Tree, tree_prefix: str = "") -> BaseMarker:
from lark import Token

groups: list[BaseMarker] = [MultiMarker()]
groups: list[BaseMarker] = [AnyMarker()]
for token in tree_elements:
if isinstance(token, Token):
if token.type == f"{tree_prefix}BOOL_OP" and token.value == "or":
groups.append(MultiMarker())
groups.append(AnyMarker())

continue

if token.data == "marker":
groups[-1] = MultiMarker.of(
groups[-1], _compact_markers(token.children, tree_prefix=tree_prefix)
)
sub_marker = _compact_markers(token.children, tree_prefix=tree_prefix)
groups[-1] = groups[-1].intersect(sub_marker)

elif token.data == f"{tree_prefix}item":
name, op, value = token.children
if value.type == f"{tree_prefix}MARKER_NAME":
Expand All @@ -727,27 +727,17 @@ def _compact_markers(tree_elements: Tree, tree_prefix: str = "") -> BaseMarker:
)

value = value[1:-1]
groups[-1] = MultiMarker.of(
groups[-1], SingleMarker(str(name), f"{op}{value}")
)
elif token.data == f"{tree_prefix}BOOL_OP" and token.children[0] == "or":
groups.append(MultiMarker())
sub_marker = SingleMarker(str(name), f"{op}{value}")
groups[-1] = groups[-1].intersect(sub_marker)

for i, group in enumerate(reversed(groups)):
if group.is_empty():
del groups[len(groups) - 1 - i]
continue

if isinstance(group, MultiMarker) and len(group.markers) == 1:
groups[len(groups) - 1 - i] = group.markers[0]

if not groups:
return EmptyMarker()
elif token.data == f"{tree_prefix}BOOL_OP" and token.children[0] == "or":
groups.append(AnyMarker())

if len(groups) == 1:
return groups[0]
result = groups.pop(0)
for group in groups:
result = result.union(group)

return MarkerUnion.of(*groups)
return result


def cnf(marker: BaseMarker) -> BaseMarker:
Expand Down
10 changes: 5 additions & 5 deletions tests/packages/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ def test_dependency_from_pep_508_complex() -> None:
assert dep.python_versions == ">=2.7 !=3.2.*"
assert (
str(dep.marker)
== 'python_version >= "2.7" and python_version != "3.2" '
'and (sys_platform == "win32" or sys_platform == "darwin") '
'and extra == "foo"'
== 'python_version >= "2.7" and python_version != "3.2" and sys_platform =='
' "win32" and extra == "foo" or python_version >= "2.7" and python_version'
' != "3.2" and sys_platform == "darwin" and extra == "foo"'
)


Expand Down Expand Up @@ -278,11 +278,11 @@ def test_dependency_from_pep_508_with_python_full_version() -> None:
assert dep.name == "requests"
assert str(dep.constraint) == "2.18.0"
assert dep.extras == frozenset()
assert dep.python_versions == ">=2.7 <2.8 || >=3.4 <3.5.4"
assert dep.python_versions == ">=2.7 <2.8 || >=3.4.0 <3.5.4"
assert (
str(dep.marker)
== 'python_version >= "2.7" and python_version < "2.8" '
'or python_full_version >= "3.4" and python_full_version < "3.5.4"'
'or python_full_version >= "3.4.0" and python_full_version < "3.5.4"'
)


Expand Down
4 changes: 2 additions & 2 deletions tests/packages/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
{
"python_version": [
[("<", "3.6")],
[("<", "3.6"), (">=", "3.3")],
[("<", "3.3")],
[("<", "3.6"), (">=", "3.3")],
],
"sys_platform": [
[("==", "win32")],
Expand All @@ -42,7 +42,7 @@
' sys_platform == "win32" and python_version < "3.3"'
),
{
"python_version": [[("<", "3.6")], [("<", "3.3")]],
"python_version": [[("<", "3.6")]],
"sys_platform": [[("==", "win32")]],
},
),
Expand Down
8 changes: 4 additions & 4 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ def test_parse_version_like_markers(marker: str, env: dict[str, str]) -> None:
'python_version >= "3.6" or extra == "foo" and implementation_name =='
' "pypy" or extra == "bar"'
),
'python_version >= "3.6" or implementation_name == "pypy"',
'implementation_name == "pypy" or python_version >= "3.6"',
),
('extra == "foo"', ""),
('extra == "foo" or extra == "bar"', ""),
Expand Down Expand Up @@ -963,7 +963,7 @@ def test_without_extras(marker: str, expected: str) -> None:
' implementation_name == "pypy"'
),
"python_version",
'extra == "foo" or extra == "bar" or implementation_name == "pypy"',
'extra == "foo" or implementation_name == "pypy" or extra == "bar"',
),
(
(
Expand All @@ -979,7 +979,7 @@ def test_without_extras(marker: str, expected: str) -> None:
' "pypy" or extra == "bar"'
),
"implementation_name",
'python_version >= "3.6" or extra == "foo" or extra == "bar"',
'extra == "foo" or python_version >= "3.6" or extra == "bar"',
),
(
'extra == "foo" and python_version >= "3.6" or python_version >= "3.6"',
Expand Down Expand Up @@ -1042,7 +1042,7 @@ def test_exclude(marker: str, excluded: str, expected: str) -> None:
' "pypy" or extra == "bar"'
),
["implementation_name", "python_version"],
'python_version >= "3.6" or implementation_name == "pypy"',
'implementation_name == "pypy" or python_version >= "3.6"',
),
],
)
Expand Down

0 comments on commit 0a6d286

Please sign in to comment.