---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass`
 --> SIM105_0.py:6:1
  |
5 |   # SIM105
6 | / try:
7 | |     foo()
8 | | except ValueError:
9 | |     pass
  | |________^
  |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
  |
1 + import contextlib
2 | def foo():
--------------------------------------------------------------------------------
6 | # SIM105
  - try:
7 + with contextlib.suppress(ValueError):
8 |     foo()
  - except ValueError:
  -     pass
9 |
  |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`except`-`pass`
  --> SIM105_0.py:13:1
   |
12 |   # SIM105
13 | / try:
14 | |     foo()
15 | | except (ValueError, OSError):
16 | |     pass
   | |________^
17 |
18 |   # SIM105
   |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
   |
1  + import contextlib
2  | def foo():
--------------------------------------------------------------------------------
13 | # SIM105
   - try:
14 + with contextlib.suppress(ValueError, OSError):
15 |     foo()
   - except (ValueError, OSError):
   -     pass
16 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`except`-`pass`
  --> SIM105_0.py:19:1
   |
18 |   # SIM105
19 | / try:
20 | |     foo()
21 | | except (ValueError, OSError) as e:
22 | |     pass
   | |________^
23 |
24 |   # SIM105
   |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
   |
1  + import contextlib
2  | def foo():
--------------------------------------------------------------------------------
19 | # SIM105
   - try:
20 + with contextlib.suppress(ValueError, OSError):
21 |     foo()
   - except (ValueError, OSError) as e:
   -     pass
22 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`pass`
  --> SIM105_0.py:25:1
   |
24 |   # SIM105
25 | / try:
26 | |     foo()
27 | | except:
28 | |     pass
   | |________^
29 |
30 |   # SIM105
   |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
   |
1  + import contextlib
2  | def foo():
--------------------------------------------------------------------------------
25 | # SIM105
   - try:
26 + with contextlib.suppress(BaseException):
27 |     foo()
   - except:
   -     pass
28 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress(a.Error, b.Error)` instead of `try`-`except`-`pass`
  --> SIM105_0.py:31:1
   |
30 |   # SIM105
31 | / try:
32 | |     foo()
33 | | except (a.Error, b.Error):
34 | |     pass
   | |________^
35 |
36 |   # OK
   |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(a.Error, b.Error): ...`
   |
1  + import contextlib
2  | def foo():
--------------------------------------------------------------------------------
31 | # SIM105
   - try:
32 + with contextlib.suppress(a.Error, b.Error):
33 |     foo()
   - except (a.Error, b.Error):
   -     pass
34 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass`
  --> SIM105_0.py:85:5
   |
83 |   def with_ellipsis():
84 |       # OK
85 | /     try:
86 | |         foo()
87 | |     except ValueError:
88 | |         ...
   | |___________^
   |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
   |
1  + import contextlib
2  | def foo():
--------------------------------------------------------------------------------
85 |     # OK
   -     try:
86 +     with contextlib.suppress(ValueError):
87 |         foo()
   -     except ValueError:
   -         ...
88 |
   |
note: This is an unsafe fix and may change runtime behavior

SIM105 Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`except`-`pass`
   --> SIM105_0.py:100:5
    |
 99 |   def with_comment():
100 | /     try:
101 | |         foo()
102 | |     except (ValueError, OSError):
103 | |         pass  # Trailing comment.
    | |____________^
104 |
105 |   try:
    |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`

SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
   --> SIM105_0.py:117:5
    |
115 |   # Regression test for: https://github.com/astral-sh/ruff/issues/7123
116 |   def write_models(directory, Models):
117 | /     try:
118 | |         os.makedirs(model_dir);
119 | |     except OSError:
120 | |         pass;
    | |____________^
121 |
122 |       try: os.makedirs(model_dir);
    |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
    |
1   + import contextlib
2   | def foo():
--------------------------------------------------------------------------------
117 | def write_models(directory, Models):
    -     try:
118 +     with contextlib.suppress(OSError):
119 |         os.makedirs(model_dir);
    -     except OSError:
    -         pass;
120 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
   --> SIM105_0.py:122:5
    |
120 |           pass;
121 |
122 | /     try: os.makedirs(model_dir);
123 | |     except OSError:
124 | |         pass;
    | |____________^
125 |
126 |       try: os.makedirs(model_dir);
    |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
    |
1   + import contextlib
2   | def foo():
--------------------------------------------------------------------------------
122 |
    -     try: os.makedirs(model_dir);
    -     except OSError:
    -         pass;
123 +     with contextlib.suppress(OSError): os.makedirs(model_dir);
124 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
   --> SIM105_0.py:126:5
    |
124 |           pass;
125 |
126 | /     try: os.makedirs(model_dir);
127 | |     except OSError:
128 | |         pass; \
    | |____________^
129 |               \
130 |               #
    |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
    |
1   + import contextlib
2   | def foo():
--------------------------------------------------------------------------------
126 |
    -     try: os.makedirs(model_dir);
    -     except OSError:
    -         pass; \
127 +     with contextlib.suppress(OSError): os.makedirs(model_dir);
128 |             \
    |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress()` instead of `try`-`except`-`pass`
   --> SIM105_0.py:133:1
    |
132 |   # Regression tests for: https://github.com/astral-sh/ruff/issues/18209
133 | / try:
134 | |     1 / 0
135 | | except ():
136 | |     pass
    | |________^
    |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(): ...`
    |
1   + import contextlib
2   | def foo():
--------------------------------------------------------------------------------
133 | # Regression tests for: https://github.com/astral-sh/ruff/issues/18209
    - try:
134 + with contextlib.suppress():
135 |     1 / 0
    - except ():
    -     pass
136 |
    |
note: This is an unsafe fix and may change runtime behavior

SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`pass`
   --> SIM105_0.py:140:1
    |
139 |   BaseException = ValueError
140 | / try:
141 | |     int("a")
142 | | except BaseException:
143 | |     pass
    | |________^
    |
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
    |
1   + import contextlib
2   | def foo():
--------------------------------------------------------------------------------
140 | BaseException = ValueError
    - try:
141 + with contextlib.suppress(BaseException):
142 |     int("a")
    - except BaseException:
    -     pass
    |
note: This is an unsafe fix and may change runtime behavior
