RUF027: Ignore template strings passed to logging calls and builtins._() calls#12889
Merged
AlexWaygood merged 1 commit intomainfrom Aug 14, 2024
Merged
RUF027: Ignore template strings passed to logging calls and builtins._() calls#12889AlexWaygood merged 1 commit intomainfrom
builtins._() calls#12889AlexWaygood merged 1 commit intomainfrom
Conversation
Contributor
|
Member
Author
I suspected that this might be the case. The false positives highlighted in the ecosystem check in #12869 looked like this, which we'll still currently emit a false positive error on, even with this PR (because the string is assigned to a variable before being passed to a import logging
foo = 42
logging_string = "{foo}"
logging.error(logging_string)I have ideas for how to get rid of this false positive, but it's more involved, so I'd prefer to do it as a followup to this PR. |
MichaReiser
approved these changes
Aug 14, 2024
Member
|
Nice! And thanks for the very detailed summary. It made reviewing a piece of cake |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
loggingmodule lazily evaluates template strings like this to improve performance. The default style of string interpolation used by theloggingmodule 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
gettextcalls. Specifically, we weren't recognising a fully qualified call tobuiltins._()as agettextcall. But runninggettext.install()literally monkeypatches thebuiltinsmodule, so even this fully qualified access should be recognised by us as agettextcall.Test Plan
I added some new fixtures that Ruff will emit errors on currently, but doesn't with this PR branch.