Skip to content

Commit

Permalink
refactor(locker): pass entire pyproject_data to locker - instead of l…
Browse files Browse the repository at this point in the history
…ocal_config, whichs corresponds to the [tool.poetry] section

This is in preparation for PEP 621 support where fields outside [tool.poetry] are relevant for locking.
  • Loading branch information
radoering committed Mar 15, 2024
1 parent 2ba28aa commit 0b6b030
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ def handle(self) -> int:
)

# Refresh the locker
self.poetry.locker.set_local_config(poetry_content)
content["tool"]["poetry"] = poetry_content
self.poetry.locker.set_pyproject_data(content)
self.installer.set_locker(self.poetry.locker)

# Cosmetic new line
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/console/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def handle(self) -> int:
)

# Refresh the locker
self.poetry.locker.set_local_config(poetry_content)
content["tool"]["poetry"] = poetry_content
self.poetry.locker.set_pyproject_data(content)
self.installer.set_locker(self.poetry.locker)
self.installer.set_package(self.poetry.package)
self.installer.dry_run(self.option("dry-run", False))
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def create_poetry(
base_poetry = super().create_poetry(cwd=cwd, with_groups=with_groups)

poetry_file = base_poetry.pyproject_path
locker = Locker(poetry_file.parent / "poetry.lock", base_poetry.local_config)
locker = Locker(poetry_file.parent / "poetry.lock", base_poetry.pyproject.data)

# Loading global configuration
config = Config.create()
Expand Down
19 changes: 15 additions & 4 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import re
import warnings

from hashlib import sha256
from pathlib import Path
Expand Down Expand Up @@ -60,9 +61,9 @@ class Locker:
]
_relevant_keys: ClassVar[list[str]] = [*_legacy_keys, "group"]

def __init__(self, lock: Path, local_config: dict[str, Any]) -> None:
def __init__(self, lock: Path, pyproject_data: dict[str, Any]) -> None:
self._lock = lock
self._local_config = local_config
self._pyproject_data = pyproject_data
self._lock_data: dict[str, Any] | None = None
self._content_hash = self._get_content_hash()

Expand Down Expand Up @@ -97,8 +98,18 @@ def is_fresh(self) -> bool:

return False

def set_pyproject_data(self, pyproject_data: dict[str, Any]) -> None:
self._pyproject_data = pyproject_data
self._content_hash = self._get_content_hash()

def set_local_config(self, local_config: dict[str, Any]) -> None:
self._local_config = local_config
warnings.warn(
"Locker.set_local_config() is deprecated and will be removed in a future"
" release. Use Locker.set_pyproject_data() instead.",
DeprecationWarning,
stacklevel=2,
)
self._pyproject_data.setdefault("tool", {})["poetry"] = local_config
self._content_hash = self._get_content_hash()

def locked_repository(self) -> LockfileRepository:
Expand Down Expand Up @@ -305,7 +316,7 @@ def _get_content_hash(self) -> str:
"""
Returns the sha256 hash of the sorted content of the pyproject file.
"""
content = self._local_config
content = self._pyproject_data.get("tool", {}).get("poetry", {})

relevant_content = {}
for key in self._relevant_keys:
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def _factory(

if use_test_locker:
locker = TestLocker(
poetry.locker.lock, locker_config or poetry.locker._local_config
poetry.locker.lock, locker_config or poetry.locker._pyproject_data
)
locker.write()

Expand Down
4 changes: 2 additions & 2 deletions tests/console/commands/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_check_lock_outdated(

locker = Locker(
lock=poetry_with_outdated_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_outdated_lockfile.locker._local_config,
pyproject_data=poetry_with_outdated_lockfile.locker._pyproject_data,
)
poetry_with_outdated_lockfile.set_locker(locker)

Expand Down Expand Up @@ -210,7 +210,7 @@ def test_check_lock_up_to_date(

locker = Locker(
lock=poetry_with_up_to_date_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_up_to_date_lockfile.locker._local_config,
pyproject_data=poetry_with_up_to_date_lockfile.locker._pyproject_data,
)
poetry_with_up_to_date_lockfile.set_locker(locker)

Expand Down
18 changes: 9 additions & 9 deletions tests/console/commands/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_lock_check_outdated_legacy(

locker = Locker(
lock=poetry_with_outdated_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_outdated_lockfile.locker._local_config,
pyproject_data=poetry_with_outdated_lockfile.locker._pyproject_data,
)
poetry_with_outdated_lockfile.set_locker(locker)

Expand All @@ -125,7 +125,7 @@ def test_lock_check_up_to_date_legacy(

locker = Locker(
lock=poetry_with_up_to_date_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_up_to_date_lockfile.locker._local_config,
pyproject_data=poetry_with_up_to_date_lockfile.locker._pyproject_data,
)
poetry_with_up_to_date_lockfile.set_locker(locker)

Expand Down Expand Up @@ -153,7 +153,7 @@ def test_lock_no_update(

locker = Locker(
lock=poetry_with_old_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_old_lockfile.locker._local_config,
pyproject_data=poetry_with_old_lockfile.locker._pyproject_data,
)
poetry_with_old_lockfile.set_locker(locker)

Expand All @@ -168,7 +168,7 @@ def test_lock_no_update(

locker = Locker(
lock=poetry_with_old_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config={},
pyproject_data={},
)
packages = locker.locked_repository().packages

Expand Down Expand Up @@ -196,7 +196,7 @@ def test_lock_no_update_path_dependencies(
locker = Locker(
lock=poetry_with_nested_path_deps_old_lockfile.pyproject.file.path.parent
/ "poetry.lock",
local_config=poetry_with_nested_path_deps_old_lockfile.locker._local_config,
pyproject_data=poetry_with_nested_path_deps_old_lockfile.locker._pyproject_data,
)
poetry_with_nested_path_deps_old_lockfile.set_locker(locker)

Expand Down Expand Up @@ -224,7 +224,7 @@ def test_lock_path_dependency_does_not_exist(
poetry = _project_factory(project, project_factory, fixture_dir)
locker = Locker(
lock=poetry.pyproject.file.path.parent / "poetry.lock",
local_config=poetry.locker._local_config,
pyproject_data=poetry.locker._pyproject_data,
)
poetry.set_locker(locker)
options = "" if update else "--no-update"
Expand Down Expand Up @@ -252,7 +252,7 @@ def test_lock_path_dependency_deleted_from_pyproject(
poetry = _project_factory(project, project_factory, fixture_dir)
locker = Locker(
lock=poetry.pyproject.file.path.parent / "poetry.lock",
local_config=poetry.locker._local_config,
pyproject_data=poetry.locker._pyproject_data,
)
poetry.set_locker(locker)

Expand All @@ -279,7 +279,7 @@ def test_lock_with_incompatible_lockfile(
locker = Locker(
lock=poetry_with_incompatible_lockfile.pyproject.file.path.parent
/ "poetry.lock",
local_config=poetry_with_incompatible_lockfile.locker._local_config,
pyproject_data=poetry_with_incompatible_lockfile.locker._pyproject_data,
)
poetry_with_incompatible_lockfile.set_locker(locker)

Expand Down Expand Up @@ -309,7 +309,7 @@ def test_lock_with_invalid_lockfile(

locker = Locker(
lock=poetry_with_invalid_lockfile.pyproject.file.path.parent / "poetry.lock",
local_config=poetry_with_invalid_lockfile.locker._local_config,
pyproject_data=poetry_with_invalid_lockfile.locker._pyproject_data,
)
poetry_with_invalid_lockfile.set_locker(locker)

Expand Down
6 changes: 3 additions & 3 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,16 @@ def reset_poetry(self) -> None:
self._poetry.set_pool(poetry.pool)
self._poetry.set_config(poetry.config)
self._poetry.set_locker(
TestLocker(poetry.locker.lock, self._poetry.local_config)
TestLocker(poetry.locker.lock, self._poetry.pyproject.data)
)


class TestLocker(Locker):
# class name begins 'Test': tell pytest that it does not contain testcases.
__test__ = False

def __init__(self, lock: Path, local_config: dict[str, Any]) -> None:
super().__init__(lock, local_config)
def __init__(self, lock: Path, pyproject_data: dict[str, Any]) -> None:
super().__init__(lock, pyproject_data)
self._locked = False
self._write = False

Expand Down
2 changes: 1 addition & 1 deletion tests/packages/test_locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ def test_content_hash_with_legacy_is_compatible(

locker = locker.__class__(
lock=locker.lock,
local_config=local_config,
pyproject_data={"tool": {"poetry": local_config}},
)

old_content_hash = sha256(
Expand Down

0 comments on commit 0b6b030

Please sign in to comment.