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

Detect useless f-strings #9530

Open
socketpair opened this issue Apr 2, 2024 · 7 comments
Open

Detect useless f-strings #9530

socketpair opened this issue Apr 2, 2024 · 7 comments
Labels
Enhancement ✨ Improvement to a component Needs decision 🔒 Needs a decision before implemention or rejection Proposal 📨

Comments

@socketpair
Copy link

Current problem

Real diff:

-                cursor.executescript(f'BEGIN;\n{diff}\nCOMMIT;')
+                cursor.executescript(f'{diff}')

This is an example where Pylint could identify codestyle problem. Useless f-string. i.e. f-strings containing single variable without formatting attributes and without other f-string parts.

Desired solution

Detect such cases and notify

Additional context

No response

@socketpair socketpair added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Apr 2, 2024
@Pierre-Sassoulas Pierre-Sassoulas added Proposal 📨 Needs decision 🔒 Needs a decision before implemention or rejection Enhancement ✨ Improvement to a component and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Apr 2, 2024
@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Apr 2, 2024

+1 from me. It also seems relatively immune to false positive and simple without looking too much into it. How would you name it ? useless-string, useless-fstring-formatting useless-format-string ?

@socketpair
Copy link
Author

socketpair commented Apr 2, 2024

@Pierre-Sassoulas I would call useless-f-string

@DanielNoord
Copy link
Collaborator

class SneakyString(str):
    def __str__(self) -> str:
        return "Different string"


x = SneakyString("A")

print(f"{x}" + "B")
print(f"{x:.2}" + "B")
print(x + "B")

I think there is a actually a pretty high chance for false positives as the logic for f-strings is quite difficult to statically imitate.

@Pierre-Sassoulas
Copy link
Member

Not sure what the false positive is in this case @DanielNoord ? Isn't print(f"{x}" + "B") an actual issue ? As it should be print(str(x) + "B") to be equivalent (well, print(f"{x}B"), but this would become hard). I imagine the message raised should be something like Useless f-string, use 'x' or str(x)' instead of 'f"{x}"'.

@DanielNoord
Copy link
Collaborator

The false positive is that f"{x}" isn't the same as x. f"{x}" is probably quicker than str(x) as there is no lookup for the str() function and thus suggesting str(x) instead of f"{x}" is actually not in line with some of our other checks 😅

@socketpair
Copy link
Author

@DanielNoord

In [1]: timeit f'{42}'
70 ns ± 1.14 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [2]: timeit str(42)
71.9 ns ± 0.856 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

@Pierre-Sassoulas
Copy link
Member

Right. Still + 0.5 on this, but it's less useful if it become consider-f-string-uselessness and a convention check, for sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Needs decision 🔒 Needs a decision before implemention or rejection Proposal 📨
Projects
None yet
Development

No branches or pull requests

3 participants