Skip to content

Comments

[flake8-gettext] Fix false negatives for plural argument of ngettext (INT001, INT002, INT003)#21078

Merged
ntBre merged 6 commits intoastral-sh:mainfrom
danparizher:fix-21062
Feb 9, 2026
Merged

[flake8-gettext] Fix false negatives for plural argument of ngettext (INT001, INT002, INT003)#21078
ntBre merged 6 commits intoastral-sh:mainfrom
danparizher:fix-21062

Conversation

@danparizher
Copy link
Contributor

@danparizher danparizher commented Oct 26, 2025

Summary

Fixes #21062. Extends the INT001, INT002, and INT003 lint rules to check both the singular and plural arguments of ngettext function calls, fixing false negatives where formatting issues in the plural argument were not being detected.

Problem Analysis

The three rules (f-string-in-get-text-func-call, format-in-get-text-func-call, and printf-in-get-text-func-call) only checked the first argument (singular) of ngettext calls but ignored the second argument (plural). This caused false negatives when f-strings, .format() calls, or % formatting were used in the plural argument.

The root cause was:

  1. Rule functions only examined args.first()
  2. No logic to determine if a function call was specifically ngettext vs gettext
  3. Missing checks for args.get(1) (the second argument)

Example of the bug:

ngettext(f"one item", f"{n} items", n)  # Only flagged first f-string, not second

Approach

  1. Modified function signatures: Updated all three rule functions to accept the func parameter
  2. Added ngettext detection: Created is_ngettext_call() helper that checks:
    • Direct name references (e.g., ngettext(...))
    • Qualified names ending with ngettext (e.g., gettext_mod.ngettext(...))
  3. Extended argument checking: For ngettext calls, now check both first and second arguments
  4. Code refactoring: Created shared helpers.rs module to eliminate duplication
  5. Updated main checker: Modified expression analyzer to pass func parameter

@github-actions
Copy link
Contributor

github-actions bot commented Oct 26, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you! And sorry for the delay on the review.

I only had a couple of nits about the code, but I think we need to improve the error messages for these new cases. We should also make this a preview change as I mentioned on the issue since this substantially increases the scope of the rule, even if there are no ecosystem changes.

@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Dec 29, 2025
@MichaReiser
Copy link
Member

Is it worth if we try to push this PR over the line, given that the changes seem pretty minimal

@danparizher
Copy link
Contributor Author

I can take a look this weekend

@danparizher danparizher requested a review from ntBre January 26, 2026 01:13
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks! I pushed a couple of commits adding the preview gating and factoring out a couple of helper functions to reuse existing code.

@ntBre ntBre enabled auto-merge (squash) February 9, 2026 18:02
@ntBre ntBre merged commit f6416d0 into astral-sh:main Feb 9, 2026
41 checks passed
@danparizher danparizher deleted the fix-21062 branch February 9, 2026 18:10
carljm added a commit to Hugo-Polloli/ruff that referenced this pull request Feb 9, 2026
* main: (45 commits)
  [ty] Fix wrong inlay hints for overloaded function arguments (astral-sh#23179)
  [ty] Respect `@no_type_check` when combined with other decorators (astral-sh#23177)
  [ty] Use type context when inferring constructor argument types (astral-sh#23139)
  [`airflow`] Add ruff rules to catch deprecated attribute access from context key for Airflow 3.0 (`AIR301`) (astral-sh#22850)
  Support formatting `pycon` markdown code blocks (astral-sh#23112)
  Markdown formatting in LSP (astral-sh#23063)
  Instruct Claude to use comments more sparingly (astral-sh#23181)
  [`flake8-gettext`] Fix false negatives for plural argument of ngettext (`INT001`, `INT002`, `INT003`) (astral-sh#21078)
  [ty] Invoking goto-def on parentheses of a class constructor call takes you too constructor method
  [ty] Make goto definition on class constructor always go to class definition
  [ty] Assign lower completions ranking to deprecated functions and classes (astral-sh#23089)
  [ty] Fix parameter references across files via keyword args (astral-sh#23012)
  [ty] Exclude enclosing class for base completions (astral-sh#23141)
  [`pyupgrade`] Fix syntax error on string with newline escape and comment (`UP037`) (astral-sh#22968)
  [ty] Improve documentation for `expect_single_definition` method (astral-sh#23175)
  [ty] Configure check mode for all projects
  Add `home-assistant` to ecosystem projects (astral-sh#23132)
  Add tabbed shell completion documentation (astral-sh#23169)
  Bump typing conformance-suite pin (astral-sh#23174)
  [ty] Fix invalid diagnostic location for a sub-call to a specialized ParamSpec (astral-sh#23036)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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.

INT false negatives for the plural argument of ngettext

3 participants