-
Notifications
You must be signed in to change notification settings - Fork 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
[ruff
] Avoid f-string false positives in gettext
calls (RUF027
)
#10118
[ruff
] Avoid f-string false positives in gettext
calls (RUF027
)
#10118
Conversation
f75fb21
to
2f991a0
Compare
2f991a0
to
f3156ad
Compare
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RUF027 | 4 | 0 | 4 | 0 | 0 |
Thank you! Will review. |
(The ecosystem checks look good.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@@ -70,13 +70,38 @@ pub(crate) fn missing_fstring_syntax( | |||
} | |||
} | |||
|
|||
// We also want to avoid expressions that are intended to be translated. | |||
if semantic.current_expressions().any(is_gettext) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to generalize this to look at all parent expressions, to catch cases like: print(_("formatting of {partial} in a translation string is bad practice"))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great additions! Thanks.
be36cb0
to
f47f2eb
Compare
ruff
] Avoid f-string false positives in gettext
calls (RUF027
)
f47f2eb
to
17693ee
Compare
…astral-sh#10118) ## Summary It is a convention to use the `_()` alias for `gettext()`. We want to avoid statement expressions and assignments related to aliases of the gettext API. See https://docs.python.org/3/library/gettext.html for details. When one uses `_() to mark a string for translation, the tools look for these markers and replace the original string with its translated counterpart. If the string contains variable placeholders or formatting, it can complicate the translation process, lead to errors or incorrect translations. ## Test Plan * Test file `RUF027_1.py` was extended such that the test reproduces the false-positive Closes astral-sh#10023. --------- Co-authored-by: Charlie Marsh <[email protected]>
Summary
It is a convention to use the
_()
alias forgettext()
. We want to avoidstatement expressions and assignments related to aliases of the gettext API.
See https://docs.python.org/3/library/gettext.html for details. When one
uses `_() to mark a string for translation, the tools look for these markers
and replace the original string with its translated counterpart. If the
string contains variable placeholders or formatting, it can complicate the
translation process, lead to errors or incorrect translations.
Test Plan
RUF027_1.py
was extended such that the test reproduces the false-positiveCloses #10023.