New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an Error / Exception / Warning when contextlib.suppress() is entered with no specified exception(s) to suppress #90975
Comments
Today if you enter a If it is, I'd love to discuss possibly raising a new NoSupressionError or at least a warning to let people know they executing an unneeded context. Example code that 3.11 does not error on: cooper@home1:~$ python3.11
Python 3.11.0a5+ (main, Feb 21 2022, 08:52:10) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>> with contextlib.suppress():
... print("Foo")
...
Foo This was reported to |
I'm -1 on this suggestion; consider the following:
This seems a reasonable case to support and would require quite some gymnastics to continue supporting while also warning on an empty set. |
Totally agree with your example use case. There you have a chance for it being useful under certain conditions. In that example there is a passed argument. In my example there is no passed argument. Thus, I believe that this will generally always be developer error, again, unless I'm missing something here. My main suggestion here is to just error/warn when no argument at all is passed to Please feel free to edit the title if that's not clear enough etc. |
But |
I concur with Zachary. Note that suppress without arguments corresponds to "except" and isinstance() with empty tuple. |
Ok thanks. Looks like the warning in flake8-bugbear is the right place then, unfortunately. And just to be sure:
Are you saying that cooper@home1:~$ python3.11
Python 3.11.0a5+ (main, Feb 22 2022, 08:51:50) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>> with contextlib.suppress():
... raise ValueError("I raise ...")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: I raise ...
>>> |
>>> try:
... raise ValueError("I raise...")
... except ():
... pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: I raise... |
No, I say that with suppress():
... is equivalent to
or
If you want to suppress all exceptions (it is not very clever idea), use suppress(BaseException). |
I would never want to do that ... I understand it's bad. Just questioning things before adding lints so we put things in the right places and more importantly making sure I understand. Thanks for the discussion all. |
FWIW - Will be looking to add to flake8-bugbear here: PyCQA/flake8-bugbear#222 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: