Skip to content

Commit

Permalink
merging python_version and python_full_version markers
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby authored and neersighted committed Feb 28, 2022
1 parent 15972bc commit dd7b2fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class UndefinedEnvironmentName(ValueError):
"python_implementation": "platform_python_implementation",
}

PYTHON_VERSION_MARKERS = ["python_version", "python_full_version"]

# Parser: PEP 508 Environment Markers
_parser = Parser(GRAMMAR_PEP_508_MARKERS, "lalr")
Expand Down Expand Up @@ -259,22 +260,6 @@ def value(self) -> str:

def intersect(self, other: MarkerTypes) -> MarkerTypes:
if isinstance(other, SingleMarker):
if other.name != self.name:
return MultiMarker(self, other)

if self == other:
return self

if self._operator in {"in", "not in"} or other.operator in {"in", "not in"}:
return MultiMarker.of(self, other)

new_constraint = self._constraint.intersect(other.constraint)
if new_constraint.is_empty():
return EmptyMarker()

if new_constraint == self._constraint or new_constraint == other.constraint:
return SingleMarker(self._name, new_constraint)

return MultiMarker.of(self, other)

return other.intersect(self)
Expand Down Expand Up @@ -416,7 +401,13 @@ def of(cls, *markers: MarkerTypes) -> MarkerTypes:
if isinstance(marker, SingleMarker):
intersected = False
for i, mark in enumerate(new_markers):
if isinstance(mark, SingleMarker) and mark.name == marker.name:
if isinstance(mark, SingleMarker) and (
mark.name == marker.name
or (
mark.name in PYTHON_VERSION_MARKERS
and marker.name in PYTHON_VERSION_MARKERS
)
):
intersection = mark.constraint.intersect(marker.constraint)
if intersection == mark.constraint:
intersected = True
Expand Down Expand Up @@ -560,13 +551,15 @@ def of(cls, *markers: BaseMarker) -> MarkerTypes:
if marker in markers:
continue

if isinstance(marker, SingleMarker) and marker.name == "python_version":
if (
isinstance(marker, SingleMarker)
and marker.name in PYTHON_VERSION_MARKERS
):
included = False
for i, mark in enumerate(markers):
if (
not isinstance(mark, SingleMarker)
or isinstance(mark, SingleMarker)
and mark.name != marker.name
or mark.name not in PYTHON_VERSION_MARKERS
):
continue

Expand Down
16 changes: 16 additions & 0 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ def test_single_marker_not_in_python_intersection():
assert str(intersection) == 'python_version not in "2.7, 3.0, 3.1, 3.2"'


def test_marker_intersection_of_python_version_and_python_full_version():
m = parse_marker('python_version >= "3.6"')
m2 = parse_marker('python_full_version >= "3.0.0"')
intersection = m.intersect(m2)

assert str(intersection) == 'python_version >= "3.6"'


def test_single_marker_union():
m = parse_marker('sys_platform == "darwin"')

Expand Down Expand Up @@ -369,6 +377,14 @@ def test_marker_union_deduplicate():
assert str(m) == 'sys_platform == "darwin" or implementation_name == "cpython"'


def test_marker_union_of_python_version_and_python_full_version():
m = parse_marker('python_version >= "3.6"')
m2 = parse_marker('python_full_version >= "3.0.0"')
union = m.union(m2)

assert str(union) == 'python_full_version >= "3.0.0"'


def test_marker_union_intersect_single_marker():
m = parse_marker('sys_platform == "darwin" or python_version < "3.4"')

Expand Down

0 comments on commit dd7b2fe

Please sign in to comment.