Proposed check: use suppress context manager
See original GitHub issueInstead of
try:
do_something()
except RuntimeError:
pass
suggest using
with contextlib.suppress(RuntimeError):
do_something()
What do you think?
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
contextlib — Utilities for with-statement contexts ... - Python Docs
Enters a new context manager and adds its __exit__() method to the callback stack. The return value is the result of the context...
Read more >How can I suppress a given exception in a Context Manager?
1 Answer 1 · Using an if condition that returns True if a given exception should be suppressed. This is based on the...
Read more >The Curious Case of Python's Context Manager
Python's context managers are great for resource management and stopping the propagation of leaked abstractions. You've probably used it ...
Read more >Context Managers and Python's with Statement
In this step-by-step tutorial, you'll learn what the Python with statement is and how to use it with existing context managers.
Read more >contextlib - Useful Context Managers (with Statement) in Python
__exit(self, exc_type, exc_value, exc_tb)__ - This method will be executed when the context manager exits or code inside it fails and there is ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I did comment on the subtle bugs it might introduce, but there is another question: is bugbear supposed to be a tool to help detect bugs or to check coding style/conventions ? Is there anything inherently wrong about
except: pass? Doessuppressfix anything ? In the end, usingsuppressis more of a style choice, and perhaps not so relevant for bugbear.Django had a ticket a while back to replace all the
try ... except ...: passblocks withcontextlib.suppress(...): ..., but it was reverted due to performance concerns.Even the people commenting on the issue that introduced it were indecisive about it. I think Ezio put it best: