question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposed check: use suppress context manager

See original GitHub issue

Instead of

try:
    do_something()
except RuntimeError:
    pass

suggest using

with contextlib.suppress(RuntimeError):
    do_something()

What do you think?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
joaoecommented, Jan 1, 2020

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 ? Does suppress fix anything ? In the end, using suppress is more of a style choice, and perhaps not so relevant for bugbear.

1reaction
orlnub123commented, Jan 1, 2020

Django had a ticket a while back to replace all the try ... except ...: pass blocks with contextlib.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:

This saves two lines, but makes the code less explicit, it can’t be used for try/except/else or try/except/except, it requires an extra import, the implementation is simple enough that it doesn’t necessary need to be in the stdlib, it might encourage bad style, and it’s slower.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found