Skip to content

Commit

Permalink
Merge pull request #72 from IBM/IntermediateExtras-71
Browse files Browse the repository at this point in the history
Intermediate extras 71
  • Loading branch information
gabe-l-hart authored Jan 13, 2023
2 parents 4f5cdc1 + d85c8d1 commit 16e7322
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
31 changes: 18 additions & 13 deletions import_tracker/setup_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ def parse_requirements(

# Determine the common requirements as the intersection of all extras sets
# union'ed with all other import sets
extras_modules_tree = {}
for extras_module in extras_modules:
parent = extras_modules_tree
for part in extras_module.split("."):
parent = parent.setdefault(part, {})
log.debug4("Extras Modules Tree: %s", extras_modules_tree)

common_intersection = None
non_extra_union = set()
for import_set_name, import_set in import_sets.items():
Expand All @@ -150,12 +143,24 @@ def parse_requirements(
common_intersection = common_intersection.intersection(import_set)

# Determine if this import set falls outside of the extras
parent = extras_modules_tree
for part in import_set_name.split("."):
parent = parent.get(part)
if parent is None:
non_extra_union = non_extra_union.union(import_set)
break
import_set_parts = import_set_name.split(".")
in_extra = any(
extras_module.startswith(import_set_name)
for extras_module in extras_modules
)
if not in_extra:
for i in range(len(import_set_parts)):
parent_path = ".".join(import_set_parts[: i + 1])
if parent_path in extras_modules:
in_extra = True
break
if not in_extra:
log.debug3(
"%s not covered by an extra. Adding %s to non extra union",
import_set_name,
import_set,
)
non_extra_union = non_extra_union.union(import_set)
common_intersection = common_intersection or set()
if len(extras_modules) == 1:
common_intersection = set()
Expand Down
2 changes: 2 additions & 0 deletions test/sample_libs/intermediate_extras/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Local
from . import bar, foo
1 change: 1 addition & 0 deletions test/sample_libs/intermediate_extras/bar/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("In bar")
2 changes: 2 additions & 0 deletions test/sample_libs/intermediate_extras/foo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Local
from . import bat, baz
2 changes: 2 additions & 0 deletions test/sample_libs/intermediate_extras/foo/bat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Third Party
import yaml
2 changes: 2 additions & 0 deletions test/sample_libs/intermediate_extras/foo/baz/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# First Party
import alog
40 changes: 40 additions & 0 deletions test/test_setup_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,43 @@ def test_setup_tools_keep_optionals():
"optional_deps.opt": [],
"optional_deps.not_opt": ["alchemy-logging"],
}


def test_intermediate_extras():
"""Make sure that intermediate extras correctly own unique dependencies that
belong to their children
"""
requirements, extras_require = parse_requirements(
["alchemy-logging", "PyYAML"],
"intermediate_extras",
["intermediate_extras.foo", "intermediate_extras.bar"],
)
assert not requirements
assert extras_require == {
"all": sorted(["alchemy-logging", "PyYAML"]),
"intermediate_extras.foo": sorted(["alchemy-logging", "PyYAML"]),
"intermediate_extras.bar": [],
}


def test_intermediate_extras_with_overlap():
"""Make sure that intermediate extras correctly own unique dependencies that
belong to their children, even when other children are held as overlapping
extras.
"""
requirements, extras_require = parse_requirements(
["alchemy-logging", "PyYAML"],
"intermediate_extras",
[
"intermediate_extras.foo",
"intermediate_extras.foo.bat",
"intermediate_extras.bar",
],
)
assert not requirements
assert extras_require == {
"all": sorted(["alchemy-logging", "PyYAML"]),
"intermediate_extras.foo": sorted(["alchemy-logging", "PyYAML"]),
"intermediate_extras.foo.bat": sorted(["PyYAML"]),
"intermediate_extras.bar": [],
}

0 comments on commit 16e7322

Please sign in to comment.