Skip to content

Commit

Permalink
fix: properly create loader, properly sort for use of qualname
Browse files Browse the repository at this point in the history
Creating a lambda in the dictcomp means that the lambda captured a reference to the iterator,
not the value it contained at that time.
Every call to the distinct lambdas would reference the last value processed.
  • Loading branch information
lilatomic committed Apr 29, 2024
1 parent 99b5bf5 commit 74d20cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
40 changes: 22 additions & 18 deletions src/python/pants/help/help_info_extracter.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,25 +838,29 @@ def _extract_api_types() -> Iterator[tuple[type, str, tuple[type, ...]]]:
),
)

def get_api_type_info(api_types: list[type]) -> PluginAPITypeInfo:
"""
Gather the info from each of the types and aggregate it.
def get_api_type_info(api_types: list[type]):
"""Gather the info from each of the types and aggregate it.
The gathering is the expensive operation, and we can only aggregate once we've gathered.
"""
infos = [
PluginAPITypeInfo.create(
api_type,
rules,
provider=type_graph[api_type]["providers"],
dependencies=type_graph[api_type]["dependencies"],
dependents=type_graph[api_type].get("dependents", ()),
union_members=tuple(
sorted(member.__qualname__ for member in union_membership.get(api_type))
),
)
for api_type in api_types
]
return reduce(lambda x, y: x.merged_with(y), infos)

def load() -> PluginAPITypeInfo:
gatherered_infos = [
PluginAPITypeInfo.create(
api_type,
rules,
provider=type_graph[api_type]["providers"],
dependencies=type_graph[api_type]["dependencies"],
dependents=type_graph[api_type].get("dependents", ()),
union_members=tuple(
sorted(member.__qualname__ for member in union_membership.get(api_type))
),
)
for api_type in api_types
]
return reduce(lambda x, y: x.merged_with(y), gatherered_infos)

return load

# We want to provide a lazy dict so we don't spend so long doing the info gathering.
# We provide a list of the types here, and the lookup function performs the gather and the aggregation
Expand All @@ -867,7 +871,7 @@ def get_api_type_info(api_types: list[type]) -> PluginAPITypeInfo:
)

infos: dict[str, Callable[[], PluginAPITypeInfo]] = {
k: lambda: get_api_type_info(v) for k, v in names_to_types.items()
k: get_api_type_info(v) for k, v in names_to_types.items()
}

return LazyFrozenDict(infos)
Expand Down
44 changes: 22 additions & 22 deletions src/python/pants/help/help_info_extracter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,21 +531,19 @@ def fake_consumed_scopes_mapper(scope: str) -> Tuple[str, ...]:
},
},
"name_to_api_type_info": {
"pants.help.help_info_extracter_test.test_get_all_help_info.<locals>.Foo": {
"consumed_by_rules": (
"pants.help.help_info_extracter_test.test_get_all_help_info.rule_info_test",
),
"dependents": ("help_info_extracter_test",),
"dependencies": ("pants.option.scope",),
"documentation": None,
"pants.option.scope.Scope": {
"consumed_by_rules": (),
"dependents": (),
"dependencies": (),
"documentation": "An options scope.",
"is_union": False,
"module": "pants.help.help_info_extracter_test",
"name": "Foo",
"provider": ("help_info_extracter_test",),
"returned_by_rules": ("construct_scope_foo",),
"module": "pants.option.scope",
"name": "Scope",
"provider": ("pants.option.scope",),
"returned_by_rules": (),
"union_members": (),
"union_type": None,
"used_in_rules": (),
"used_in_rules": ("construct_scope_foo",),
},
"pants.engine.target.Target": {
"consumed_by_rules": (),
Expand All @@ -569,19 +567,21 @@ def fake_consumed_scopes_mapper(scope: str) -> Tuple[str, ...]:
"union_type": None,
"used_in_rules": (),
},
"pants.option.scope.Scope": {
"consumed_by_rules": (),
"dependents": (),
"dependencies": (),
"documentation": "An options scope.",
"pants.help.help_info_extracter_test.test_get_all_help_info.<locals>.Foo": {
"consumed_by_rules": (
"pants.help.help_info_extracter_test.test_get_all_help_info.rule_info_test",
),
"dependents": ("help_info_extracter_test",),
"dependencies": ("pants.option.scope",),
"documentation": None,
"is_union": False,
"module": "pants.option.scope",
"name": "Scope",
"provider": ("pants.option.scope",),
"returned_by_rules": (),
"module": "pants.help.help_info_extracter_test",
"name": "test_get_all_help_info.<locals>.Foo",
"provider": ("help_info_extracter_test",),
"returned_by_rules": ("construct_scope_foo",),
"union_members": (),
"union_type": None,
"used_in_rules": ("construct_scope_foo",),
"used_in_rules": (),
},
},
"name_to_backend_help_info": {
Expand Down

0 comments on commit 74d20cb

Please sign in to comment.