-
Notifications
You must be signed in to change notification settings - Fork 2k
SIM105 should be suppressed for except* in Python 3.11 #23798
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't workingruleImplementing or modifying a lint ruleImplementing or modifying a lint rule
Description
Summary
The fix for suppressible-exception (SIM105) can change the program’s behavior in Python 3.11 for except*. If some code uses except*, it’s probably because a BaseExceptionGroup can be raised. Because contextlib.suppress only gained support for BaseExceptionGroup in Python 3.12, the fix is most likely wrong for except* in Python 3.11. SIM105 should be suppressed in that case: not just the fix, but the whole rule, because there’s generally no way to simplify the code under these circumstances, so it’s not helpful for Ruff to flag it at all. Example:
$ cat >sim105.py <<'# EOF'
def raise_group(x):
raise ExceptionGroup("!", [ValueError(x)])
try:
raise_group(1)
except* ValueError:
pass
# EOF
$ python3.11 sim105.py; echo $?
0
$ ruff --isolated check --select SIM105 sim105.py --unsafe-fixes --fix --target-version py311
Found 1 error (1 fixed, 0 remaining).
$ cat sim105.py
import contextlib
def raise_group(x):
raise ExceptionGroup("!", [ValueError(x)])
with contextlib.suppress(ValueError):
raise_group(1)
$ python3.11 sim105.py 2>&1 | sed 's:/.*/::g'
+ Exception Group Traceback (most recent call last):
| File "sim105.py", line 5, in <module>
| raise_group(1)
| File "sim105.py", line 3, in raise_group
| raise ExceptionGroup("!", [ValueError(x)])
| ExceptionGroup: ! (1 sub-exception)
+-+---------------- 1 ----------------
| ValueError: 1
+------------------------------------Version
ruff 0.15.5 (5e4a3d9 2026-03-05)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingruleImplementing or modifying a lint ruleImplementing or modifying a lint rule