pylint
pylint copied to clipboard
Detect useless f-strings
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
+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
?
@Pierre-Sassoulas I would call useless-f-string
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.
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}"'.
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 :sweat_smile:
@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)
Right. Still + 0.5 on this, but it's less useful if it become consider-f-string-uselessness
and a convention check, for sure.