-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[flake8-logging-format] Add auto-fix for f-string logging calls (G004)
#19303
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
[flake8-logging-format] Add auto-fix for f-string logging calls (G004)
#19303
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| G004 | 138 | 0 | 0 | 138 | 0 |
|
How does this handle arbitrary format specs? Reading the code it looks to me like they all get converted to import logging
logging.info(f"{x:arbitrary data in format spec} {x:{'nested format spec'}}") |
|
Thanks for working on this! I think @MeGaGiGaGon raises some good points. I think it would make sense to bail out and not offer a fix if we see any complicated format specs, especially for a first iteration. We could keep the I can try to give this a review next week if you resolve the conflicts :) |
09e44b3 to
a7af730
Compare
|
You're welcome! That works for me. I pulled the latest changes and fixed the merge conflicts. |
ntBre
left a comment
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! I had some organizational suggestions and also some edge cases I think we'll need to handle.
crates/ruff_linter/src/rules/flake8_logging_format/violations.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
|
You're welcome! Weekends are usually when I have more availability; I'll see if I can commit your suggestions now, though. |
07990a7 to
d51e06c
Compare
ntBre
left a comment
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, this is still looking good! Just a few more minor structural suggestions and another test idea. We still need to preview-gate this too. Let me know if you need help with that.
Apologies for the long delay in re-reviewing!
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
...ake8_logging_format/snapshots/ruff_linter__rules__flake8_logging_format__tests__G004.py.snap
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
f1c8082 to
ddbe52d
Compare
ntBre
left a comment
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.
Thank you again, this is very nearly ready. I mostly had very small performance-related nits and a suggestion on test placement. There's also one last edge case I came up with, but I think we can mostly delete code to solve it, which is always nice.
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_logging_format/rules/logging_call.rs
Show resolved
Hide resolved
...ng_format/snapshots/ruff_linter__rules__flake8_logging_format__tests__G004_arg_order.py.snap
Show resolved
Hide resolved
1475b09 to
019e528
Compare
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
019e528 to
7729095
Compare
ntBre
left a comment
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.
Excellent, thanks again!
I went through the ecosystem changes, and they look good too. We don't show the suggested fix, but they all look like simple cases that I'd expect the fix to handle.
flake8-logging-format] Add auto-fix for f-string logging calls (G004)
|
You're welcome! Thanks for the suggestions and feedback. |
* main: [`ruff`] Preserve relative whitespace in multi-line expressions (`RUF033`) (astral-sh#19647) [ty] Optimize TDD atom ordering (astral-sh#20098) [`airflow`] Extend `AIR311` and `AIR312` rules (astral-sh#20082) [ty] Preserve qualifiers when accessing attributes on unions/intersections (astral-sh#20114) [ty] Fix the inferred interface of specialized generic protocols (astral-sh#19866) [ty] Infer slightly more precise types for comprehensions (astral-sh#20111) [ty] Add more tests for protocols (astral-sh#20095) [ty] don't eagerly unpack aliases in user-authored unions (astral-sh#20055) [`flake8-use-pathlib`] Update links to the table showing the correspondence between `os` and `pathlib` (astral-sh#20103) [`flake8-use-pathlib`] Make `PTH100` fix unsafe because it can change behavior (astral-sh#20100) [`flake8-use-pathlib`] Delete unused `Rule::OsSymlink` enabled check (astral-sh#20099) [ty] Add search paths info to unresolved import diagnostics (astral-sh#20040) [`flake8-logging-format`] Add auto-fix for f-string logging calls (`G004`) (astral-sh#19303) Add a `ScopeKind` for the `__class__` cell (astral-sh#20048) Fix incorrect D413 links in docstrings convention FAQ (astral-sh#20089) [ty] Refactor inlay hints structure to use separate parts (astral-sh#20052)
…004`) (astral-sh#19303) Closes astral-sh#19302 <!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary This adds an auto-fix for `Logging statement uses f-string` Ruff G004, so users don't have to resolve it manually. <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan I ran the auto-fixes on a Python file locally and and it worked as expected. <!-- How was it tested? --> --------- Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Closes #19302
Summary
This adds an auto-fix for
Logging statement uses f-stringRuff G004, so users don't have to resolve it manually.Test Plan
I ran the auto-fixes on a Python file locally and and it worked as expected.