Skip to content

Conversation

@hamirmahal
Copy link
Contributor

Closes #19302

Summary

This adds an auto-fix for Logging statement uses f-string Ruff 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.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 12, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -0 violations, +138 -0 fixes in 3 projects; 52 projects unchanged)

Snowflake-Labs/snowcli (+0 -0 violations, +4 -0 fixes)

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

- tests_integration/tests_using_container_services/spcs/docker/test_counter/simple_counter.py:37:21: G004 Logging statement uses f-string
+ tests_integration/tests_using_container_services/spcs/docker/test_counter/simple_counter.py:37:21: G004 [*] Logging statement uses f-string
- tests_integration/tests_using_container_services/spcs/docker/test_counter/simple_counter.py:39:17: G004 Logging statement uses f-string
+ tests_integration/tests_using_container_services/spcs/docker/test_counter/simple_counter.py:39:17: G004 [*] Logging statement uses f-string

apache/superset (+0 -0 violations, +120 -0 fixes)

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

- superset/app.py:103:21: G004 Logging statement uses f-string
+ superset/app.py:103:21: G004 [*] Logging statement uses f-string
- superset/cli/examples.py:38:21: G004 Logging statement uses f-string
+ superset/cli/examples.py:38:21: G004 [*] Logging statement uses f-string
- superset/commands/database/uploaders/csv_reader.py:122:17: G004 Logging statement uses f-string
+ superset/commands/database/uploaders/csv_reader.py:122:17: G004 [*] Logging statement uses f-string
- superset/commands/theme/seed.py:104:26: G004 Logging statement uses f-string
+ superset/commands/theme/seed.py:104:26: G004 [*] Logging statement uses f-string
- superset/commands/theme/seed.py:96:26: G004 Logging statement uses f-string
+ superset/commands/theme/seed.py:96:26: G004 [*] Logging statement uses f-string
- superset/examples/bart_lines.py:62:18: G004 Logging statement uses f-string
+ superset/examples/bart_lines.py:62:18: G004 [*] Logging statement uses f-string
- superset/examples/birth_names.py:111:22: G004 Logging statement uses f-string
+ superset/examples/birth_names.py:111:22: G004 [*] Logging statement uses f-string
- superset/examples/multiformat_time_series.py:87:18: G004 Logging statement uses f-string
+ superset/examples/multiformat_time_series.py:87:18: G004 [*] Logging statement uses f-string
- superset/examples/paris.py:58:18: G004 Logging statement uses f-string
+ superset/examples/paris.py:58:18: G004 [*] Logging statement uses f-string
- superset/examples/random_time_series.py:71:18: G004 Logging statement uses f-string
+ superset/examples/random_time_series.py:71:18: G004 [*] Logging statement uses f-string
- superset/examples/sf_population_polygons.py:62:18: G004 Logging statement uses f-string
+ superset/examples/sf_population_polygons.py:62:18: G004 [*] Logging statement uses f-string
- superset/extensions/discovery.py:43:24: G004 Logging statement uses f-string
+ superset/extensions/discovery.py:43:24: G004 [*] Logging statement uses f-string
- superset/extensions/discovery.py:53:21: G004 Logging statement uses f-string
+ superset/extensions/discovery.py:53:21: G004 [*] Logging statement uses f-string
- superset/extensions/discovery.py:62:33: G004 Logging statement uses f-string
+ superset/extensions/discovery.py:62:33: G004 [*] Logging statement uses f-string
- superset/extensions/discovery.py:65:30: G004 Logging statement uses f-string
+ superset/extensions/discovery.py:65:30: G004 [*] Logging statement uses f-string
- superset/extensions/discovery.py:69:22: G004 Logging statement uses f-string
+ superset/extensions/discovery.py:69:22: G004 [*] Logging statement uses f-string
- superset/extensions/local_extensions_watcher.py:103:22: G004 Logging statement uses f-string
+ superset/extensions/local_extensions_watcher.py:103:22: G004 [*] Logging statement uses f-string
- superset/extensions/local_extensions_watcher.py:47:21: G004 Logging statement uses f-string
+ superset/extensions/local_extensions_watcher.py:47:21: G004 [*] Logging statement uses f-string
- superset/extensions/local_extensions_watcher.py:73:28: G004 Logging statement uses f-string
+ superset/extensions/local_extensions_watcher.py:73:28: G004 [*] Logging statement uses f-string
- superset/extensions/local_extensions_watcher.py:78:21: G004 Logging statement uses f-string
+ superset/extensions/local_extensions_watcher.py:78:21: G004 [*] Logging statement uses f-string
- superset/extensions/local_extensions_watcher.py:92:32: G004 Logging statement uses f-string
+ superset/extensions/local_extensions_watcher.py:92:32: G004 [*] Logging statement uses f-string
- superset/migrations/shared/catalogs.py:305:17: G004 Logging statement uses f-string
... 77 additional changes omitted for project

bokeh/bokeh (+0 -0 violations, +14 -0 fixes)

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

- src/bokeh/application/handlers/directory.py:135:25: G004 Logging statement uses f-string
+ src/bokeh/application/handlers/directory.py:135:25: G004 [*] Logging statement uses f-string
- src/bokeh/application/handlers/document_lifecycle.py:66:25: G004 Logging statement uses f-string
+ src/bokeh/application/handlers/document_lifecycle.py:66:25: G004 [*] Logging statement uses f-string
- src/bokeh/io/notebook.py:581:19: G004 Logging statement uses f-string
+ src/bokeh/io/notebook.py:581:19: G004 [*] Logging statement uses f-string
- src/bokeh/io/notebook.py:582:19: G004 Logging statement uses f-string
+ src/bokeh/io/notebook.py:582:19: G004 [*] Logging statement uses f-string
- src/bokeh/io/state.py:176:22: G004 Logging statement uses f-string
+ src/bokeh/io/state.py:176:22: G004 [*] Logging statement uses f-string
... 4 additional changes omitted for project

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
G004 138 0 0 138 0

@MeGaGiGaGon
Copy link
Contributor

How does this handle arbitrary format specs? Reading the code it looks to me like they all get converted to %s, which is definitely not ideal, and should at the very least make the fix unsafe/not offer a fix at all. As an example, what does the fix do on this code?

import logging
logging.info(f"{x:arbitrary data in format spec} {x:{'nested format spec'}}")

@MichaReiser MichaReiser added the fixes Related to suggested fixes for violations label Jul 14, 2025
@ntBre
Copy link
Contributor

ntBre commented Jul 17, 2025

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 %d and %f handling that it looks like you already worked on, but I'd even be happy with only handling {var} -> %s in a first version (omitting format specs entirely).

I can try to give this a review next week if you resolve the conflicts :)

@ntBre ntBre self-requested a review July 17, 2025 16:27
@hamirmahal hamirmahal force-pushed the feat/add-auto-fix-for-f-string-logging-statements-g004 branch from 09e44b3 to a7af730 Compare July 21, 2025 06:39
@hamirmahal
Copy link
Contributor Author

You're welcome! That works for me. I pulled the latest changes and fixed the merge conflicts.

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 had some organizational suggestions and also some edge cases I think we'll need to handle.

@hamirmahal
Copy link
Contributor Author

You're welcome! Weekends are usually when I have more availability; I'll see if I can commit your suggestions now, though.

@hamirmahal hamirmahal force-pushed the feat/add-auto-fix-for-f-string-logging-statements-g004 branch 3 times, most recently from 07990a7 to d51e06c Compare July 27, 2025 00:26
@hamirmahal hamirmahal requested a review from ntBre July 27, 2025 18:28
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, 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!

@hamirmahal hamirmahal force-pushed the feat/add-auto-fix-for-f-string-logging-statements-g004 branch 2 times, most recently from f1c8082 to ddbe52d Compare August 18, 2025 06:45
@hamirmahal hamirmahal requested a review from ntBre August 19, 2025 17:36
@ntBre ntBre added the preview Related to preview mode features label Aug 20, 2025
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 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.

@hamirmahal hamirmahal force-pushed the feat/add-auto-fix-for-f-string-logging-statements-g004 branch from 1475b09 to 019e528 Compare August 24, 2025 20:02
@hamirmahal hamirmahal force-pushed the feat/add-auto-fix-for-f-string-logging-statements-g004 branch from 019e528 to 7729095 Compare August 24, 2025 20:20
@hamirmahal hamirmahal requested a review from ntBre August 24, 2025 22:32
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.

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.

@ntBre ntBre changed the title feat: add auto-fix for f-string logging statements [flake8-logging-format] Add auto-fix for f-string logging calls (G004) Aug 26, 2025
@ntBre ntBre merged commit 136abac into astral-sh:main Aug 26, 2025
35 checks passed
@hamirmahal hamirmahal deleted the feat/add-auto-fix-for-f-string-logging-statements-g004 branch August 27, 2025 03:12
@hamirmahal
Copy link
Contributor Author

You're welcome! Thanks for the suggestions and feedback.

carljm added a commit to leandrobbraga/ruff that referenced this pull request Aug 27, 2025
* 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)
second-ed pushed a commit to second-ed/ruff that referenced this pull request Sep 9, 2025
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Add Autofix for f-string formatting G004 rule

4 participants