Skip to content

Commit

Permalink
fix: PEP 440 pre-release handling (part 2)
Browse files Browse the repository at this point in the history
Remove implicit "allow-prereleases" in repository, consider explicit allow-prereleases in show, make search_for test in provider forward-compatible.
  • Loading branch information
radoering authored Jan 1, 2023
1 parent bfd490c commit 7c86992
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
13 changes: 10 additions & 3 deletions src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,25 @@ 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()
break

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
Expand Down
23 changes: 2 additions & 21 deletions src/poetry/repositories/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
21 changes: 12 additions & 9 deletions tests/puzzle/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
[
Expand All @@ -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


Expand Down

0 comments on commit 7c86992

Please sign in to comment.