Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idea to get rid of a address map pop. #21732

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/python/pants/engine/internals/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from pants.engine.env_vars import EnvironmentVars
from pants.engine.internals.defaults import BuildFileDefaultsParserState, SetDefaultsT
from pants.engine.internals.dep_rules import BuildFileDependencyRulesParserState
from pants.engine.internals.target_adaptor import TargetAdaptor
from pants.engine.internals.target_adaptor import TargetAdaptor, SyntheticTargetAdaptor
from pants.engine.target import Field, ImmutableValue, RegisteredTargetTypes
from pants.engine.unions import UnionMembership
from pants.util.docutil import doc_url
Expand Down Expand Up @@ -334,7 +334,10 @@ def __call__(self, **kwargs: Any) -> TargetAdaptor:
kwargs["__description_of_origin__"] = f"{self._parse_state.filepath()}:{source_line}"
raw_values = dict(self._parse_state.defaults.get(self._type_alias))
raw_values.update(kwargs)
target_adaptor = TargetAdaptor(self._type_alias, **raw_values)
if not raw_values.pop("_extend_synthetic", False):
target_adaptor = TargetAdaptor(self._type_alias, **raw_values)
else:
target_adaptor = SyntheticTargetAdaptor(self._type_alias, **raw_values)
self._parse_state.add(target_adaptor)
return target_adaptor

Expand Down
4 changes: 2 additions & 2 deletions src/python/pants/engine/internals/synthetic_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def rules(cls) -> Iterator[UnionRule]:
class SyntheticAddressMap(AddressMap):
def process_declared_targets(self, address_map: AddressMap) -> None:
for name, target_adaptor in address_map.name_to_target_adaptor.items():
extend_synthetic = target_adaptor.kwargs.pop("_extend_synthetic", False)
extend_synthetic = isinstance(target_adaptor, SyntheticTargetAdaptor)
if name not in self.name_to_target_adaptor:
if extend_synthetic:
raise InvalidTargetException(
Expand All @@ -169,7 +169,7 @@ def process_declared_targets(self, address_map: AddressMap) -> None:

# Pop synthetic target to let the explicit target declared in BUILD file take
# precedence.
synthetic_target_adaptor = self.name_to_target_adaptor.pop(name)
synthetic_target_adaptor = self.name_to_target_adaptor.pop(name) # TODO: this here is another mutating operation!
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to do with this one, though.. :/


if not extend_synthetic:
# The explicitly declared target should replace the synthetic one.
Expand Down
11 changes: 10 additions & 1 deletion src/python/pants/engine/internals/target_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def debug_hint(self) -> str:
return self.address.spec


@final
# @final -- we mark this class final further down.
class TargetAdaptor:
"""A light-weight object to store target information before being converted into the Target
API."""
Expand Down Expand Up @@ -124,3 +124,12 @@ def __eq__(self, other: Any | TargetAdaptor) -> bool:
@property
def name_explicitly_set(self) -> bool:
return self.name is not None


@final
class SyntheticTargetAdaptor(TargetAdaptor):
"""Subclass to indicate this target is used to extend a synthetic target only."""


TargetAdaptor = final(TargetAdaptor)

Loading