diff --git a/docs/checks.md b/docs/checks.md index 76c17ff..44057f5 100644 --- a/docs/checks.md +++ b/docs/checks.md @@ -142,8 +142,8 @@ spaces_4 = "\thello world".expandtabs(4) Categories: `contextlib` `readability` -Often times you want to handle an exception, and just ignore it. You can do -this with a `try/except` block, using a single `pass` in the `except` +Often times you want to handle an exception and just ignore it. You can do +this with a `try`/`except` block with a single `pass` in the `except` block, but there is a simpler and more concise way using the `suppress()` function from `contextlib`: @@ -164,6 +164,9 @@ with suppress(FileNotFoundError): f() ``` +Note: `suppress()` is slower than using `try`/`except`, so for performance +critical code you might consider ignoring this check. + ## FURB108: `use-in-oper` Categories: `logical` `readability` diff --git a/refurb/checks/contextlib/with_suppress.py b/refurb/checks/contextlib/with_suppress.py index ade2e21..e27caf5 100644 --- a/refurb/checks/contextlib/with_suppress.py +++ b/refurb/checks/contextlib/with_suppress.py @@ -8,8 +8,8 @@ @dataclass class ErrorInfo(Error): """ - Often times you want to handle an exception, and just ignore it. You can do - this with a `try/except` block, using a single `pass` in the `except` + Often times you want to handle an exception and just ignore it. You can do + this with a `try`/`except` block with a single `pass` in the `except` block, but there is a simpler and more concise way using the `suppress()` function from `contextlib`: @@ -29,6 +29,9 @@ class ErrorInfo(Error): with suppress(FileNotFoundError): f() ``` + + Note: `suppress()` is slower than using `try`/`except`, so for performance + critical code you might consider ignoring this check. """ name = "use-with-suppress"