Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 917c89b

Browse files
authored
fix: ignore disambiguation for repeated entries (#390)
* fix: update disambiguation rule for repeated entry * tests: update unit test
1 parent 2bb98ae commit 917c89b

3 files changed

Lines changed: 51 additions & 11 deletions

File tree

docfx_yaml/extension.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ class Bcolors:
162162
logging.getLogger("blib2to3").setLevel(logging.ERROR)
163163

164164

165+
# Type alias used for yaml entries.
166+
_yaml_type_alias = dict[str, any]
167+
168+
165169
def _grab_repo_metadata() -> Mapping[str, str] | None:
166170
"""Retrieves the repository's metadata info if found."""
167171
try:
@@ -1406,10 +1410,18 @@ def find_unique_name(package_name, entries):
14061410
# If there is no way to disambiguate or we found duplicates, return the identifier name.
14071411
return [package_name[-1]]
14081412

1409-
# Used to disambiguate names that have same entries.
1410-
# Returns a dictionary of names that are disambiguated in the form of:
1411-
# {uidname: disambiguated_name}
1412-
def disambiguate_toc_name(pkg_toc_yaml):
1413+
1414+
def disambiguate_toc_name(
1415+
pkg_toc_yaml: _yaml_type_alias,
1416+
) -> Mapping[str, str]:
1417+
"""Disambiguates names that have same entries in table of contents.
1418+
1419+
Args:
1420+
pkg_toc_yaml: The table of contents for the client library.
1421+
1422+
Returns:
1423+
Mapping from uidname to disambiguated name.
1424+
"""
14131425
name_entries = {}
14141426
disambiguated_names = {}
14151427

@@ -1418,13 +1430,21 @@ def disambiguate_toc_name(pkg_toc_yaml):
14181430
if module_name not in name_entries:
14191431
name_entries[module_name] = {}
14201432

1433+
prev_part = ""
14211434
# Split the name and mark all duplicates.
14221435
# There will be at least 1 unique identifer for each name.
14231436
for part in module['uidname'].split("."):
1424-
if part not in name_entries[module_name]:
1437+
if part not in name_entries[module_name] or (
1438+
# If the name appears in a row, for example:
1439+
# google.cloud.run_v2.services
1440+
# google.cloud.run_v2.services.services
1441+
# then ignore this entry as this is clear from the left-nav layout.
1442+
part == prev_part
1443+
):
14251444
name_entries[module_name][part] = 1
14261445
else:
14271446
name_entries[module_name][part] += 1
1447+
prev_part = part
14281448

14291449
# Some entries don't contain `name` in `uidname`, add these into the map as well.
14301450
if module_name not in name_entries[module_name]:
@@ -1436,7 +1456,7 @@ def disambiguate_toc_name(pkg_toc_yaml):
14361456

14371457
for module in pkg_toc_yaml:
14381458
module_name = module['name']
1439-
# Check if there are multiple entires of module['name'], disambiguate if needed.
1459+
# Check if there are multiple entries of module['name'], disambiguate if needed.
14401460
if name_entries[module_name][module_name] > 1:
14411461
module['name'] = ".".join(find_unique_name(module['uidname'].split("."), name_entries[module_name]))
14421462
disambiguated_names[module['uidname']] = module['name']
@@ -1481,10 +1501,6 @@ def pretty_package_name(package_group):
14811501
return " ".join(capitalized_name)
14821502

14831503

1484-
# Type alias used for yaml entries.
1485-
_yaml_type_alias = dict[str, any]
1486-
1487-
14881504
def _find_and_add_summary_details(
14891505
yaml_data: _yaml_type_alias,
14901506
summary_type: str,

tests/test_unit.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ def test_finds_unique_name(self):
5353
"google.api_core.client_info": "client_info",
5454
"google.api_core.gapic_v1.client_info": "gapic_v1.client_info",
5555
},
56-
]
56+
],
57+
[
58+
# Tests repeated duplicates in a row. No changes expected.
59+
"tests/yaml_repeat_duplicate.yaml",
60+
"tests/yaml_repeat_duplicate.yaml",
61+
{},
62+
],
5763
]
5864
@parameterized.expand(test_entries)
5965
def test_disambiguates_toc_name(

tests/yaml_repeat_duplicate.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"name":"Services",
4+
"uidname":"google.cloud.run_v2.services",
5+
"items":[
6+
{
7+
"name":"Overview",
8+
"uidname":"google.cloud.run_v2.services.services",
9+
"uid":"google.cloud.run_v2.services.services",
10+
},
11+
{
12+
"name":"Pagers",
13+
"uidname":"google.cloud.run_v2.pagers",
14+
"uid":"google.cloud.run_v2.pagers",
15+
}
16+
]
17+
},
18+
]

0 commit comments

Comments
 (0)