Skip to content

Don't rename string prefixes that match the renamed name - #949

Merged
lieryan merged 2 commits into
python-rope:masterfrom
aliassar:fix-rename-string-prefix
Nov 13, 2026
Merged

lieryan merged 2 commits into
python-rope:masterfrom
aliassar:fix-rename-string-prefix

Conversation

@aliassar

Copy link
Copy Markdown
Contributor

Description

The occurrence regex in _TextualFinder matched \bname\b, so renaming a variable called f also hit the f of every f"..." prefix (same for b, u, rf, ...) and produced code like fh.write(fh"{r}"). In valid Python a name directly followed by a quote can only be a string prefix, so the pattern now has a (?!["']) lookahead. Names inside the replacement fields are still renamed. docs=True uses a plain substring search with the same problem; I left it alone because there a quoted 'f' in a docstring is a real match. #906 would also fix this as part of its move to the tokenizer (I checked and it gives the same output on these cases), so if that lands first I can drop the code change and keep just the tests.

Two new tests in renametest.py fail on master and pass now. The full suite on 3.13 gives 2170 passed, 7 skipped, 5 xfailed; the one error, test_get_package_tuple_compiled, also happens on master.

Fixes #948

Checklist (delete if not relevant):

  • I have added tests that prove my fix is effective or that my feature works
  • I have updated CHANGELOG.md

The textual occurrence pattern matched `\bname\b`, so for a variable
called `f` it also matched the `f` of an f-string prefix (and likewise
`b`, `u`, `rf`, ...). Rename rewrote `f"{f}"` into `g"{g}"` and inline
produced `1"{1}"`, both syntax errors.

A name directly followed by a quote can only be a string prefix, so
skip those matches and let the string patterns consume the literal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aliassar
aliassar force-pushed the fix-rename-string-prefix branch from 7dd4b6d to 22a54c2 Compare November 10, 2026 21:12
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.44%. Comparing base (162d60b) to head (7d77aa7).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #949   +/-   ##
=======================================
  Coverage   95.44%   95.44%           
=======================================
  Files         134      134           
  Lines       26902    26912   +10     
=======================================
+ Hits        25677    25687   +10     
  Misses       1225     1225           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lieryan
lieryan enabled auto-merge November 13, 2026 20:35
@lieryan
lieryan merged commit 44f5b42 into python-rope:master Nov 13, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Renaming a variable named f also renames the f prefix of f-strings

2 participants