From 0fd722c7b5c4c6c8b90acd7d8290a3e89f1ea8c0 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Fri, 23 Apr 2021 01:19:25 +0200 Subject: [PATCH] dependency: mark unstable releases as prerelease Relates-to: python-poetry/poetry#2963 --- poetry/core/packages/dependency.py | 29 ++++++++++++++++------------- tests/packages/test_dependency.py | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/poetry/core/packages/dependency.py b/poetry/core/packages/dependency.py index 6e06bc6fa..d2c4406ac 100644 --- a/poetry/core/packages/dependency.py +++ b/poetry/core/packages/dependency.py @@ -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): @@ -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__( @@ -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() ) diff --git a/tests/packages/test_dependency.py b/tests/packages/test_dependency.py index e849b1ebb..635077055 100644 --- a/tests/packages/test_dependency.py +++ b/tests/packages/test_dependency.py @@ -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")