Skip to content

[Pyflakes] F541 fix changes behavior if the string becomes a docstring #18807

Closed

Description

@MeGaGiGaGon

Summary

If the string f-string-missing-placeholders (F541) is targeting is in a position where it would become a docstring, it can change behavior. It should probably be marked unsafe or not apply in this case.
playground

PS ~\Desktop\New_folder\ruff>Get-Content issue.py
f"Hello, world!"
print(__doc__)
PS ~\Desktop\New_folder\ruff>py issue.py
None
PS ~\Desktop\New_folder\ruff>uvx ruff check issue.py --isolated --select F
issue.py:1:1: F541 [*] f-string without any placeholders
  |
1 | f"Hello, world!"
  | ^^^^^^^^^^^^^^^^ F541
2 | print(__doc__)
  |
  = help: Remove extraneous `f` prefix

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, world!"
print(__doc__)
PS ~\Desktop\New_folder\ruff>py issue.py
Hello, world!

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

    Thanks. I think we shouldn't provide a fix in this (rare) case

  3. added a commit that references this issue on Apr 9, 2026
    ab55213
  4. baltasarblanco commented on Aug 26, 2026

    @baltasarblanco
    Contributor

    Hi, how's it going @MichaReiser? I saw that a while ago you said we shouldn't offer a fix here. I'd like to ask for your opinion: I've been running some tests and noticed the scope is wider than what was originally reported; it's not just the module case — I've verified that function, class and implicit concatenation are also affected (""f"" becomes "" ""). I wanted to know whether you'd still prefer the earlier approach (not offering a fix) or degrading it to unsafe? I've already identified where a check could go. Looking forward to your reply, thanks!

  5. MichaReiser commented on Aug 26, 2026

    @MichaReiser
    Member

    Either unsafe or not provide a fix seems fine to me. Yes, this applies to f-strings in all docstring positions, this also includes attribute docstrings

  6. baltasarblanco commented on Aug 26, 2026

    @baltasarblanco
    Contributor

    Perfect, thank you! One more question: would you recommend covering only the reachable positions, avoiding touching the checker? Or would you accept a change to cover all of them? With that, I could cover module and attribute. Thanks again!

  7. added a commit that references this issue on Oct 2, 2026
    3b1c928
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