Skip to content

Bug with f-string + brackets + quotes #435

@bbcVld

Description

@bbcVld

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUnexpected or incorrect user-visible behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions