Skip to content

Commit

Permalink
typechecking semver.version_union
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed May 14, 2022
1 parent c17e430 commit 95f1961
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ module = [
'poetry.core.packages.utils.utils',
'poetry.core.packages.dependency',
'poetry.core.packages.package',
'poetry.core.semver.version_union',
'poetry.core.toml.exceptions',
'poetry.core.toml.file',
]
Expand Down
17 changes: 14 additions & 3 deletions src/poetry/core/semver/version_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def of(cls, *ranges: VersionConstraint) -> VersionConstraint:
):
merged.append(constraint)
else:
merged[-1] = merged[-1].union(constraint)
new_constraint = merged[-1].union(constraint)
assert isinstance(new_constraint, VersionRangeConstraint)
merged[-1] = new_constraint

if len(merged) == 1:
return merged[0]
Expand Down Expand Up @@ -147,7 +149,7 @@ def union(self, other: VersionConstraint) -> VersionConstraint:
def difference(self, other: VersionConstraint) -> VersionConstraint:
our_ranges = iter(self._ranges)
their_ranges = iter(self._ranges_for(other))
new_ranges = []
new_ranges: list[VersionConstraint] = []

state = {
"current": next(our_ranges, None),
Expand All @@ -159,6 +161,7 @@ def their_next_range() -> bool:
if state["their_range"]:
return True

assert state["current"] is not None
new_ranges.append(state["current"])
our_current = next(our_ranges, None)
while our_current:
Expand All @@ -169,6 +172,7 @@ def their_next_range() -> bool:

def our_next_range(include_current: bool = True) -> bool:
if include_current:
assert state["current"] is not None
new_ranges.append(state["current"])

our_current = next(our_ranges, None)
Expand All @@ -183,6 +187,7 @@ def our_next_range(include_current: bool = True) -> bool:
if state["their_range"] is None:
break

assert state["current"] is not None
if state["their_range"].is_strictly_lower(state["current"]):
if not their_next_range():
break
Expand All @@ -207,6 +212,7 @@ def our_next_range(include_current: bool = True) -> bool:
if not our_next_range(False):
break
else:
assert isinstance(difference, VersionRangeConstraint)
state["current"] = difference

if state["current"].allows_higher(state["their_range"]):
Expand Down Expand Up @@ -251,7 +257,9 @@ def _exclude_single_wildcard_range_string(self) -> str:
# and the one with the max is the first part
idx_order = (0, 1) if self._ranges[0].max else (1, 0)
one = self._ranges[idx_order[0]].max
assert one is not None
two = self._ranges[idx_order[1]].min
assert two is not None

# versions can have both semver and non semver parts
parts_one = [
Expand Down Expand Up @@ -302,6 +310,9 @@ def _excludes_single_wildcard_range_check_is_valid_range(
wildcard range.
"""

assert one.max is not None
assert two.min is not None

max_precision = max(one.max.precision, two.min.precision)

if max_precision <= 3:
Expand Down Expand Up @@ -361,7 +372,7 @@ def _excludes_single_wildcard_range_check_is_valid_range(
_padded_version_one.major,
_padded_version_one.minor,
_padded_version_one.patch,
*_extra,
tuple(_extra),
)
)

Expand Down

0 comments on commit 95f1961

Please sign in to comment.