Skip to content
Merged
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
@@ -1,5 +1,6 @@
# **Upcoming release**

- #777, #698 add validation to refuse Rename refactoring to a python keyword
- #730 Match on module aliases for autoimport suggestions
- #755 Remove dependency on `build` package being installed while running tests

Expand Down
12 changes: 12 additions & 0 deletions rope/refactor/rename.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warnings
from keyword import iskeyword

from rope.base import (
codeanalyze,
Expand Down Expand Up @@ -105,6 +106,7 @@ def unsure_func(value=unsure):
resources = [self.resource]
if resources is None:
resources = self.project.get_python_files()
self.validate_changes(new_name)
changes = ChangeSet(f"Renaming <{self.old_name}> to <{new_name}>")
finder = occurrences.create_finder(
self.project,
Expand All @@ -128,6 +130,16 @@ def unsure_func(value=unsure):
self._rename_module(resource, new_name, changes)
return changes

def validate_changes(
self,
new_name: str,
**_unused,
):
if iskeyword(new_name):
raise exceptions.RefactoringError(
f"Invalid refactoring target name. '{new_name}' is a Python keyword."
)

def _is_allowed_to_move(self, resources, resource):
if resource.is_folder():
try:
Expand Down
5 changes: 5 additions & 0 deletions ropetest/refactor/renametest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import unittest
from textwrap import dedent
from rope.base import exceptions

import rope.base.codeanalyze
import rope.refactor.occurrences
Expand Down Expand Up @@ -1390,6 +1391,10 @@ def test_renaming_modules_aliased_many_dots(self):
)
self.assertEqual("import new_json.utils.a as stdlib_json_utils\n", mod2.read())

def test_rename_refuses_renaming_to_python_keyword(self):
with self.assertRaises(exceptions.RefactoringError, msg="Invalid refactoring target name. 'class' is a Python keyword."):
self._local_rename("a_var = 20\n", 2, "class")


class RenameRefactoringWithSuperclassTest(RenameTestMixin, unittest.TestCase):
ORIGINAL_CODE = dedent("""\
Expand Down