-
Notifications
You must be signed in to change notification settings - Fork 2k
EM101 fix rebinds or shadows a variable named msg #24335
Copy link
Copy link
Open
Description
Summary
The fix for raw-string-in-exception (EM101) can change the program’s behavior when a variable named msg is already in scope. The function fresh_binding_name added in #24316 may help.
$ cat >em101_1.py <<'# EOF'
def f():
msg = "."
try:
raise RuntimeError("!")
except RuntimeError:
return msg
print(f())
# EOF
$ python em101_1.py
.
$ ruff --isolated check em101_1.py --select EM101 --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).
$ cat em101_1.py
def f():
msg = "."
try:
msg = "!"
raise RuntimeError(msg)
except RuntimeError:
return msg
print(f())
$ python em101_1.py
!$ cat >em101_2.py <<'# EOF'
msg = "."
def f():
try:
raise RuntimeError("!")
except RuntimeError:
return msg
print(f())
# EOF
$ python em101_2.py
.
$ ruff --isolated check em101_2.py --select EM101 --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).
$ cat em101_2.py
msg = "."
def f():
try:
msg = "!"
raise RuntimeError(msg)
except RuntimeError:
return msg
print(f())
$ python em101_2.py
!Version
ruff 0.15.8 (c2a8815 2026-03-26)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels