Skip to content

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

@MeGaGiGaGon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated 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