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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# **Upcoming release**

- #828 Preserve builtin subclass instances when resolving calls, so inlining an
overridden method updates its calls as well as removing its definition.
- #859 Fix `find_definition`/Rename not resolving `async def` methods called via `self` (@gadievron)
- #895 patchedast Starred and keyword now consumes their syntactically expected and ** (@lieryan)
- #896 patchedast cleanup and refactoring (@lieryan)
Expand Down
7 changes: 6 additions & 1 deletion rope/base/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ def _get_returned(pyobject):
result = None
if "__new__" in pyobject:
new_function = pyobject["__new__"].get_object()
result = _get_returned(new_function)
# Builtin constructors model the builtin type, not its subclasses.
if not (
isinstance(pyobject, pyobjects.PyClass)
and isinstance(new_function, rope.base.builtins.BuiltinFunction)
):
result = _get_returned(new_function)
if result is None or result == rope.base.pyobjects.get_unknown():
result = rope.base.pyobjects.PyObject(pyobject)
self.result = rope.base.pynames.UnboundName(pyobject=result)
Expand Down
49 changes: 49 additions & 0 deletions ropetest/builtinstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,55 @@ def test_simple_case(self):
pymod = self.project.get_pymodule(self.mod)
self.assertTrue("append" in pymod["l"].get_object())

def test_builtin_subclass_constructors(self):
for builtin in ("list", "dict", "tuple", "set", "str", "int"):
with self.subTest(builtin=builtin):
self.mod.write(dedent(f"""\
class Subclass({builtin}):
pass
instance = Subclass()
"""))
pymod = self.project.get_pymodule(self.mod)
self.assertEqual(
pymod["Subclass"].get_object(),
pymod["instance"].get_object().get_type(),
)

def test_inherited_builtin_subclass_constructors(self):
self.mod.write(dedent("""\
class Parent(list):
pass
class Child(Parent):
pass
instance = Child([1, 2])
"""))
pymod = self.project.get_pymodule(self.mod)
self.assertEqual(
pymod["Child"].get_object(), pymod["instance"].get_object().get_type()
)

def test_builtin_subclass_custom_new(self):
self.mod.write(dedent("""\
class Subclass(list):
def __new__(cls):
return {}
instance = Subclass()
"""))
pymod = self.project.get_pymodule(self.mod)
self.assertIsInstance(pymod["instance"].get_object().get_type(), builtins.Dict)

def test_builtin_subclass_inherits_custom_new(self):
self.mod.write(dedent("""\
class Parent(list):
def __new__(cls):
return {}
class Child(Parent):
pass
instance = Child()
"""))
pymod = self.project.get_pymodule(self.mod)
self.assertIsInstance(pymod["instance"].get_object().get_type(), builtins.Dict)

def test_holding_type_information(self):
self.mod.write(dedent("""\
class C(object):
Expand Down
26 changes: 26 additions & 0 deletions ropetest/refactor/inlinetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,32 @@ class A(object):
self.mod.read(),
)

def test_inlining_overridden_builtin_method(self):
code = dedent("""\
class Word(str):
pass
class WordList(list):
def append(self, obj):
super().append(Word(obj))
def extend(self, iterable):
for e in iterable:
self.append(e)
words = WordList()
words.extend(["hello", "world"])
result = [(str(word), type(word).__name__) for word in words]
""")
for offset in (code.index("extend"), code.rindex("extend")):
with self.subTest(offset=offset):
refactored = self._inline(code, offset)
self.assertNotIn("def extend", refactored)
self.assertNotIn("words.extend", refactored)
self.assertIn("words.append(e)", refactored)
namespace = {}
exec(refactored, namespace)
self.assertEqual(
[("hello", "Word"), ("world", "Word")], namespace["result"]
)

def test_simple_returns_with_backslashes(self):
self.mod.write(dedent("""\
def a_func():
Expand Down
Loading