diff --git a/src/python/pants/help/help_info_extracter.py b/src/python/pants/help/help_info_extracter.py index fe85a3b475c..6db023f47ca 100644 --- a/src/python/pants/help/help_info_extracter.py +++ b/src/python/pants/help/help_info_extracter.py @@ -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 @@ -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) diff --git a/src/python/pants/help/help_info_extracter_test.py b/src/python/pants/help/help_info_extracter_test.py index 7c3fab9de24..ff248b44a8c 100644 --- a/src/python/pants/help/help_info_extracter_test.py +++ b/src/python/pants/help/help_info_extracter_test.py @@ -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..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": (), @@ -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..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..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": {