Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with f-string + brackets + quotes #435

Closed
bbcVld opened this issue Oct 11, 2021 · 1 comment · Fixed by #643
Closed

Bug with f-string + brackets + quotes #435

bbcVld opened this issue Oct 11, 2021 · 1 comment · Fixed by #643
Labels
bug Unexpected or incorrect user-visible behavior
Milestone

Comments

@bbcVld
Copy link

bbcVld commented Oct 11, 2021

Describe the bug
If code has a special string (see code below) the rope falls with error.

To Reproduce
Steps to reproduce the behavior:

  1. Code before refactoring:
aa = 1
bb = 2

cc = f"({aa}, {bb})"  # ok
cc = f"{aa}, '{bb}'"  # ok
cc = "({aa}, '{bb}')"  # ok
cc = r"({aa}, '{bb}')"  # ok
cc = f"({aa}), '{bb}'"  # ok
cc = f"({aa}, 'bb')"  # ok
cc = f"(aa, '{bb}')"  # gotcha
cc = f"({aa}, '{bb}')"  # gotcha

# this is the actual code where I found the issue:
list_of_tuples = [(1,2), (3,4), (5,6)]
gotha = [f"({aa}, '{bb}')" for aa, bb in list_of_tuples]


def pow(x, y):
    result = 1
    for i in range(y):
        result *= x
    return result

a = pow(2, 3)  # example from tutorail. Try to change to 2 ** 3

  1. Describe the refactoring you want to do
    I'm trying to Restructure code.

  2. Expected code after refactoring:
    --

  3. Describe the error or unexpected result that you are getting

in file test.py
Traceback (most recent call last):
  File ".../rope/refactor/patchedast.py", line 866, in consume
    new_offset = self.source.index(token, self.offset)
ValueError: substring not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../rope_test/tmp0r3rcc", line 12, in <module>

    changes = Restructure(myproject, 'pow', 'pow').get_changes()
  File ".../rope/refactor/restructure.py", line 143, in get_changes
    wildcards=self.wildcards)
  File ".../rope/refactor/similarfinder.py", line 29, in __init__
    pymodule.source_code, pymodule.get_ast(), self._does_match)
  File ".../rope/refactor/similarfinder.py", line 82, in __init__
    self._init_using_ast(node, source)
  File ".../rope/refactor/similarfinder.py", line 91, in _init_using_ast
    patchedast.patch_ast(node, source)
  File ".../rope/refactor/patchedast.py", line 41, in patch_ast
    ast.call_for_nodes(node, walker)
  File ".../rope/base/ast.py", line 63, in call_for_nodes
    result = callback(node)
  File ".../rope/refactor/patchedast.py", line 87, in __call__
    return method(node)
  File ".../rope/refactor/patchedast.py", line 664, in _Module
    self._handle(node, list(node.body), eat_spaces=True)
  File ".../rope/refactor/patchedast.py", line 115, in _handle
    ast.call_for_nodes(child, self)
  File ".../rope/base/ast.py", line 63, in call_for_nodes
    result = callback(node)
  File ".../rope/refactor/patchedast.py", line 87, in __call__
    return method(node)
  File ".../rope/refactor/patchedast.py", line 265, in _Assign
    self._handle(node, children)
  File ".../rope/refactor/patchedast.py", line 115, in _handle
    ast.call_for_nodes(child, self)
  File ".../rope/base/ast.py", line 63, in call_for_nodes
    result = callback(node)
  File ".../rope/refactor/patchedast.py", line 87, in __call__
    return method(node)
  File ".../rope/refactor/patchedast.py", line 667, in _Name
    self._handle(node, [node.id])
  File ".../rope/refactor/patchedast.py", line 143, in _handle
    region = self.source.consume(child)
  File ".../rope/refactor/patchedast.py", line 874, in consume
    (token, self._get_location()))
rope.refactor.patchedast.MismatchedTokenError: Token <cc> at (11, 21) cannot be matched

Editor information (please complete the following information):

  • Project Python version: 3.7.6
  • Rope Python version: 0.20.1
  • Rope version: ...
  • Text editor/IDE and version: Sublime Text Build 3207 (with PyRefactor)

Additional context
I got feeling that it is some boundary case.

@bbcVld bbcVld added the bug Unexpected or incorrect user-visible behavior label Oct 11, 2021
@lieryan lieryan added this to the 0.22.x milestone Oct 18, 2021
@lieryan lieryan modified the milestones: 0.22.x, 0.23.0, 0.24.0 Nov 22, 2021
@lieryan lieryan modified the milestones: 1.1.0, 1.2.0 Apr 14, 2022
@lieryan lieryan modified the milestones: 1.2.0, 1.3.0 Jun 17, 2022
@lieryan lieryan modified the milestones: 1.3.0, 1.4.0 Jul 12, 2022
@lieryan lieryan modified the milestones: 1.4.0, 1.5.0 Oct 21, 2022
@lieryan lieryan modified the milestones: 1.5.0, 1.6.0 Nov 22, 2022
@lieryan lieryan modified the milestones: 1.6.0, 1.7.0 Dec 15, 2022
@apmorton
Copy link
Contributor

apmorton commented Jan 5, 2023

Encountered this in my own codebase - here is a minimal test to be added in RenameRefactoringTest to reproduce the error:

    def test_rename_in_module_bug(self):
        mod1 = testutils.create_module(self.project, "mod1")
        mod1.write(
            dedent("""\
                a = 10
                print(1+a)

                def bar():
                    return f'('
            """)
        )
        pymod = self.project.get_module("mod1")
        old_pyname = pymod["a"]
        finder = rope.refactor.occurrences.create_finder(self.project, "a", old_pyname)
        rename.rename_in_module(
            finder, "new_var", pymodule=pymod, replace_primary=True
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected or incorrect user-visible behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants