> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Use `contextlib.suppress`

#### Sourcery refactoring id: `use-contextlib-suppress`

#### Description

Use [`contextlib`](https://docs.python.org/3/library/contextlib.html)'s
`suppress` method to silence a specific error, instead of `pass`ing in an
exception handler. This refactoring will add an import for `contextlib` if
needed.

#### Before

```python
try:
    travel_world(days=80)
except DistractionError:
    pass
```

#### After

```python

with contextlib.suppress(DistractionError):
    travel_world(days=80)
```

#### Explanation

The context manager slightly shortens the code and significantly clarifies the
author's intention to ignore the specific errors. The standard library feature
was introduced following a [discussion](https://bugs.python.org/issue15806),
where the consensus was that

> A key benefit here is in the priming effect for readers... The with statement
> form makes it clear before you start reading the code that certain exceptions
> won't propagate.

#### Related Rules

- [merge-except-handler](/reference/refactorings/python/merge-except-handler/) to merge except handlers with
  the same content
- [remove-redundant-exception](/reference/refactorings/python/remove-redundant-exception/) to clean up the
  tuple of an except clause
- [remove-redundant-except-handler](/reference/refactorings/python/remove-redundant-except-handler/) to
  remove unreachable `except` blocks
- [do-not-use-bare-except](/reference/refactorings/python/do-not-use-bare-except/) to always specify which
  errors an `except` block is handling
