Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dependency: mark unstable releases as prerelease #170

Merged
merged 2 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
from typing import Optional
from typing import Union

from poetry.core.packages.constraints import (
parse_constraint as parse_generic_constraint,
)
from poetry.core.packages.specification import PackageSpecification
from poetry.core.semver.helpers import parse_constraint
from poetry.core.semver.version_range_constraint import VersionRangeConstraint
from poetry.core.version.markers import parse_marker

from .constraints import parse_constraint as parse_generic_constraint
from .specification import PackageSpecification


if TYPE_CHECKING:
from poetry.core.semver.helpers import VersionTypes # noqa
from poetry.core.version.markers import BaseMarker # noqa

from .constraints import BaseConstraint # noqa
from .directory_dependency import DirectoryDependency # noqa
from .file_dependency import FileDependency # noqa
from .package import Package
from .types import DependencyTypes
from poetry.core.packages.constraints import BaseConstraint
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
from poetry.core.packages.package import Package
from poetry.core.packages.types import DependencyTypes
from poetry.core.semver.helpers import VersionTypes
from poetry.core.version.markers import BaseMarker


class Dependency(PackageSpecification):
Expand All @@ -41,7 +42,6 @@ def __init__(
source_reference: Optional[str] = None,
source_resolved_reference: Optional[str] = None,
):
from poetry.core.semver.version_range import VersionRange
from poetry.core.version.markers import AnyMarker

super(Dependency, self).__init__(
Expand All @@ -60,7 +60,10 @@ def __init__(
self._optional = optional
self._category = category

if isinstance(self._constraint, VersionRange) and self._constraint.min:
if (
isinstance(self._constraint, VersionRangeConstraint)
and self._constraint.min
):
allows_prereleases = (
allows_prereleases or self._constraint.min.is_unstable()
)
Expand Down
3 changes: 1 addition & 2 deletions poetry/core/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

if TYPE_CHECKING:
from poetry.core.semver.helpers import VersionTypes
from poetry.core.semver.version_range import VersionRange
from poetry.core.version.pep440 import LocalSegmentType


Expand Down Expand Up @@ -150,7 +149,7 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return "<Version {}>".format(str(self))

def __eq__(self, other: Union["Version", "VersionRange"]) -> bool:
def __eq__(self, other: Union["Version", "VersionRangeConstraint"]) -> bool:
from poetry.core.semver.version_range import VersionRange

if isinstance(other, VersionRange):
Expand Down
22 changes: 11 additions & 11 deletions poetry/core/semver/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def allows_all(self, other: "VersionTypes") -> bool:
if isinstance(other, VersionUnion):
return all([self.allows_all(constraint) for constraint in other.ranges])

if isinstance(other, VersionRange):
if isinstance(other, VersionRangeConstraint):
return not other.allows_lower(self) and not other.allows_higher(self)

raise ValueError("Unknown VersionConstraint type {}.".format(other))
Expand All @@ -121,7 +121,7 @@ def allows_any(self, other: "VersionTypes") -> bool:
if isinstance(other, VersionUnion):
return any([self.allows_any(constraint) for constraint in other.ranges])

if isinstance(other, VersionRange):
if isinstance(other, VersionRangeConstraint):
return not other.is_strictly_lower(self) and not other.is_strictly_higher(
self
)
Expand All @@ -144,7 +144,7 @@ def intersect(self, other: "VersionTypes") -> "VersionTypes":

return EmptyConstraint()

if not isinstance(other, VersionRange):
if not isinstance(other, VersionRangeConstraint):
raise ValueError("Unknown VersionConstraint type {}.".format(other))

if self.allows_lower(other):
Expand Down Expand Up @@ -202,7 +202,7 @@ def union(self, other: "VersionTypes") -> "VersionTypes":

return VersionUnion.of(self, other)

if isinstance(other, VersionRange):
if isinstance(other, VersionRangeConstraint):
# If the two ranges don't overlap, we won't be able to create a single
# VersionRange for both of them.
edges_touch = (
Expand Down Expand Up @@ -261,7 +261,7 @@ def difference(self, other: "VersionTypes") -> "VersionTypes":
VersionRange(self.min, other, self.include_min, False),
VersionRange(other, self.max, False, self.include_max),
)
elif isinstance(other, VersionRange):
elif isinstance(other, VersionRangeConstraint):
if not self.allows_any(other):
return self

Expand Down Expand Up @@ -326,7 +326,7 @@ def difference(self, other: "VersionTypes") -> "VersionTypes":
raise ValueError("Unknown VersionConstraint type {}.".format(other))

def __eq__(self, other: Any) -> int:
if not isinstance(other, VersionRange):
if not isinstance(other, VersionRangeConstraint):
return False

return (
Expand All @@ -336,19 +336,19 @@ def __eq__(self, other: Any) -> int:
and self._include_max == other.include_max
)

def __lt__(self, other: "VersionRange") -> int:
def __lt__(self, other: "VersionRangeConstraint") -> int:
return self._cmp(other) < 0

def __le__(self, other: "VersionRange") -> int:
def __le__(self, other: "VersionRangeConstraint") -> int:
return self._cmp(other) <= 0

def __gt__(self, other: "VersionRange") -> int:
def __gt__(self, other: "VersionRangeConstraint") -> int:
return self._cmp(other) > 0

def __ge__(self, other: "VersionRange") -> int:
def __ge__(self, other: "VersionRangeConstraint") -> int:
return self._cmp(other) >= 0

def _cmp(self, other: "VersionRange") -> int:
def _cmp(self, other: "VersionRangeConstraint") -> int:
if self.min is None:
if other.min is None:
return self._compare_max(other)
Expand Down
6 changes: 4 additions & 2 deletions poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,11 @@ def invert(self) -> MarkerTypes:
# This one is more tricky to handle
# since it's technically a multi marker
# so the inverse will be a union of inverse
from poetry.core.semver.version_range import VersionRange
from poetry.core.semver.version_range_constraint import (
VersionRangeConstraint,
)

if not isinstance(self._constraint, VersionRange):
if not isinstance(self._constraint, VersionRangeConstraint):
# The constraint must be a version range, otherwise
# it's an internal error
raise RuntimeError(
Expand Down
17 changes: 17 additions & 0 deletions tests/packages/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ def test_accepts():
assert dependency.accepts(package)


@pytest.mark.parametrize(
"constraint,result",
[
("^1.0", False),
("^1.0.dev0", True),
("^1.0.0", False),
("^1.0.0.dev0", True),
("^1.0.0.alpha0", True),
("^1.0.0.alpha0+local", True),
("^1.0.0.rc0+local", True),
("^1.0.0-1", False),
],
)
def test_allows_prerelease(constraint, result):
assert Dependency("A", constraint).allows_prereleases() == result


def test_accepts_prerelease():
dependency = Dependency("A", "^1.0", allows_prereleases=True)
package = Package("A", "1.4-beta.1")
Expand Down