Summary
The fix for duplicate-isinstance-call (SIM101) can introduce syntax errors when the duplicated expression is an f-string because it normalizes the expression instead of keeping it unchanged. Example:
$ cat >sim101_1.py <<'# EOF'
isinstance(f"{(lambda: 0)}", int) or isinstance(f"{(lambda: 0)}", str)
isinstance(f"{0:{(lambda: 0)}}", int) or isinstance(f"{0:{(lambda: 0)}}", str)
isinstance(f"{0:\x22}", int) or isinstance(f"{0:\x22}", str)
isinstance(f"{0:\x7b}", int) or isinstance(f"{0:\x7b}", str)
# EOF
$ ruff --isolated check sim101_1.py --select SIM101 --unsafe-fixes --diff 2>&1 | grep error:
error: Fix introduced a syntax error. Reverting all changes.
It can also change the program’s behavior. Example:
$ cat >sim101_2.py <<'# EOF'
from datetime import datetime
d = datetime.min
class OddMeta(type):
def __instancecheck__(self, instance):
return len(instance) % 2
class Odd(metaclass=OddMeta): pass
print(isinstance(f"{d:\x7d}", int) or isinstance(f"{d:\x7d}", Odd))
print(isinstance(f"{d:\x7b \x7d}", int) or isinstance(f"{d:\x7b \x7d}", Odd))
# EOF
$ python sim101_2.py
True
True
$ ruff --isolated check sim101_2.py --select SIM101 --unsafe-fixes --fix
Found 2 errors (2 fixed, 0 remaining).
$ python sim101_2.py
False
False
Version
ruff 0.12.5 (d13228a 2025-07-24)
Summary
The fix for
duplicate-isinstance-call(SIM101) can introduce syntax errors when the duplicated expression is an f-string because it normalizes the expression instead of keeping it unchanged. Example:It can also change the program’s behavior. Example:
Version
ruff 0.12.5 (d13228a 2025-07-24)