Skip to content
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

Conversation

robincaloudis
Copy link
Contributor

@robincaloudis robincaloudis commented Feb 25, 2024

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 #10023.

@robincaloudis robincaloudis force-pushed the rc-fix-false-positive-translation-strings branch 4 times, most recently from f75fb21 to 2f991a0 Compare February 25, 2024 12:11
Copy link
Contributor

github-actions bot commented Feb 25, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -4 violations, +0 -0 fixes in 1 projects; 42 projects unchanged)

zulip/zulip (+0 -4 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

- zerver/actions/streams.py:1575:13: RUF027 Possible f-string without an `f` prefix
- zerver/actions/streams.py:1576:13: RUF027 Possible f-string without an `f` prefix
- zerver/actions/streams.py:1577:13: RUF027 Possible f-string without an `f` prefix
- zerver/lib/typed_endpoint.py:366:32: RUF027 Possible f-string without an `f` prefix

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF027 4 0 4 0 0

@charliermarsh charliermarsh self-assigned this Feb 25, 2024
@charliermarsh charliermarsh added the bug Something isn't working label Feb 25, 2024
@charliermarsh
Copy link
Member

Thank you! Will review.

@charliermarsh
Copy link
Member

(The ecosystem checks look good.)

Copy link
Member

@charliermarsh charliermarsh left a 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) {
Copy link
Member

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"))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great additions! Thanks.

@charliermarsh charliermarsh force-pushed the rc-fix-false-positive-translation-strings branch from be36cb0 to f47f2eb Compare February 25, 2024 23:03
@charliermarsh charliermarsh changed the title Fix false-positive in RUF027 [ruff] Avoid f-string false positives in gettext calls (RUF027) Feb 25, 2024
@charliermarsh charliermarsh force-pushed the rc-fix-false-positive-translation-strings branch from f47f2eb to 17693ee Compare February 25, 2024 23:03
@charliermarsh charliermarsh merged commit fc8738f into astral-sh:main Feb 25, 2024
17 checks passed
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
…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 <charlie.r.marsh@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RUF027: false positive for gettext translation strings
2 participants