|
20 | 20 | from poetry.core.constraints.generic import MultiConstraint |
21 | 21 | from poetry.core.constraints.generic import UnionConstraint |
22 | 22 | from poetry.core.constraints.version import VersionConstraint |
| 23 | +from poetry.core.constraints.version import VersionUnion |
23 | 24 | from poetry.core.constraints.version.exceptions import ParseConstraintError |
24 | 25 | from poetry.core.version.grammars import GRAMMAR_PEP_508_MARKERS |
25 | 26 | from poetry.core.version.parser import Parser |
@@ -107,6 +108,12 @@ def exclude(self, marker_name: str) -> BaseMarker: |
107 | 108 | def only(self, *marker_names: str) -> BaseMarker: |
108 | 109 | raise NotImplementedError |
109 | 110 |
|
| 111 | + @abstractmethod |
| 112 | + def reduce_by_python_constraint( |
| 113 | + self, python_constraint: VersionConstraint |
| 114 | + ) -> BaseMarker: |
| 115 | + raise NotImplementedError |
| 116 | + |
110 | 117 | @abstractmethod |
111 | 118 | def invert(self) -> BaseMarker: |
112 | 119 | raise NotImplementedError |
@@ -145,6 +152,11 @@ def exclude(self, marker_name: str) -> BaseMarker: |
145 | 152 | def only(self, *marker_names: str) -> BaseMarker: |
146 | 153 | return self |
147 | 154 |
|
| 155 | + def reduce_by_python_constraint( |
| 156 | + self, python_constraint: VersionConstraint |
| 157 | + ) -> BaseMarker: |
| 158 | + return self |
| 159 | + |
148 | 160 | def invert(self) -> EmptyMarker: |
149 | 161 | return EmptyMarker() |
150 | 162 |
|
@@ -186,6 +198,11 @@ def exclude(self, marker_name: str) -> EmptyMarker: |
186 | 198 | def only(self, *marker_names: str) -> BaseMarker: |
187 | 199 | return self |
188 | 200 |
|
| 201 | + def reduce_by_python_constraint( |
| 202 | + self, python_constraint: VersionConstraint |
| 203 | + ) -> BaseMarker: |
| 204 | + return self |
| 205 | + |
189 | 206 | def invert(self) -> AnyMarker: |
190 | 207 | return AnyMarker() |
191 | 208 |
|
@@ -283,6 +300,11 @@ def only(self, *marker_names: str) -> BaseMarker: |
283 | 300 |
|
284 | 301 | return self |
285 | 302 |
|
| 303 | + def reduce_by_python_constraint( |
| 304 | + self, python_constraint: VersionConstraint |
| 305 | + ) -> BaseMarker: |
| 306 | + return self |
| 307 | + |
286 | 308 | def intersect(self, other: BaseMarker) -> BaseMarker: |
287 | 309 | if isinstance(other, SingleMarkerLike): |
288 | 310 | merged = _merge_single_markers(self, other, MultiMarker) |
@@ -395,6 +417,18 @@ def value(self) -> str: |
395 | 417 | def _key(self) -> tuple[object, ...]: |
396 | 418 | return self._name, self._operator, self._value |
397 | 419 |
|
| 420 | + def reduce_by_python_constraint( |
| 421 | + self, python_constraint: VersionConstraint |
| 422 | + ) -> BaseMarker: |
| 423 | + if self.name in PYTHON_VERSION_MARKERS: |
| 424 | + assert isinstance(self._constraint, VersionConstraint) |
| 425 | + if self._constraint.allows_all(python_constraint): |
| 426 | + return AnyMarker() |
| 427 | + elif not self._constraint.allows_any(python_constraint): |
| 428 | + return EmptyMarker() |
| 429 | + |
| 430 | + return self |
| 431 | + |
398 | 432 | def invert(self) -> BaseMarker: |
399 | 433 | if self._operator in ("===", "=="): |
400 | 434 | operator = "!=" |
@@ -670,6 +704,13 @@ def exclude(self, marker_name: str) -> BaseMarker: |
670 | 704 | def only(self, *marker_names: str) -> BaseMarker: |
671 | 705 | return self.of(*(m.only(*marker_names) for m in self._markers)) |
672 | 706 |
|
| 707 | + def reduce_by_python_constraint( |
| 708 | + self, python_constraint: VersionConstraint |
| 709 | + ) -> BaseMarker: |
| 710 | + return self.of( |
| 711 | + *(m.reduce_by_python_constraint(python_constraint) for m in self._markers) |
| 712 | + ) |
| 713 | + |
673 | 714 | def invert(self) -> BaseMarker: |
674 | 715 | markers = [marker.invert() for marker in self._markers] |
675 | 716 |
|
@@ -839,6 +880,31 @@ def exclude(self, marker_name: str) -> BaseMarker: |
839 | 880 | def only(self, *marker_names: str) -> BaseMarker: |
840 | 881 | return self.of(*(m.only(*marker_names) for m in self._markers)) |
841 | 882 |
|
| 883 | + def reduce_by_python_constraint( |
| 884 | + self, python_constraint: VersionConstraint |
| 885 | + ) -> BaseMarker: |
| 886 | + from poetry.core.packages.utils.utils import get_python_constraint_from_marker |
| 887 | + |
| 888 | + markers: Iterable[BaseMarker] = self._markers |
| 889 | + if isinstance(python_constraint, VersionUnion): |
| 890 | + python_only_markers = [] |
| 891 | + other_markers = [] |
| 892 | + for m in self._markers: |
| 893 | + if m == m.only(*PYTHON_VERSION_MARKERS): |
| 894 | + python_only_markers.append(m) |
| 895 | + else: |
| 896 | + other_markers.append(m) |
| 897 | + if get_python_constraint_from_marker( |
| 898 | + self.of(*python_only_markers) |
| 899 | + ).allows_all(python_constraint): |
| 900 | + if not other_markers: |
| 901 | + return AnyMarker() |
| 902 | + markers = other_markers |
| 903 | + |
| 904 | + return self.of( |
| 905 | + *(m.reduce_by_python_constraint(python_constraint) for m in markers) |
| 906 | + ) |
| 907 | + |
842 | 908 | def invert(self) -> BaseMarker: |
843 | 909 | markers = [marker.invert() for marker in self._markers] |
844 | 910 | return MultiMarker(*markers) |
|
0 commit comments