Skip to content

Commit

Permalink
PEP 621 support: fix merging project.optional-dependencies with tool.…
Browse files Browse the repository at this point in the history
…poetry.dependencies

The extra marker of optional dependencies is usually only implicit but has to be considered when merging dependencies.
  • Loading branch information
radoering committed Nov 23, 2024
1 parent 2140a40 commit fc7b524
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/poetry/core/packages/dependency_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from collections import defaultdict
from typing import TYPE_CHECKING

from poetry.core.version.markers import parse_marker


if TYPE_CHECKING:
from poetry.core.packages.dependency import Dependency
from poetry.core.version.markers import BaseMarker


MAIN_GROUP = "main"


Expand Down Expand Up @@ -42,9 +43,20 @@ def dependencies_for_locking(self) -> list[Dependency]:
for dep in self._dependencies:
if dep.name in poetry_dependencies_by_name:
enriched = False
dep_marker = dep.marker
if dep.in_extras:
dep_marker = dep.marker.intersect(
parse_marker(
" or ".join(
f"extra == '{extra}'" for extra in dep.in_extras
)
)
)
for poetry_dep in poetry_dependencies_by_name[dep.name]:
marker = dep.marker.intersect(poetry_dep.marker)
marker = dep_marker.intersect(poetry_dep.marker)
if not marker.is_empty():
if marker == dep_marker:
marker = dep.marker
enriched = True
dependencies.append(_enrich_dependency(dep, poetry_dep, marker))
if not enriched:
Expand Down
90 changes: 90 additions & 0 deletions tests/packages/test_dependency_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest

from packaging.utils import canonicalize_name

from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import DependencyGroup
from poetry.core.packages.directory_dependency import DirectoryDependency
Expand All @@ -15,6 +17,7 @@ def create_dependency(
constraint: str = "*",
*,
extras: tuple[str, ...] = (),
in_extras: tuple[str, ...] = (),
allows_prereleases: bool | None = None,
develop: bool = False,
source_name: str | None = None,
Expand All @@ -26,6 +29,8 @@ def create_dependency(
extras=extras,
allows_prereleases=allows_prereleases,
)
if in_extras:
dep._in_extras = [canonicalize_name(extra) for extra in in_extras]
if develop:
dep._develop = develop
if source_name:
Expand Down Expand Up @@ -260,6 +265,88 @@ def test_remove_dependency_removes_from_both_lists() -> None:
[create_dependency("foo", source_name="src")],
[create_dependency("foo", source_name="src", marker="extra == 'extra1'")],
),
# extras - special
# root extras do not have an extra marker, they just have set _in_extras!
(
[
Dependency.create_from_pep_508("foo;extra!='extra1'"),
create_dependency("foo", in_extras=("extra1",)),
],
[
create_dependency("foo", marker="extra!='extra1'", source_name="src1"),
create_dependency("foo", marker="extra=='extra1'", source_name="src2"),
],
[
create_dependency("foo", source_name="src1", marker="extra!='extra1'"),
create_dependency("foo", source_name="src2", in_extras=("extra1",)),
],
),
(
[
Dependency.create_from_pep_508(
"foo;extra!='extra1' and extra!='extra2'"
),
create_dependency("foo", in_extras=("extra1", "extra2")),
],
[
create_dependency(
"foo",
marker="extra!='extra1' and extra!='extra2'",
source_name="src1",
),
create_dependency(
"foo",
marker="extra=='extra1' or extra=='extra2'",
source_name="src2",
),
],
[
create_dependency(
"foo",
source_name="src1",
marker="extra!='extra1' and extra!='extra2'",
),
create_dependency(
"foo", source_name="src2", in_extras=("extra1", "extra2")
),
],
),
(
[
create_dependency(
"foo", marker="extra!='extra2'", in_extras=("extra1",)
),
create_dependency(
"foo", marker="extra!='extra1'", in_extras=("extra2",)
),
],
[
create_dependency(
"foo",
marker="extra!='extra2' and extra=='extra1'",
source_name="src1",
),
create_dependency(
"foo",
marker="extra!='extra1' and extra=='extra2'",
source_name="src2",
),
],
[
create_dependency(
"foo",
source_name="src1",
marker="extra!='extra2'",
in_extras=("extra1",),
),
create_dependency(
"foo",
source_name="src2",
marker="extra!='extra1'",
in_extras=("extra2",),
),
],
),
],
)
def test_dependencies_for_locking(
Expand All @@ -285,6 +372,9 @@ def test_dependencies_for_locking(
assert [d._develop for d in group.dependencies_for_locking] == [
d._develop for d in expected_dependencies
]
assert [d.in_extras for d in group.dependencies_for_locking] == [
d.in_extras for d in expected_dependencies
]


@pytest.mark.parametrize(
Expand Down

0 comments on commit fc7b524

Please sign in to comment.