[flake8-logging] Allow closures in except handlers for LOG004#24464
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| LOG004 | 1 | 0 | 1 | 0 | 0 |
|
This would now lead to the following false negative: import logging
def make_later():
try:
raise ValueError()
except Exception:
def later():
logging.exception("x") # should be LOG004
logging.error("x", exc_info=True) # should be LOG014
return later
make_later()()
The range check only applies to where the function was defined, not where it's called, which we can't determine / track in Ruff. So if we want to fix this false positive, we'll have to introduce some false negatives. I suppose it might be worth it though. |
Thank you so much for the detailed review, @charliermarsh I've updated the PR description to reflect the limitation around definition site vs call site. Happy to add your example as a documented known false negative in the test fixture. Would you recommend going with this tradeoff, or would you prefer a different approach? Thank you |
|
I think we should document this limitation, but otherwise I'm comfortable with it. I'll add a note to the docs before merging. |
flake8-logging] Allow closures in except handlers for LOG004
Summary
Fixes #18646.
outside_handlerswalks AST ancestors looking for aTrystatement whose handler range contains the call offset. It used to bail out atFunctionDefnodes to avoid crossing scope boundaries, which meant anylogging.exception()inside a closure defined in an except block got flagged.The range check applies to where the function is defined, not where it's called (which Ruff can't track). This means functions defined inside an except block but called outside of it won't be flagged. This tradeoff fixes the more common false positive at the cost of a less common false negative.
Dropped the
FunctionDefbreak.Test Plan