rope icon indicating copy to clipboard operation
rope copied to clipboard

SyntaxError when parsing f-string with Python 3.12 new syntax

Open plutonium-94 opened this issue 7 months ago • 0 comments

Describe the bug When inlining a variable using Rope’s InlineVariable refactor, the library fails to parse an f-string that uses nested single quotes which is introduced with Python 3.12’s new f-string syntax.

To Reproduce Run the code below:

from rope.base.project import Project
from rope.refactor.inline import InlineVariable

project = Project('.', ropefolder=None)
file = project.root.create_file('file.py')
file.write('''\
s = None
print(s)
f'{''}'
''')
changes = InlineVariable(project, file, 0).get_changes('s')
project.do(changes)
print(file.read())
file.remove()
project.close()

And get the following error:

Traceback (most recent call last):
  File "rope_bug.py", line 11, in <module>
    changes = InlineVariable(project, file, 0).get_changes('s')
  File "Lib\site-packages\rope\refactor\inline.py", line 276, in get_changes        
    source = self._change_main_module(remove, only_current, docs)
  File "Lib\site-packages\rope\refactor\inline.py", line 290, in _change_main_module
    return _inline_variable(
        self.project,
    ...<5 lines>...
        docs=docs,
    )
  File "Lib\site-packages\rope\refactor\inline.py", line 628, in _inline_variable   
    changed_source = rename.rename_in_module(
        occurrence_finder,
    ...<4 lines>...
        region=region,
    )
  File "Lib\site-packages\rope\refactor\rename.py", line 243, in rename_in_module   
    for occurrence in occurrences_finder.find_occurrences(resource, pymodule):
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "Lib\site-packages\rope\refactor\occurrences.py", line 83, in find_occurrences
    for offset in self._textual_finder.find_offsets(tools.source_code):
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "Lib\site-packages\rope\refactor\occurrences.py", line 330, in find_offsets
    yield from searcher(source)
  File "Lib\site-packages\rope\refactor\occurrences.py", line 338, in _re_search
    for offset in self._search_in_f_string(f_string):
                  ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "Lib\site-packages\rope\refactor\occurrences.py", line 342, in _search_in_f_string
    tree = ast.parse(f_string)
  File "Lib\site-packages\rope\base\ast.py", line 33, in parse
    return ast.parse(source, filename=filename, *args, **kwargs)
           ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "Lib\ast.py", line 54, in parse
    return compile(source, filename, mode, flags,
                   _feature_version=feature_version, optimize=optimize)
  File "<string>", line 1
    f'{'
       ^
SyntaxError: f-string: expecting '}'

After replacing f'{''}' with f'{""}', there is no error.

Editor information:

  • Project Python version: 3.13.3
  • Rope Python version: 3.13.3
  • Rope version: 1.13.0
  • Text editor/IDE and version: None (rope used as a library)

plutonium-94 avatar May 21 '25 23:05 plutonium-94