Skip to content

Commit

Permalink
add support for PEP 621: consider relevant project sections when calc…
Browse files Browse the repository at this point in the history
…ulating the content hash for the lock file (#9135)
  • Loading branch information
radoering committed Sep 15, 2024
1 parent d68e310 commit cd7c9ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
36 changes: 31 additions & 5 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Locker:
"dev-dependencies",
]
_relevant_keys: ClassVar[list[str]] = [*_legacy_keys, "group"]
_relevant_project_keys: ClassVar[list[str]] = [
"requires-python",
"dependencies",
"optional-dependencies",
]

def __init__(self, lock: Path, pyproject_data: dict[str, Any]) -> None:
self._lock = lock
Expand Down Expand Up @@ -324,16 +329,37 @@ def _get_content_hash(self) -> str:
"""
Returns the sha256 hash of the sorted content of the pyproject file.
"""
content = self._pyproject_data.get("tool", {}).get("poetry", {})
project_content = self._pyproject_data.get("project", {})
tool_poetry_content = self._pyproject_data.get("tool", {}).get("poetry", {})

relevant_content = {}
relevant_project_content = {}
for key in self._relevant_project_keys:
data = project_content.get(key)
if data is not None:
relevant_project_content[key] = data

relevant_poetry_content = {}
for key in self._relevant_keys:
data = content.get(key)
data = tool_poetry_content.get(key)

if data is None and key not in self._legacy_keys:
if data is None and (
# Special handling for legacy keys is just for backwards compatibility,
# and thereby not required if there is relevant content in [project].
key not in self._legacy_keys or relevant_project_content
):
continue

relevant_content[key] = data
relevant_poetry_content[key] = data

if relevant_project_content:
relevant_content = {
"project": relevant_project_content,
"tool": {"poetry": relevant_poetry_content},
}
else:
# For backwards compatibility, we have to put the relevant content
# of the [tool.poetry] section at top level!
relevant_content = relevant_poetry_content

return sha256(json.dumps(relevant_content, sort_keys=True).encode()).hexdigest()

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/up_to_date_lock/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd7c9ce

Please sign in to comment.