-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
broad-exception-caught
(W0718) is too strict
#9010
Comments
I'm -1 on this let me explain why. Quoting the doc:
In the case of your example if there's something like this in def func():
distance = request.get("...")
time = request.get("...")
return distance / time Then catching all |
@Pierre-Sassoulas I disagree with your assessment. Even in the example that you gave, what makes you think that The intent is pretty clear here - try to run the function, but fall back to a default value if anything goes wrong. Additionally, it doesn't necessarily "hide" anything. Would you still say that it hides the Also note, that we aren't necessarily talking about whether any particular example with |
I think there is a significant chance that a lot of newbies write this code without knowing about the issues with broad except. That's why I think it is still a valid message. If have seen very little cases where a broad except was really useful in production code and shouldn't have been replaced with specific excepts. If you really "know what you're doing" you can just disable the message, but in general I would just add a local disable for the specific cases where applicable. A linter is opinionated by design: it warns about patterns that it thinks are problematic. If you don't agree with it just disable it? |
Can I disable "except Exception" but enable "except BaseException"? |
Yes! See this configuration option: |
To be explicit, |
On a related note, is the intent of this warning to persuade the developer to never use |
Yes it is. Or opt in by explicitly disabling the linter, which can be sensible in the outermost |
That seems rather draconian and obtrusive when the language obviously supports this, especially when using multiple |
In my personal experience I have never had to put too many ignore in the codebase. This is also the purpose of a linter: warn you about features the language supports but that you should not use. This makes a linter opinionated by definition and therefore not all warnings will be to your liking. |
I get it, but there also comes a point where a linter starts complaining too much. Sometimes you just need to trust that the dev knows what he/she is doing. It's not uncommon to see code that catches a top level exception instead of catching each individual one, which requires a lot more research and coding to accomplish (especially in Python where they don't make it obvious what exceptions may be thrown). |
I think you're describing the issue that Anyway, I don't think |
Bug description
I believe that the following piece of code should not cause a
broad-exception-caught
warning:The documentation for
broad-exception-caught
doesn't actually provide strong justification that catchingException
is always wrong. It links to a stack overflow post, but that post mainly discusses bareexcept:
clauses, notexcept Exception as e:
. Bareexcept:
is ALWAYS wrong, but there are a lot of cases whereexcept Exception
is perfectly valid (even without rethrowing).I believe that the above code is perfectly reasonable and should not produce a warning. The
func
function might raise arbitrary exceptions, so there is no way to rewrite this above code. Additionally, PEP8 (when talking about bareexcept:
clauses) states that broad exception handling is valid if "the exception handler will be printing out or logging the traceback" which is the case here.Configuration
No response
Command used
Pylint output
************* Module example example.py:8:11: W0718: Catching too general exception Exception (broad-exception-caught)
Expected behavior
(no warnings)
Pylint version
OS / Environment
No response
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: