Skip to content

Commit

Permalink
refactor: use Package instead of DependencyPackage as key in override…
Browse files Browse the repository at this point in the history
…s dict (#8956)
  • Loading branch information
radoering authored Feb 13, 2024
1 parent a1878a3 commit 4ed5e1a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
8 changes: 3 additions & 5 deletions src/poetry/puzzle/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

if TYPE_CHECKING:
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package

from poetry.mixology.failure import SolveFailure
from poetry.packages import DependencyPackage


class SolverProblemError(Exception):
Expand All @@ -22,11 +22,9 @@ def error(self) -> SolveFailure:


class OverrideNeeded(Exception):
def __init__(
self, *overrides: dict[DependencyPackage, dict[str, Dependency]]
) -> None:
def __init__(self, *overrides: dict[Package, dict[str, Dependency]]) -> None:
self._overrides = overrides

@property
def overrides(self) -> tuple[dict[DependencyPackage, dict[str, Dependency]], ...]:
def overrides(self) -> tuple[dict[Package, dict[str, Dependency]], ...]:
return self._overrides
22 changes: 7 additions & 15 deletions src/poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(
self._env: Env | None = None
self._python_constraint = package.python_constraint
self._is_debugging: bool = self._io.is_debug() or self._io.is_very_verbose()
self._overrides: dict[DependencyPackage, dict[str, Dependency]] = {}
self._overrides: dict[Package, dict[str, Dependency]] = {}
self._deferred_cache: dict[Dependency, Package] = {}
self._load_deferred = True
self._source_root: Path | None = None
Expand Down Expand Up @@ -155,9 +155,7 @@ def use_latest(self) -> Collection[NormalizedName]:
def is_debugging(self) -> bool:
return self._is_debugging

def set_overrides(
self, overrides: dict[DependencyPackage, dict[str, Dependency]]
) -> None:
def set_overrides(self, overrides: dict[Package, dict[str, Dependency]]) -> None:
self._overrides = overrides

def load_deferred(self, load_deferred: bool) -> None:
Expand Down Expand Up @@ -383,7 +381,7 @@ def _search_for_url(self, dependency: URLDependency) -> Package:
return package

def _get_dependencies_with_overrides(
self, dependencies: list[Dependency], package: DependencyPackage
self, dependencies: list[Dependency], package: Package
) -> list[Dependency]:
overrides = self._overrides.get(package, {})
_dependencies = []
Expand Down Expand Up @@ -458,9 +456,7 @@ def incompatibilities_for(
and self._python_constraint.allows_any(dep.python_constraint)
and (not self._env or dep.marker.validate(self._env.marker_env))
]
dependencies = self._get_dependencies_with_overrides(
_dependencies, dependency_package
)
dependencies = self._get_dependencies_with_overrides(_dependencies, package)

return [
Incompatibility(
Expand Down Expand Up @@ -568,9 +564,7 @@ def complete_package(
continue
self.search_for_direct_origin_dependency(dep)

dependencies = self._get_dependencies_with_overrides(
_dependencies, dependency_package
)
dependencies = self._get_dependencies_with_overrides(_dependencies, package)

# Searching for duplicate dependencies
#
Expand Down Expand Up @@ -647,11 +641,9 @@ def fmt_warning(d: Dependency) -> str:
for dep in deps:
if not overrides_marker_intersection.intersect(dep.marker).is_empty():
current_overrides = self._overrides.copy()
package_overrides = current_overrides.get(
dependency_package, {}
).copy()
package_overrides = current_overrides.get(package, {}).copy()
package_overrides.update({dep.name: dep})
current_overrides.update({dependency_package: package_overrides})
current_overrides.update({package: package_overrides})
overrides.append(current_overrides)

if overrides:
Expand Down
5 changes: 2 additions & 3 deletions src/poetry/puzzle/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from poetry.core.packages.package import Package
from poetry.core.packages.project_package import ProjectPackage

from poetry.packages import DependencyPackage
from poetry.puzzle.transaction import Transaction
from poetry.repositories import RepositoryPool
from poetry.utils.env import Env
Expand All @@ -51,7 +50,7 @@ def __init__(
self._provider = Provider(
self._package, self._pool, self._io, installed=installed, locked=locked
)
self._overrides: list[dict[DependencyPackage, dict[str, Dependency]]] = []
self._overrides: list[dict[Package, dict[str, Dependency]]] = []

@property
def provider(self) -> Provider:
Expand Down Expand Up @@ -119,7 +118,7 @@ def _progress(self) -> Iterator[None]:

def _solve_in_compatibility_mode(
self,
overrides: tuple[dict[DependencyPackage, dict[str, Dependency]], ...],
overrides: tuple[dict[Package, dict[str, Dependency]], ...],
) -> tuple[list[Package], list[int]]:
packages = []
depths = []
Expand Down

0 comments on commit 4ed5e1a

Please sign in to comment.