-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[flake8-gettext] Resolve qualified names and built-in bindings (INT001, INT002, INT003)
#19045
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
Merged
ntBre
merged 6 commits into
astral-sh:main
from
robsdedude:fix/19028-flake8-gettext-false-negative
Oct 20, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eec7743
[`flake8_gettext`]: Make `INTxxx` rules also trigger on aliased imports
robsdedude 299c2c3
Simplify code
robsdedude 61987a6
Revert "Simplify code"
robsdedude 88b2fc7
Simplify code
robsdedude 2f91997
Merge branch 'main' into fix/19028-flake8-gettext-false-negative
robsdedude da2ec00
Merge branch 'main' into fix/19028-flake8-gettext-false-negative
robsdedude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
crates/ruff_linter/resources/test/fixtures/flake8_gettext/INT001.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,49 @@ | ||
| _(f"{'value'}") | ||
| gettext(f"{'value'}") | ||
| ngettext(f"{'value'}", f"{'values'}", 2) | ||
|
|
||
| _gettext(f"{'value'}") # no lint | ||
| gettext_fn(f"{'value'}") # no lint | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 - import aliases | ||
| import gettext as gettext_mod | ||
| from gettext import ( | ||
| gettext as gettext_fn, | ||
| ngettext as ngettext_fn, | ||
| ) | ||
|
|
||
| name = "Guido" | ||
|
|
||
| gettext_mod.gettext(f"Hello, {name}!") | ||
| gettext_mod.ngettext(f"Hello, {name}!", f"Hello, {name}s!", 2) | ||
| gettext_fn(f"Hello, {name}!") | ||
| ngettext_fn(f"Hello, {name}!", f"Hello, {name}s!", 2) | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 - deferred translations | ||
| def _(message): return message | ||
|
|
||
| animals = [_(f"{'mollusk'}"), | ||
| _(f"{'albatross'}"), | ||
| _(f"{'rat'}"), | ||
| _(f"{'penguin'}"), | ||
| _(f"{'python'}"),] | ||
|
|
||
| del _ | ||
|
|
||
| for a in animals: | ||
| print(_(a)) | ||
| print(_(f"{a}")) | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 | ||
| # - installed translations/i18n functions dynamically assigned to builtins | ||
| import builtins | ||
|
|
||
| gettext_mod.install("domain") | ||
| builtins.__dict__["gettext"] = gettext_fn | ||
|
|
||
| builtins._(f"{'value'}") | ||
| builtins.gettext(f"{'value'}") | ||
| builtins.ngettext(f"{'value'}", f"{'values'}", 2) |
48 changes: 48 additions & 0 deletions
48
crates/ruff_linter/resources/test/fixtures/flake8_gettext/INT002.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,49 @@ | ||
| _("{}".format("line")) | ||
| gettext("{}".format("line")) | ||
| ngettext("{}".format("line"), "{}".format("lines"), 2) | ||
|
|
||
| _gettext("{}".format("line")) # no lint | ||
| gettext_fn("{}".format("line")) # no lint | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 | ||
| import gettext as gettext_mod | ||
| from gettext import ( | ||
| gettext as gettext_fn, | ||
| ngettext as ngettext_fn, | ||
| ) | ||
|
|
||
| name = "Guido" | ||
|
|
||
| gettext_mod.gettext("Hello, {}!".format(name)) | ||
| gettext_mod.ngettext("Hello, {}!".format(name), "Hello, {}s!".format(name), 2) | ||
| gettext_fn("Hello, {}!".format(name)) | ||
| ngettext_fn("Hello, {}!".format(name), "Hello, {}s!".format(name), 2) | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 - deferred translations | ||
| def _(message): return message | ||
|
|
||
| animals = [_("{}".format("mollusk")), | ||
| _("{}".format("albatross")), | ||
| _("{}".format("rat")), | ||
| _("{}".format("penguin")), | ||
| _("{}".format("python")),] | ||
|
|
||
| del _ | ||
|
|
||
| for a in animals: | ||
| print(_(a)) | ||
| print(_("{}".format(a))) | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 | ||
| # - installed translations/i18n functions dynamically assigned to builtins | ||
| import builtins | ||
|
|
||
| gettext_mod.install("domain") | ||
| builtins.__dict__["gettext"] = gettext_fn | ||
|
|
||
| builtins._("{}".format("line")) | ||
| builtins.gettext("{}".format("line")) | ||
| builtins.ngettext("{}".format("line"), "{}".format("lines"), 2) |
48 changes: 48 additions & 0 deletions
48
crates/ruff_linter/resources/test/fixtures/flake8_gettext/INT003.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,49 @@ | ||
| _("%s" % "line") | ||
| gettext("%s" % "line") | ||
| ngettext("%s" % "line", "%s" % "lines", 2) | ||
|
|
||
| _gettext("%s" % "line") # no lint | ||
| gettext_fn("%s" % "line") # no lint | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 | ||
| import gettext as gettext_mod | ||
| from gettext import ( | ||
| gettext as gettext_fn, | ||
| ngettext as ngettext_fn, | ||
| ) | ||
|
|
||
| name = "Guido" | ||
|
|
||
| gettext_mod.gettext("Hello, %s!" % name) | ||
| gettext_mod.ngettext("Hello, %s!" % name, "Hello, %ss!" % name, 2) | ||
| gettext_fn("Hello, %s!" % name) | ||
| ngettext_fn("Hello, %s!" % name, "Hello, %ss!" % name, 2) | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 - deferred translations | ||
| def _(message): return message | ||
|
|
||
| animals = [_("%s" %"mollusk"), | ||
| _("%s" %"albatross"), | ||
| _("%s" %"rat"), | ||
| _("%s" %"penguin"), | ||
| _("%s" %"python"),] | ||
|
|
||
| del _ | ||
|
|
||
| for a in animals: | ||
| print(_(a)) | ||
| print(_("%s" % a)) | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/19028 | ||
| # - installed translations/i18n functions dynamically assigned to builtins | ||
| import builtins | ||
|
|
||
| gettext_mod.install("domain") | ||
| builtins.__dict__["gettext"] = gettext_fn | ||
|
|
||
| builtins._("%s" % "line") | ||
| builtins.gettext("%s" % "line") | ||
| builtins.ngettext("%s" % "line", "%s" % "lines", 2) |
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
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
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
77 changes: 76 additions & 1 deletion
77
.../ruff_linter__rules__flake8_gettext__tests__f-string-in-get-text-func-call_INT001.py.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,84 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_gettext/mod.rs | ||
| snapshot_kind: text | ||
| --- | ||
| INT001.py:1:3: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 1 | _(f"{'value'}") | ||
| | ^^^^^^^^^^^^ INT001 | ||
| 2 | gettext(f"{'value'}") | ||
| 3 | ngettext(f"{'value'}", f"{'values'}", 2) | ||
| | | ||
|
|
||
| INT001.py:2:9: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 1 | _(f"{'value'}") | ||
| 2 | gettext(f"{'value'}") | ||
| | ^^^^^^^^^^^^ INT001 | ||
| 3 | ngettext(f"{'value'}", f"{'values'}", 2) | ||
| | | ||
|
|
||
| INT001.py:3:10: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 1 | _(f"{'value'}") | ||
| 2 | gettext(f"{'value'}") | ||
| 3 | ngettext(f"{'value'}", f"{'values'}", 2) | ||
| | ^^^^^^^^^^^^ INT001 | ||
| 4 | | ||
| 5 | _gettext(f"{'value'}") # no lint | ||
| | | ||
|
|
||
| INT001.py:27:14: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 25 | def _(message): return message | ||
| 26 | | ||
| 27 | animals = [_(f"{'mollusk'}"), | ||
| | ^^^^^^^^^^^^^^ INT001 | ||
| 28 | _(f"{'albatross'}"), | ||
| 29 | _(f"{'rat'}"), | ||
| | | ||
|
|
||
| INT001.py:28:14: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 27 | animals = [_(f"{'mollusk'}"), | ||
| 28 | _(f"{'albatross'}"), | ||
| | ^^^^^^^^^^^^^^^^ INT001 | ||
| 29 | _(f"{'rat'}"), | ||
| 30 | _(f"{'penguin'}"), | ||
| | | ||
|
|
||
| INT001.py:29:14: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 27 | animals = [_(f"{'mollusk'}"), | ||
| 28 | _(f"{'albatross'}"), | ||
| 29 | _(f"{'rat'}"), | ||
| | ^^^^^^^^^^ INT001 | ||
| 30 | _(f"{'penguin'}"), | ||
| 31 | _(f"{'python'}"),] | ||
| | | ||
|
|
||
| INT001.py:30:14: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 28 | _(f"{'albatross'}"), | ||
| 29 | _(f"{'rat'}"), | ||
| 30 | _(f"{'penguin'}"), | ||
| | ^^^^^^^^^^^^^^ INT001 | ||
| 31 | _(f"{'python'}"),] | ||
| | | ||
|
|
||
| INT001.py:31:14: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 29 | _(f"{'rat'}"), | ||
| 30 | _(f"{'penguin'}"), | ||
| 31 | _(f"{'python'}"),] | ||
| | ^^^^^^^^^^^^^ INT001 | ||
| 32 | | ||
| 33 | del _ | ||
| | | ||
|
|
||
| INT001.py:37:13: INT001 f-string is resolved before function call; consider `_("string %s") % arg` | ||
| | | ||
| 35 | for a in animals: | ||
| 36 | print(_(a)) | ||
| 37 | print(_(f"{a}")) | ||
| | ^^^^^^ INT001 | ||
| | |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.