-
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
TRY400 flags log.error() + raise/sys.exit() #4136
Comments
Is there a reason why you couldn't pass |
Mostly because I didn't know of it :-) but also because I wanted a different log level than EXCEPTION. |
If you're using Python 3.11, then you should use the new
Is this a custom defined log level as it doesn't exists in the Correct me if I'm wrong as I might not be aware of the context, but I think you could get away with just using def foo():
def bar():
try:
raise Exception("exception message")
except:
logging.exception("additional message")
bar()
foo()
# ERROR:root:additional message
# Traceback (most recent call last):
# File "<ipython-input-7-78c777a5f655>", line 4, in bar
# raise Exception("exception message")
# Exception: exception message |
Wow, thanks, I didn't know that. Always though that exception is it's own level. re add_notes: Yes, I think that would be nice, but I haven't checked how this is then displayed in the logs. It's really nice that one has a extra log line with just the extra info instead of the "log lines explosion" of the stacktrace... Anyway: I still think this pattern should be not flagged: it's perfectly good code to use e.g. |
If you disable the sending of information about the exception with import logging
logger = logging.getLogger('stream.logger')
def foo():
try:
raise ValueError('error message')
except Exception as e:
logger.error('Found handled error: %s', e)
logger.error('Found handled error: %s', e, exc_info=False)
logger.exception('Found handled error: %s', e, exc_info=False)
logger.exception('Found handled error: %s', str(e), exc_info=False)
I agree that it is often necessary to log only text without traceback using the |
But in this case the log will also be traceback, and I do not need it |
Hm, I guess if |
Similarly, "Redundant exception object included in call" should be enforced when we log |
I believe this should be true for all other log levels as the
Reference: https://docs.python.org/3/library/logging.html?highlight=logging#logging.Logger.debug |
For what it's worth, I agree In part, I may have qualms with |
Another bad case: when the log is followed by a # In an optional script
try:
import ....
except ImportError:
msg = "Dependencies are missing: run `poetry install --with <dependency group> --sync`"
logger.error(msg) # noqa: TRY400
sys.exit(1) I specifically do NOT want the whole stacktrace here because it makes figuring out what to do harder than just seeing the specific instruction here. |
I just want to say that I wholeheartedly agree with @zanieb's stance on this. |
In the following code snippet, a TRY400 violation is raised because I use log.error() and not log.exception() in an exception handler. In this case it's by design as I immediately re-raise the exception, I just want to add some message to the exception and I don't want the stacktrace shown twice.
In my opinion the pattern "in except block, with a log.something + 'raise' in all cases` should be something TRY400 should ignore/be ok with.
The text was updated successfully, but these errors were encountered: