Reputation: 477
I have the following function that create a name from the variable size arguments:
def create_name(*argv):
"""Create a name from passed arguments."""
return f"%s" % "_".join(str(arg) for arg in argv)
When running pylint, i am getting the following warning.
W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
Just curious if any improvement can be made to resolve this warning.
Upvotes: 1
Views: 795
Reputation: 31319
Your expression f"%s" % "_".join(str(arg) for arg in argv)
breaks down like this:
f"%s"
is formatted with "_".join(str(arg) for arg in argv)
, using the %
operatorf"%s"
in turn is an interpolated string, but nothing is done to it since there's no {}
in it, so "%s"
would achieve the same"%s"
with another string using %
just gets you the same string again, so the whole expression could just be "_".join(str(arg) for arg in argv)
So, these all work:
return "_".join(str(arg) for arg in argv)
return "%s" % "_".join(str(arg) for arg in argv)
return f'{"_".join(str(arg) for arg in argv)}'
But there's little reason not to use the first, the rest just adds complications and no functionality.
Specifically, you're issued that warning because interpolated strings (or 'f-strings') in Python expect expressions in {}
to replace, but your f-string doesn't have any, so pylint is telling you you can just remove the f
. Because your f-string "does not have any interpolated variables".
Upvotes: 3