RUF027: Ignore template strings passed to logging calls and builtins._()
calls
#12889
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
#12869 initially proposed stabilising RUF027, but the ecosystem report showed that there were too many false positives. Some of those false positives stem from the fact that we currently emit errors on calls like this:
But you're not meant to pass f-strings to logging calls, as the
logging
module lazily evaluates template strings like this to improve performance. The default style of string interpolation used by thelogging
module is%
-style interpolation, but this can be configured to use{}
-style instead: https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles. As such, this PR adjusts the rule's logic to ignore any strings that are inside logging calls.I'm also making another small tweak to the logic used to detect
gettext
calls. Specifically, we weren't recognising a fully qualified call tobuiltins._()
as agettext
call. But runninggettext.install()
literally monkeypatches thebuiltins
module, so even this fully qualified access should be recognised by us as agettext
call.Test Plan
I added some new fixtures that Ruff will emit errors on currently, but doesn't with this PR branch.