From d99c5b3c637b375162e3a89427ca65d0e5f3277c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Thu, 22 Dec 2022 13:48:42 +0100 Subject: [PATCH] pre-release handling: remove implicit "allow-prereleases" in repository, consider explicit allow-prereleases in show, make search_for test in provider forward-compatible --- src/poetry/console/commands/show.py | 12 +++++++++--- src/poetry/repositories/repository.py | 23 ++--------------------- tests/puzzle/test_provider.py | 21 ++++++++++++--------- 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/poetry/console/commands/show.py b/src/poetry/console/commands/show.py index 3c92574bc32..d0ca2b8add0 100644 --- a/src/poetry/console/commands/show.py +++ b/src/poetry/console/commands/show.py @@ -520,18 +520,24 @@ def find_latest_package( from poetry.version.version_selector import VersionSelector # find the latest version allowed in this pool + requires = root.all_requires if package.is_direct_origin(): - requires = root.all_requires - for dep in requires: if dep.name == package.name and dep.source_type == package.source_type: provider = Provider(root, self.poetry.pool, NullIO()) return provider.search_for_direct_origin_dependency(dep) + allow_prereleases = False + for dep in requires: + if dep.name == package.name: + allow_prereleases = dep.allows_prereleases() + name = package.name selector = VersionSelector(self.poetry.pool) - return selector.find_best_candidate(name, f">={package.pretty_version}") + return selector.find_best_candidate( + name, f">={package.pretty_version}", allow_prereleases + ) def get_update_status(self, latest: Package, package: Package) -> str: from poetry.core.constraints.version import parse_constraint diff --git a/src/poetry/repositories/repository.py b/src/poetry/repositories/repository.py index 274ece2ae65..c9dd4b85969 100644 --- a/src/poetry/repositories/repository.py +++ b/src/poetry/repositories/repository.py @@ -6,7 +6,6 @@ from packaging.utils import canonicalize_name from poetry.core.constraints.version import Version -from poetry.core.constraints.version import VersionRange from poetry.repositories.abstract_repository import AbstractRepository from poetry.repositories.exceptions import PackageNotFound @@ -34,11 +33,10 @@ def packages(self) -> list[Package]: def find_packages(self, dependency: Dependency) -> list[Package]: packages = [] - constraint, allow_prereleases = self._get_constraints_from_dependency( - dependency - ) ignored_pre_release_packages = [] + constraint = dependency.constraint + allow_prereleases = dependency.allows_prereleases() for package in self._find_packages(dependency.name, constraint): if package.yanked and not isinstance(constraint, Version): # PEP 592: yanked files are always ignored, unless they are the only @@ -92,23 +90,6 @@ def search(self, query: str) -> list[Package]: return results - @staticmethod - def _get_constraints_from_dependency( - dependency: Dependency, - ) -> tuple[VersionConstraint, bool]: - constraint = dependency.constraint - - allow_prereleases = dependency.allows_prereleases() - if isinstance(constraint, VersionRange) and ( - constraint.max is not None - and constraint.max.is_unstable() - or constraint.min is not None - and constraint.min.is_unstable() - ): - allow_prereleases = True - - return constraint, allow_prereleases - def _find_packages( self, name: NormalizedName, constraint: VersionConstraint ) -> list[Package]: diff --git a/tests/puzzle/test_provider.py b/tests/puzzle/test_provider.py index a5126a47337..5e96d952832 100644 --- a/tests/puzzle/test_provider.py +++ b/tests/puzzle/test_provider.py @@ -67,15 +67,7 @@ def provider(root: ProjectPackage, pool: RepositoryPool) -> Provider: (Dependency("foo", "<2"), [Package("foo", "1")]), (Dependency("foo", "<2", extras=["bar"]), [Package("foo", "1")]), (Dependency("foo", ">=1"), [Package("foo", "2"), Package("foo", "1")]), - ( - Dependency("foo", ">=1a"), - [ - Package("foo", "3a"), - Package("foo", "2"), - Package("foo", "2a"), - Package("foo", "1"), - ], - ), + (Dependency("foo", ">=1a"), [Package("foo", "2"), Package("foo", "1")]), ( Dependency("foo", ">=1", allows_prereleases=True), [ @@ -101,6 +93,17 @@ def test_search_for( repository.add_package(foo2a) repository.add_package(foo2) repository.add_package(foo3a) + # TODO: remove workaround when poetry-core with + # https://github.com/python-poetry/poetry-core/pull/543 is available + if str(dependency.constraint) == ">=1a": + result = provider.search_for(dependency) + assert result == expected or result == [ + Package("foo", "3a"), + Package("foo", "2"), + Package("foo", "2a"), + Package("foo", "1"), + ] + return assert provider.search_for(dependency) == expected