Skip to content

Commit 15542b1

Browse files
committed
Fully resolve pytd aliases in LookupExternalType's duplicate aliases check.
This fixes a pytype test failure in python/typeshed#8469. PiperOrigin-RevId: 464935418
1 parent c59cdb0 commit 15542b1

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

pytype/pytd/visitors.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,31 @@ def _DiscardExistingNames(self, node, potential_members):
556556
new_members.append(m)
557557
return new_members
558558

559+
def _ResolveAlias(self, alias):
560+
if not isinstance(alias.type, pytd.NamedType):
561+
return alias.type
562+
try:
563+
module, _ = self._LookupModuleRecursive(alias.type.name)
564+
except KeyError:
565+
return alias.type
566+
try:
567+
value = module.Lookup(alias.type.name)
568+
except KeyError:
569+
return alias.type
570+
if isinstance(value, pytd.Alias):
571+
return self._ResolveAlias(value)
572+
else:
573+
return value
574+
575+
def _EquivalentAliases(self, alias1, alias2) -> bool:
576+
if alias1 == alias2:
577+
return True
578+
if (isinstance(alias1.type, pytd.Module) and
579+
isinstance(alias2.type, pytd.Module) and
580+
alias1.type.module_name == alias2.type.module_name):
581+
return True
582+
return self._ResolveAlias(alias1) == self._ResolveAlias(alias2)
583+
559584
def _HandleDuplicates(self, new_aliases):
560585
"""Handle duplicate module-level aliases.
561586
@@ -572,13 +597,6 @@ def _HandleDuplicates(self, new_aliases):
572597
Raises:
573598
KeyError: If there is a name clash.
574599
"""
575-
def SameModuleName(a, b):
576-
return (
577-
isinstance(a.type, pytd.Module) and
578-
isinstance(b.type, pytd.Module) and
579-
a.type.module_name == b.type.module_name
580-
)
581-
582600
name_to_alias = {}
583601
out = []
584602
for a in new_aliases:
@@ -587,10 +605,12 @@ def SameModuleName(a, b):
587605
out.append(a)
588606
continue
589607
existing = name_to_alias[a.name]
590-
if existing == a or SameModuleName(existing, a):
608+
if self._EquivalentAliases(existing, a):
591609
continue
610+
existing_name = existing.type.name or existing.type.__class__.__name__
611+
a_name = a.type.name or a.type.__class__.__name__
592612
raise KeyError("Duplicate top level items: {!r}, {!r}".format(
593-
existing.type.name, a.type.name))
613+
existing_name, a_name))
594614
return out
595615

596616
def EnterTypeDeclUnit(self, node):

0 commit comments

Comments
 (0)