Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

RUF027: Ignore template strings passed to logging calls and builtins._() calls #12889

Merged
merged 1 commit into from
Aug 14, 2024

Conversation

AlexWaygood
Copy link
Member

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:

import logging

foo = 42
logging.error("{foo}")

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 the logging 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 to builtins._() as a gettext call. But running gettext.install() literally monkeypatches the builtins module, so even this fully qualified access should be recognised by us as a gettext call.

Test Plan

I added some new fixtures that Ruff will emit errors on currently, but doesn't with this PR branch.

@AlexWaygood AlexWaygood added rule Implementing or modifying a lint rule preview Related to preview mode features labels Aug 14, 2024
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@AlexWaygood
Copy link
Member Author

Linter (preview)

✅ ecosystem check detected no linter changes.

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 logging call):

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
Copy link
Member

Nice! And thanks for the very detailed summary. It made reviewing a piece of cake

@AlexWaygood AlexWaygood merged commit c487149 into main Aug 14, 2024
20 checks passed
@AlexWaygood AlexWaygood deleted the ruf027-gettext-logging branch August 14, 2024 10:27
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants