Skip to content

Commit

Permalink
Fix a bug in pytype's handling of classes with the same name.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 333598812
  • Loading branch information
rchen152 committed Sep 29, 2020
1 parent 62b9bd1 commit 584fdb0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pytype/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,13 @@ def _get_attribute_from_super_instance(
valself = obj.super_obj.to_binding(node)
# When multiple inheritance is present, the two classes' MROs may differ.
# In this case, we want to use the MRO of starting_cls but skip all the
# classes up to and including current_cls.
# classes up to and including current_cls. We traverse the MRO backwards
# to correctly handle the case in which the starting and current classes
# are different classes with the same name.
skip = set()
for parent in starting_cls.mro:
skip.add(parent)
for i, parent in enumerate(reversed(starting_cls.mro)):
if parent.full_name == current_cls.full_name:
skip.update(starting_cls.mro[:len(starting_cls.mro)-i])
break
else:
starting_cls = self.vm.convert.super_type
Expand Down
10 changes: 10 additions & 0 deletions pytype/tests/test_super.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,15 @@ def __init__(self):
""")
self.assertErrorRegexes(errors, {"e": r"super"})

def test_duplicate_class_names(self):
self.Check("""
class Foo(object):
def __new__(self, *args, **kwargs):
typ = type('Foo', (Foo,), {})
return super(Foo, typ).__new__(typ)
def __init__(self, x):
super(Foo, self).__init__()
""")


test_base.main(globals(), __name__ == "__main__")

0 comments on commit 584fdb0

Please sign in to comment.