Skip to content

Commit

Permalink
Handle circular dependencies when getting extras (#2787)
Browse files Browse the repository at this point in the history
Resolves: #2376
  • Loading branch information
abn authored Aug 28, 2020
1 parent 1c8abd4 commit 8b8b97a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions poetry/utils/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def get_extra_package_names(
for extra_package_name in extras.get(extra_name, ())
]

# keep record of packages seen during recursion in order to avoid recursion error
seen_package_names = set()

def _extra_packages(package_names):
"""Recursively find dependencies for packages names"""
# for each extra pacakge name
Expand All @@ -41,11 +44,16 @@ def _extra_packages(package_names):
# dependency (like setuptools), which should be ignored
package = packages_by_name.get(canonicalize_name(package_name))
if package:
yield package.name
if package.name not in seen_package_names:
seen_package_names.add(package.name)
yield package.name
# Recurse for dependencies
for dependency_package_name in _extra_packages(
dependency.name for dependency in package.requires
dependency.name
for dependency in package.requires
if dependency.name not in seen_package_names
):
seen_package_names.add(dependency_package_name)
yield dependency_package_name

return _extra_packages(extra_package_names)
12 changes: 12 additions & 0 deletions tests/utils/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
_PACKAGE_BAR = Package("bar", "0.3.0")
_PACKAGE_BAR.add_dependency("foo")

# recursive dependency
_PACKAGE_BAZ = Package("baz", "0.4.0")
_PACKAGE_BAZ.add_dependency("quix")
_PACKAGE_QUIX = Package("quix", "0.5.0")
_PACKAGE_QUIX.add_dependency("baz")


@pytest.mark.parametrize(
"packages,extras,extra_names,expected_extra_package_names",
Expand Down Expand Up @@ -40,6 +46,12 @@
["group0", "group1"],
["bar", "foo", "spam"],
),
(
[_PACKAGE_BAZ, _PACKAGE_QUIX],
{"group0": ["baz"], "group1": ["quix"]},
["group0", "group1"],
["baz", "quix"],
),
],
)
def test_get_extra_package_names(
Expand Down

0 comments on commit 8b8b97a

Please sign in to comment.