Skip to content

Commit

Permalink
fix(extract-method): Extracts method when list comprehension within m…
Browse files Browse the repository at this point in the history
…ethod's scope

fixes python-rope#461
  • Loading branch information
dryobates committed May 13, 2022
1 parent 6a86c2f commit a42ee46
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
## New feature
- #464 Improve autoimport code to use a sqllite3 database, cache all available modules quickly, search for names and produce import statements, sort import statements.

## Bug fixes

- #461 Fix bug while extracting method with list comprehension in class method (@dryobates)

# Release 1.0.0

Date: 2022-04-08
Expand Down
3 changes: 3 additions & 0 deletions rope/base/pyobjectsdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def _create_scope(self):
self.pycore, self, _ComprehensionVisitor
)

def get_kind(self):
return "Comprehension"


class PyClass(pyobjects.PyClass):
def __init__(self, pycore, ast_node, parent):
Expand Down
20 changes: 20 additions & 0 deletions ropetest/refactor/extracttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,26 @@ def bar():
""")
self.assertEqual(expected, refactored)

def test_extract_method_with_list_comprehension_in_class_method(self):
code = dedent("""\
class SomeClass:
def method(self):
result = [i for i in range(1)]
print(1)
""")
start, end = self._convert_line_range_to_offset(code, 4, 4)
refactored = self.do_extract_method(code, start, end, "baz", similar=True)
expected = dedent("""\
class SomeClass:
def method(self):
result = [i for i in range(1)]
self.baz()
def baz(self):
print(1)
""")
self.assertEqual(expected, refactored)

def test_extract_method_with_list_comprehension_and_iter(self):
code = dedent("""\
def foo():
Expand Down

0 comments on commit a42ee46

Please sign in to comment.