rope icon indicating copy to clipboard operation
rope copied to clipboard

Rename doesn't work for debug notation inside f-strings

Open ahauan4 opened this issue 4 years ago • 1 comments

Rope fails to rename inside f-strings when followed by equal sign for debugging. See https://realpython.com/lessons/simpler-debugging-f-strings/ And https://bugs.python.org/issue36817

Rope version: 0.17.0 Python version: 3.8.2

Example:

myvar = 6
print(f"{myvar}")    # works
print(f"{myvar=}")   # does not work
print(f"{myvar = }") # does not work

ahauan4 avatar Oct 05 '20 15:10 ahauan4

This is caused by a set of bugs in the standard library's ast module related to f-string, which doesn't produce correct offsets. Ref: https://bugs.python.org/issue39564, https://bugs.python.org/issue37458, https://bugs.python.org/issue35212,

Background: The current version of CPython doesn't fill col_offset correctly because internally f-string are not really part of the main parser's grammar, but rather is parsed in a second pass of a "handwritten f-string parser" that doesn't keep track of those information.

Until that bug is fixed in ast, it likely wouldn't really be possible to fix this in Rope, unless we write our own f-string parser, maybe as some sort of simple, regex-based parser that recognizes some of the most common f-string constructions.

lieryan avatar Dec 05 '20 01:12 lieryan