Skip to content

Commit

Permalink
Revert "Pool -> AbstractRepository"
Browse files Browse the repository at this point in the history
This reverts commit b3e5364.
  • Loading branch information
juhoautio committed Oct 10, 2022
1 parent b5ccc78 commit 4341009
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
20 changes: 11 additions & 9 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from packaging.utils import NormalizedName
from poetry.core.packages.package import Package

from poetry.repositories.abstract_repository import AbstractRepository
from poetry.repositories import Pool
from poetry.utils.requirements import Requirements


Expand Down Expand Up @@ -61,7 +61,7 @@ class InitCommand(Command):
def __init__(self) -> None:
super().__init__()

self._repository: AbstractRepository | None = None
self._pool: Pool | None = None

def handle(self) -> int:
from pathlib import Path
Expand Down Expand Up @@ -274,7 +274,7 @@ def _determine_requirements_interactive(self) -> list[dict[str, Any]]:
continue

canonicalized_name = canonicalize_name(constraint["name"])
matches = self._get_repository().search(canonicalized_name)
matches = self._get_pool().search(canonicalized_name)
if not matches:
self.line_error("<error>Unable to find package</error>")
package = False
Expand Down Expand Up @@ -322,7 +322,7 @@ def _determine_requirements_interactive(self) -> list[dict[str, Any]]:

if package_constraint is None:
_, package_constraint = find_best_version_for_package(
self._get_repository(), package
self._get_pool(), package
)

self.line(
Expand Down Expand Up @@ -350,7 +350,7 @@ def _determine_requirements(
return self._determine_requirements_interactive()
else:
return determine_requirements_from_list(
self, self._get_repository(), requires, allow_prereleases, source
self, self._get_pool(), requires, allow_prereleases, source
)

def _validate_author(self, author: str, default: str) -> str | None:
Expand All @@ -377,10 +377,12 @@ def _validate_package(package: str | None) -> str | None:

return package

def _get_repository(self) -> AbstractRepository:
def _get_pool(self) -> Pool:
from poetry.repositories import Pool
from poetry.repositories.pypi_repository import PyPiRepository

if self._repository is None:
self._repository = PyPiRepository()
if self._pool is None:
self._pool = Pool()
self._pool.add_repository(PyPiRepository())

return self._repository
return self._pool
16 changes: 9 additions & 7 deletions src/poetry/console/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


if TYPE_CHECKING:
from poetry.repositories.abstract_repository import AbstractRepository
from poetry.repositories import Pool
from poetry.utils.requirements import Requirements


Expand Down Expand Up @@ -50,7 +50,7 @@ class NewCommand(Command):
def __init__(self) -> None:
super().__init__()

self._repository: AbstractRepository | None = None
self._pool: Pool | None = None

def handle(self) -> int:
from pathlib import Path
Expand Down Expand Up @@ -105,7 +105,7 @@ def handle(self) -> int:
if self.option("dependency"):
requirements = format_requirements(
determine_requirements_from_list(
self, self._get_repository(), self.option("dependency")
self, self._get_pool(), self.option("dependency")
)
)

Expand Down Expand Up @@ -133,10 +133,12 @@ def handle(self) -> int:

return 0

def _get_repository(self) -> AbstractRepository:
def _get_pool(self) -> Pool:
from poetry.repositories import Pool
from poetry.repositories.pypi_repository import PyPiRepository

if self._repository is None:
self._repository = PyPiRepository()
if self._pool is None:
self._pool = Pool()
self._pool.add_repository(PyPiRepository())

return self._repository
return self._pool
2 changes: 1 addition & 1 deletion src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def find_latest_package(
return provider.search_for_direct_origin_dependency(dep)

name = package.name
selector = VersionSelector(self.poetry.pool.repositories[0])
selector = VersionSelector(self.poetry.pool)

return selector.find_best_candidate(name, f">={package.pretty_version}")

Expand Down
12 changes: 6 additions & 6 deletions src/poetry/utils/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from tomlkit.items import InlineTable

from poetry.console.commands.command import Command
from poetry.repositories.abstract_repository import AbstractRepository
from poetry.repositories import Pool
from poetry.utils.env import Env


Expand Down Expand Up @@ -61,15 +61,15 @@ def format_requirements(requirements: list[dict[str, str]]) -> Requirements:


def find_best_version_for_package(
repository: AbstractRepository,
pool: Pool,
name: str,
required_version: str | None = None,
allow_prereleases: bool = False,
source: str | None = None,
) -> tuple[str, str]:
from poetry.version.version_selector import VersionSelector

selector = VersionSelector(repository)
selector = VersionSelector(pool)
package = selector.find_best_candidate(
name, required_version, allow_prereleases=allow_prereleases, source=source
)
Expand All @@ -83,7 +83,7 @@ def find_best_version_for_package(

def determine_requirements_from_list(
command: Command,
repository: AbstractRepository,
pool: Pool,
requires: list[str],
allow_prereleases: bool = False,
source: str | None = None,
Expand All @@ -96,7 +96,7 @@ def determine_requirements_from_list(
elif "version" not in requirement:
# determine the best version automatically
name, version = find_best_version_for_package(
repository,
pool,
requirement["name"],
allow_prereleases=allow_prereleases,
source=source,
Expand All @@ -109,7 +109,7 @@ def determine_requirements_from_list(
# check that the specified version/constraint exists
# before we proceed
name, _ = find_best_version_for_package(
repository,
pool,
requirement["name"],
requirement["version"],
allow_prereleases=allow_prereleases,
Expand Down
8 changes: 4 additions & 4 deletions src/poetry/version/version_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
if TYPE_CHECKING:
from poetry.core.packages.package import Package

from poetry.repositories.abstract_repository import AbstractRepository
from poetry.repositories import Pool


class VersionSelector:
def __init__(self, repository: AbstractRepository) -> None:
self._repository = repository
def __init__(self, pool: Pool) -> None:
self._pool = pool

def find_best_candidate(
self,
Expand All @@ -36,7 +36,7 @@ def find_best_candidate(
"source": source,
},
)
candidates = self._repository.find_packages(dependency)
candidates = self._pool.find_packages(dependency)
only_prereleases = all(c.version.is_unstable() for c in candidates)

if not candidates:
Expand Down
5 changes: 3 additions & 2 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from packaging.utils import canonicalize_name

from poetry.console.commands.init import InitCommand
from poetry.repositories import Pool
from poetry.utils._compat import decode
from poetry.utils.requirements import parse_requirements
from tests.helpers import PoetryTestApplication
Expand Down Expand Up @@ -46,7 +47,7 @@ def source_dir(tmp_path: Path) -> Iterator[Path]:
def patches(mocker: MockerFixture, source_dir: Path, repo: TestRepository) -> None:
mocker.patch("pathlib.Path.cwd", return_value=source_dir)
mocker.patch(
"poetry.console.commands.init.InitCommand._get_repository", return_value=repo
"poetry.console.commands.init.InitCommand._get_pool", return_value=Pool([repo])
)


Expand Down Expand Up @@ -106,7 +107,7 @@ def test_noninteractive(
tmp_path: Path,
):
command = app.find("init")
command._repository = repo
command._pool = poetry.pool

repo.add_package(get_package("pytest", "3.6.0"))

Expand Down
1 change: 0 additions & 1 deletion tests/console/commands/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ def test_command_new_with_dependencies(
):
repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0"))
tester.command._repository = repo

package_path = "custompackage"
path = Path(tmp_dir) / package_path
Expand Down

0 comments on commit 4341009

Please sign in to comment.