Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- #937 Preserve non-ASCII argument expressions and default values when changing function signatures (@Jinyong-Li)
- #826, #940 Keep star imports in other modules when inlining a function or method (@chinghar)
- #948, #949 Don't treat string prefixes such as the `f` in `f"..."` as occurrences when renaming a variable named `f`, `b`, `u`, `rf`, etc. (@aliassar)
- #962 Inherit class attributes and type hints along the C3 method resolution order, add `get_mro()` and `get_consistent_mro()` to classes (@marlon-costa-dc)

# Release 1.15.1

Expand Down
4 changes: 4 additions & 0 deletions rope/base/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class BadIdentifierError(RopeError):
"""The name cannot be resolved"""


class InconsistentHierarchyError(RopeError):
"""The class hierarchy has no method resolution order"""


class ModuleSyntaxError(RopeError):
"""Module has syntax errors

Expand Down
34 changes: 25 additions & 9 deletions rope/base/oi/type_hinting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ def get_super_func(pyfunc):
if not isinstance(pyfunc.parent, PyClass):
return

for cls in get_mro(pyfunc.parent)[1:]:
try:
superfunc = cls.get_attribute(pyfunc.get_name()).get_object()
except AttributeNotFoundError:
pass
else:
for attributes in _get_super_attributes(pyfunc.parent):
if pyfunc.get_name() in attributes:
superfunc = attributes[pyfunc.get_name()].get_object()
if isinstance(superfunc, PyFunction):
return superfunc

Expand All @@ -34,9 +31,9 @@ def get_super_assignment(pyname):
except TypeError:
return
else:
for super_pyclass in get_mro(pyclass)[1:]:
if attr_name in super_pyclass:
return super_pyclass[attr_name]
for attributes in _get_super_attributes(pyclass):
if attr_name in attributes:
return attributes[attr_name]


def get_class_with_attr_name(pyname):
Expand Down Expand Up @@ -65,6 +62,25 @@ def get_lineno_for_node(assign_node):
return 1


def _get_super_attributes(pyclass):
"""Yield the attributes of the ancestors of `pyclass` in lookup order

Each ancestor contributes the attributes it defines itself, in
method resolution order. When the hierarchy has no method
resolution order, each ancestor in `get_mro()` contributes all of
its attributes.

"""
mro = pyclass.get_consistent_mro()
if mro is None:
for cls in get_mro(pyclass)[1:]:
yield cls.get_attributes()
else:
for cls in mro[1:]:
if isinstance(cls, PyClass):
yield cls._get_structural_attributes()


def get_mro(pyclass):
# FIXME: to use real mro() result
class_list = [pyclass]
Expand Down
52 changes: 52 additions & 0 deletions rope/base/pyobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,58 @@ def get_doc(self):
def get_superclasses(self):
return []

def get_mro(self):
"""Return the method resolution order of this class

The order is the C3 linearization Python uses for ``__mro__``,
starting with this class. Raises
`rope.base.exceptions.InconsistentHierarchyError` for cyclic,
repeated or unmergeable bases, which Python refuses to create.

"""
return _linearize(self, [])

def get_consistent_mro(self):
"""Return `get_mro()`, or `None` when the hierarchy is inconsistent"""
try:
return self.get_mro()
except exceptions.InconsistentHierarchyError:
return None


def _linearize(pyclass, subclasses):
if pyclass in subclasses:
raise exceptions.InconsistentHierarchyError(
"Cyclic inheritance in class %s" % pyclass.get_name()
)
bases = pyclass.get_superclasses()
for index, base in enumerate(bases):
if base in bases[:index]:
raise exceptions.InconsistentHierarchyError(
"Duplicate base class %s in class %s"
% (base.get_name(), pyclass.get_name())
)
sequences = [_linearize(base, subclasses + [pyclass]) for base in bases]
sequences.append(list(bases))
result = [pyclass]
while True:
sequences = [sequence for sequence in sequences if sequence]
if not sequences:
return result
for sequence in sequences:
head = sequence[0]
if not any(head in other[1:] for other in sequences):
break
else:
raise exceptions.InconsistentHierarchyError(
"Cannot create a consistent method resolution order for class %s"
% pyclass.get_name()
)
result.append(head)
for sequence in sequences:
if sequence[0] is head:
del sequence[0]


class AbstractFunction(PyObject):
def __init__(self):
Expand Down
12 changes: 10 additions & 2 deletions rope/base/pyobjectsdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,16 @@ def get_name(self):

def _create_concluded_attributes(self):
result = {}
for base in reversed(self.get_superclasses()):
result.update(base.get_attributes())
mro = self.get_consistent_mro()
if mro is None:
for base in reversed(self.get_superclasses()):
result.update(base.get_attributes())
return result
for ancestor in reversed(mro[1:]):
if isinstance(ancestor, pyobjects.PyClass):
result.update(ancestor._get_structural_attributes())
else:
result.update(ancestor.get_attributes())
return result

def _get_bases(self):
Expand Down
118 changes: 118 additions & 0 deletions ropetest/pycoretest.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,124 @@ def f(self):
derived = mod["Derived"].get_object()
self.assertTrue("f" in derived)

def test_get_mro_matches_python(self):
hierarchy = [
("O", []),
("A", ["O"]),
("B", ["O"]),
("C", ["O"]),
("D", ["O"]),
("E", ["O"]),
("K1", ["A", "B", "C"]),
("K2", ["D", "B", "E"]),
("K3", ["D", "A"]),
("Z", ["K1", "K2", "K3"]),
]
code = "".join(
"class %s(%s):\n pass\n" % (name, ", ".join(bases))
for name, bases in hierarchy
)
mod = libutils.get_string_module(self.project, code)
classes = {}
for name, bases in hierarchy:
classes[name] = type(name, tuple(classes[base] for base in bases), {})
for name, _ in hierarchy:
expected = [
cls.__name__ for cls in classes[name].__mro__ if cls is not object
]
mro = mod[name].get_object().get_mro()
self.assertEqual(expected, [cls.get_name() for cls in mro])

def test_inheriting_attributes_along_the_mro(self):
code = dedent("""\
class A(object):
def method(self):
pass
class B(A):
pass
class C(A):
def method(self):
pass
class D(B, C):
pass
""")
mod = libutils.get_string_module(self.project, code)
c_class = mod["C"].get_object()
d_class = mod["D"].get_object()
self.assertEqual(
["D", "B", "C", "A", "object"],
[cls.get_name() for cls in d_class.get_mro()],
)
self.assertEqual(c_class["method"].get_object(), d_class["method"].get_object())

def test_inconsistent_mro(self):
code = dedent("""\
class A(object):
def method(self):
pass
class B(A):
def method(self):
pass
class C(A, B):
pass
""")
mod = libutils.get_string_module(self.project, code)
a_class = mod["A"].get_object()
c_class = mod["C"].get_object()
with self.assertRaises(exceptions.InconsistentHierarchyError):
c_class.get_mro()
self.assertIsNone(c_class.get_consistent_mro())
self.assertEqual(a_class["method"].get_object(), c_class["method"].get_object())

def test_inheriting_inconsistent_mro(self):
code = dedent("""\
class A(object):
def method(self):
pass
class B(A):
pass
class C(A, B):
pass
class D(C):
pass
""")
mod = libutils.get_string_module(self.project, code)
d_class = mod["D"].get_object()
with self.assertRaises(exceptions.InconsistentHierarchyError):
d_class.get_mro()
self.assertIsNone(d_class.get_consistent_mro())
self.assertTrue("method" in d_class)

def test_duplicate_base_class_mro(self):
code = dedent("""\
class A(object):
def method(self):
pass
class B(A, A):
pass
""")
mod = libutils.get_string_module(self.project, code)
b_class = mod["B"].get_object()
with self.assertRaises(exceptions.InconsistentHierarchyError):
b_class.get_mro()
self.assertIsNone(b_class.get_consistent_mro())
self.assertTrue("method" in b_class)

def test_cyclic_inheritance_mro(self):
code = dedent("""\
class A(B):
pass
class B(A):
def method(self):
pass
""")
mod = libutils.get_string_module(self.project, code)
a_class = mod["A"].get_object()
with self.assertRaises(exceptions.InconsistentHierarchyError):
a_class.get_mro()
self.assertIsNone(a_class.get_consistent_mro())
self.assertTrue("method" in a_class)

def test_module_creation(self):
new_module = testutils.create_module(self.project, "module")
self.assertFalse(new_module.is_folder())
Expand Down
45 changes: 45 additions & 0 deletions ropetest/type_hinting_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ def a_method(self):
result = self._assist(code)
self.assert_completion_in_result("is_alive", "attribute", result)

def test_hierarchical_hint_rtype_follows_mro(self):
code = dedent('''\
class IBase(object):
def b_method(self):
""":rtype: str"""


class ILeft(IBase):
pass


class IRight(IBase):
def b_method(self):
""":rtype: threading.Thread"""


class Sample(ILeft, IRight):
def b_method(self):
pass
def a_method(self):
self.b_method().is_a''')
result = self._assist(code)
self.assert_completion_in_result("is_alive", "attribute", result)


def fix_indents(hint):
return indent(hint, " " * 12).strip()
Expand Down Expand Up @@ -116,6 +140,27 @@ def a_method(self):
result = self._assist(code)
self.assert_completion_in_result("is_alive", "attribute", result)

def test_hierarchical_hint_attr_follows_mro(self):
code = dedent(f"""\
class IBase(object):
{fix_indents(self._make_class_hint("str"))}


class ILeft(IBase):
pass


class IRight(IBase):
{fix_indents(self._make_class_hint("threading.Thread"))}


class Sample(ILeft, IRight):
a_attr = None
def a_method(self):
self.a_attr.is_a""")
result = self._assist(code)
self.assert_completion_in_result("is_alive", "attribute", result)

def test_hint_defined_by_constructor(self):
code = dedent(f"""\
class Sample(object):
Expand Down
Loading