forked from python-poetry/poetry-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add constraint_regions (python-poetry#381)
* Add constraint_regions * unit test constraint_regions * _ranges_for() and flatten are the same
- Loading branch information
1 parent
45523bb
commit 8df7d87
Showing
7 changed files
with
162 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from poetry.core.semver.version_range import VersionRange | ||
|
||
|
||
if TYPE_CHECKING: | ||
from poetry.core.semver.version_constraint import VersionConstraint | ||
|
||
|
||
def constraint_regions(constraints: list[VersionConstraint]) -> list[VersionRange]: | ||
""" | ||
Transform a list of VersionConstraints into a list of VersionRanges that mark out | ||
the distinct regions of version-space. | ||
eg input >=3.6 and >=2.7,<3.0.0 || >=3.4.0 | ||
output <2.7, >=2.7,<3.0.0, >=3.0.0,<3.4.0, >=3.4.0,<3.6, >=3.6. | ||
""" | ||
flattened = [] | ||
for constraint in constraints: | ||
flattened += constraint.flatten() | ||
|
||
mins = { | ||
(constraint.min, not constraint.include_min) | ||
for constraint in flattened | ||
if constraint.min is not None | ||
} | ||
maxs = { | ||
(constraint.max, constraint.include_max) | ||
for constraint in flattened | ||
if constraint.max is not None | ||
} | ||
|
||
edges = sorted(mins | maxs) | ||
if not edges: | ||
return [VersionRange(None, None)] | ||
|
||
start = edges[0] | ||
regions = [ | ||
VersionRange(None, start[0], include_max=start[1]), | ||
] | ||
|
||
for low, high in zip(edges, edges[1:]): | ||
version_range = VersionRange( | ||
low[0], | ||
high[0], | ||
include_min=not low[1], | ||
include_max=high[1], | ||
) | ||
regions.append(version_range) | ||
|
||
end = edges[-1] | ||
regions.append( | ||
VersionRange(end[0], None, include_min=not end[1]), | ||
) | ||
|
||
return regions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
from poetry.core.semver.empty_constraint import EmptyConstraint | ||
from poetry.core.semver.util import constraint_regions | ||
from poetry.core.semver.version import Version | ||
from poetry.core.semver.version_range import VersionRange | ||
|
||
|
||
if TYPE_CHECKING: | ||
from poetry.core.semver.version_constraint import VersionConstraint | ||
|
||
|
||
PY27 = Version.parse("2.7") | ||
PY30 = Version.parse("3") | ||
PY36 = Version.parse("3.6.0") | ||
PY37 = Version.parse("3.7") | ||
PY38 = Version.parse("3.8.0") | ||
PY40 = Version.parse("4.0.0") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"versions, expected", | ||
[ | ||
([VersionRange(None, None)], [VersionRange(None, None)]), | ||
([EmptyConstraint()], [VersionRange(None, None)]), | ||
( | ||
[VersionRange(PY27, None, include_min=True)], | ||
[ | ||
VersionRange(None, PY27, include_max=False), | ||
VersionRange(PY27, None, include_min=True), | ||
], | ||
), | ||
( | ||
[VersionRange(None, PY40, include_max=False)], | ||
[ | ||
VersionRange(None, PY40, include_max=False), | ||
VersionRange(PY40, None, include_min=True), | ||
], | ||
), | ||
( | ||
[VersionRange(PY27, PY27, include_min=True, include_max=True)], | ||
[ | ||
VersionRange(None, PY27, include_max=False), | ||
VersionRange(PY27, PY27, include_min=True, include_max=True), | ||
VersionRange(PY27, None, include_min=False), | ||
], | ||
), | ||
( | ||
[VersionRange(PY27, PY30, include_min=True, include_max=False)], | ||
[ | ||
VersionRange(None, PY27, include_max=False), | ||
VersionRange(PY27, PY30, include_min=True, include_max=False), | ||
VersionRange(PY30, None, include_min=True), | ||
], | ||
), | ||
( | ||
[ | ||
VersionRange(PY27, PY30, include_min=True, include_max=False).union( | ||
VersionRange(PY37, PY40, include_min=False, include_max=True) | ||
), | ||
VersionRange(PY36, PY38, include_min=True, include_max=False), | ||
], | ||
[ | ||
VersionRange(None, PY27, include_max=False), | ||
VersionRange(PY27, PY30, include_min=True, include_max=False), | ||
VersionRange(PY30, PY36, include_min=True, include_max=False), | ||
VersionRange(PY36, PY37, include_min=True, include_max=True), | ||
VersionRange(PY37, PY38, include_min=False, include_max=False), | ||
VersionRange(PY38, PY40, include_min=True, include_max=True), | ||
VersionRange(PY40, None, include_min=False), | ||
], | ||
), | ||
], | ||
) | ||
def test_constraint_regions( | ||
versions: list[VersionConstraint], expected: list[VersionRange] | ||
) -> None: | ||
regions = constraint_regions(versions) | ||
assert regions == expected |