Skip to content
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

Ruff rule suggestion: warn for useless f-strings #16246

Open
Datamine opened this issue Feb 19, 2025 · 1 comment
Open

Ruff rule suggestion: warn for useless f-strings #16246

Datamine opened this issue Feb 19, 2025 · 1 comment
Labels
rule Implementing or modifying a lint rule type-inference Requires more advanced type inference.

Comments

@Datamine
Copy link

Description

Occasionally I have seen f-strings used like this:

my_variable = "foo bar"
print(f"{my_variable}")

if f"{my_variable}" == "baz":
    return

In these instances, the f-string usage is improper because:

  • The f-string contains only this variable, and nothing else. Using the f-string syntax is superfluous.
  • If the user wanted to use a non-string variable as a string, then explicitly casting it using str(my_variable) would be more Pythonic.

I haven't seen a RUF rule to this effect, though I've seen a few other f-string rules. I think this would be a fine addition.

@MichaReiser
Copy link
Member

Thanks for this rule suggestion.

As you pointed out: Whether the use of my_variable is unnecessary depends on if my_variable is already a string. Ruff's capability to detect this right now are very limited and I worry that the rule would have too many false-negatives (it couldn't flag an f-string like that if it can't determine the type of my_variable.

Related #16103

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule type-inference Requires more advanced type inference. labels Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule type-inference Requires more advanced type inference.
Projects
None yet
Development

No branches or pull requests

2 participants