Skip to content

[Pyflakes] F504 fix changes behavior if args have side effects #18806

Description

@MeGaGiGaGon

Summary

The percent-format-extra-named-arguments (F504) fix can change behavior if the extra args have side effects. It should probably be an always unsafe fix, or only safe if the args being removed are only literals.
playground

PS ~\Desktop\New_folder\ruff>Get-Content issue.py
"Hello, %(name)s" % {"greeting": print(1), "name": "World"}
PS ~\Desktop\New_folder\ruff>py issue.py
1
PS ~\Desktop\New_folder\ruff>uvx ruff check issue.py --isolated --select F
issue.py:1:1: F504 [*] `%`-format string has unused named argument(s): greeting
  |
1 | "Hello, %(name)s" % {"greeting": print(1), "name": "World"}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ F504
  |
  = help: Remove extra named arguments: greeting

Found 1 error.
[*] 1 fixable with the `--fix` option.
PS ~\Desktop\New_folder\ruff>uvx ruff check issue.py --isolated --select F --fix
Found 1 error (1 fixed, 0 remaining).
PS ~\Desktop\New_folder\ruff>Get-Content issue.py
"Hello, %(name)s" % {"name": "World"}
PS ~\Desktop\New_folder\ruff>py issue.py

This also applies to string-dot-format-extra-named-arguments (F522) playground

This also applies to string-dot-format-extra-positional-arguments (F523) playground

Version

ruff 0.12.0 (87f0feb 2025-06-17) + playground

Activity

  1. added
    bugAn issue describing something that isn't working, or a PR that fixes a bug
    fixesRelated to suggested fixes for violations
    on Jun 20, 2025
  2. MichaReiser commented on Jun 20, 2025

    @MichaReiser
    Member

    I think we can limit it to only be unsafe if the argument is a call expression (or contains one). Yes, there are a few obscure cases where e.g. a __str__ call (or __format__?) has side effects, but we shouldn't optimize for edge cases here.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn issue describing something that isn't working, or a PR that fixes a bugfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions