Skip to content

[flake8-logging-format] Avoid behavior changes and syntax errors in fixes (G004) - #27419

Merged
ntBre merged 3 commits into
astral-sh:mainfrom
hirehamir:fix-g004-fix-semantics
Nov 3, 2026
Merged

ntBre merged 3 commits into
astral-sh:mainfrom
hirehamir:fix-g004-fix-semantics

Conversation

@hirehamir

Copy link
Copy Markdown
Contributor

Summary

Fixes #20151

I authored the original fix in #19303. This improves that implementation.

Test Plan

I added a new fixture that covers multiple cases from the issue.

crates/ruff_linter/resources/test/fixtures/flake8_logging_format/G004_fix_semantics.py

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 2, 2026 17:48
@astral-sh-bot

astral-sh-bot Bot commented Aug 2, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! This looks reasonable to me overall. I had one request about the tests, and then Codex found a couple of additional edge cases that we should also try to handle:

  • Keyword arguments can produce invalid code

     import logging
     
     value = "sample"
     logging.warning(msg=(f"message: {value}"))

    this produces

     logging.warning(msg="message: %s", value)

    with the keyword before the positional argument. I think the result just needs to be parenthesized, like we currently do on main: https://play.ruff.rs/4d6a3c7a-bb6b-4943-b73a-903beef076f7.

  • Comments can now be removed in a safe fix

     import logging
     
     value = "sample"
     
     logging.warning(
         (  # Important explanation
             f"message: {value}"
             # Another important explanation
         )
     )

    This is okay overall, we just need to mark the fix unsafe in this case. We can check something like this:

    let applicability = if checker.comment_ranges().intersects(expr.range()) {
    Applicability::Unsafe
    } else {
    Applicability::Safe
    };

    This is one downside of using the generator, though. On main we preserve all the comments: https://play.ruff.rs/833dbeba-42c6-4782-ab5f-e1dedd15e7b8

@ntBre ntBre added bug An issue describing something that isn't working, or a PR that fixes a bug fixes Related to suggested fixes for violations preview Related to preview-mode features labels Aug 6, 2026
@ntBre ntBre changed the title fix: G004 semantics [flake8-logging-format] Avoid behavior changes and syntax errors in fixes (G004) Aug 6, 2026
@hirehamir
hirehamir force-pushed the fix-g004-fix-semantics branch from f1f4917 to 25ba8bc Compare August 7, 2026 17:59
@hirehamir
hirehamir requested a review from ntBre August 7, 2026 17:59

@hirehamir hirehamir left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're welcome! I think adding the parentheses would result in different runtime behavior, but I did implement the other suggestions.

@hirehamir

Copy link
Copy Markdown
Contributor Author

@ntBre are there any other changes I should make here?

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me, thank you! I just trimmed some of the comments a bit.

@ntBre
ntBre merged commit 0c9a7b6 into astral-sh:main Nov 3, 2026
54 checks passed
@hirehamir
hirehamir deleted the fix-g004-fix-semantics branch November 3, 2026 23:55
@hirehamir

Copy link
Copy Markdown
Contributor Author

You're welcome! I'm happy to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug fixes Related to suggested fixes for violations preview Related to preview-mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

G004 fix can change behavior and introduce syntax errors

2 participants