Skip to content

Commit

Permalink
Include transitive requirements missing for python_distribution. (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
kaos authored Dec 2, 2022
1 parent 298409b commit 991878e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/python/pants/backend/python/goals/setup_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
from pants.util.logging import LogLevel
from pants.util.memo import memoized_property
from pants.util.meta import frozen_after_init
from pants.util.ordered_set import FrozenOrderedSet
from pants.util.ordered_set import FrozenOrderedSet, OrderedSet
from pants.util.strutil import softwrap

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -862,6 +862,15 @@ async def get_requirements(
direct_deps_tgts = await MultiGet(
Get(Targets, DependenciesRequest(tgt.get(Dependencies))) for tgt in owned_by_us
)
direct_deps_chained = OrderedSet(itertools.chain.from_iterable(direct_deps_tgts))
# If a python_requirement T has an undeclared requirement R, we recommend fixing that by adding
# an explicit dependency from T to a python_requirement target for R. In that case we want to
# represent these explicit deps in T's distribution metadata. See issue #17593.
transitive_explicit_reqs = await MultiGet(
Get(TransitiveTargets, TransitiveTargetsRequest([tgt.address]))
for tgt in direct_deps_chained
if tgt.has_field(PythonRequirementsField)
)

transitive_excludes: FrozenOrderedSet[Target] = FrozenOrderedSet()
uneval_trans_excl = [
Expand All @@ -875,7 +884,9 @@ async def get_requirements(
itertools.chain.from_iterable(excludes for excludes in nested_trans_excl)
)

direct_deps_chained = FrozenOrderedSet(itertools.chain.from_iterable(direct_deps_tgts))
direct_deps_chained.update(
itertools.chain.from_iterable(t.dependencies for t in transitive_explicit_reqs)
)
direct_deps_with_excl = direct_deps_chained.difference(transitive_excludes)

req_strs = list(
Expand Down
44 changes: 44 additions & 0 deletions src/python/pants/backend/python/goals/setup_py_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,50 @@ def test_get_requirements_with_exclude() -> None:
)


def test_get_requirements_with_override_dependency_issue_17593() -> None:
rule_runner = create_setup_py_rule_runner(
rules=[
determine_explicitly_provided_setup_kwargs,
get_requirements,
get_owned_dependencies,
get_exporting_owner,
*target_types_rules.rules(),
SubsystemRule(SetupPyGeneration),
QueryRule(ExportedTargetRequirements, (DependencyOwner,)),
]
)
rule_runner.write_files(
{
"3rdparty/BUILD": textwrap.dedent(
"""
python_requirement(name='ext1', requirements=['ext1==1.22.333'], dependencies=[':ext2'])
python_requirement(name='ext2', requirements=['ext2==4.5.6'])
"""
),
"src/python/foo/bar/baz/a.py": "",
"src/python/foo/bar/baz/BUILD": "python_sources(dependencies=['3rdparty:ext1'])",
"src/python/foo/bar/a.py": "",
"src/python/foo/bar/BUILD": textwrap.dedent(
"""
python_distribution(
name='bar-dist',
dependencies=[':bar'],
provides=python_artifact(name='bar', version='9.8.7'),
)
python_sources(dependencies=['src/python/foo/bar/baz'])
"""
),
}
)

assert_requirements(
rule_runner,
["ext1==1.22.333", "ext2==4.5.6"],
Address("src/python/foo/bar", target_name="bar-dist"),
)


def assert_requirements(
rule_runner,
expected_req_strs,
Expand Down

0 comments on commit 991878e

Please sign in to comment.