Skip to content

Commit

Permalink
Simplify parsed markers exactly once
Browse files Browse the repository at this point in the history
No need to do it in every recursive sub-call
  • Loading branch information
dimbleby committed Nov 30, 2022
1 parent 3cee6d9 commit f6ce707
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,9 @@ def parse_marker(marker: str) -> BaseMarker:
return markers


def _compact_markers(tree_elements: Tree, tree_prefix: str = "") -> BaseMarker:
def _compact_markers(
tree_elements: Tree, tree_prefix: str = "", top_level: bool = True
) -> BaseMarker:
from lark import Token

# groups is a disjunction of conjunctions
Expand All @@ -711,7 +713,9 @@ def _compact_markers(tree_elements: Tree, tree_prefix: str = "") -> BaseMarker:
continue

if token.data == "marker":
sub_marker = _compact_markers(token.children, tree_prefix=tree_prefix)
sub_marker = _compact_markers(
token.children, tree_prefix=tree_prefix, top_level=False
)
groups[-1].append(sub_marker)

elif token.data == f"{tree_prefix}item":
Expand All @@ -729,10 +733,15 @@ def _compact_markers(tree_elements: Tree, tree_prefix: str = "") -> BaseMarker:
elif token.data == f"{tree_prefix}BOOL_OP" and token.children[0] == "or":
groups.append([])

# Combine and simplify the groups.
# Combine the groups.
sub_markers = [MultiMarker(*group) for group in groups]
union = MarkerUnion(*sub_markers)

# This function calls itself recursively. In the inner calls we don't perform any
# simplification, instead doing it all only when we have the complete marker.
if not top_level:
return union

conjunction = cnf(union)
if not isinstance(conjunction, MultiMarker):
return conjunction
Expand Down

0 comments on commit f6ce707

Please sign in to comment.