I've upgraded to pylint 2.15.2, and suddenly I'm getting lots of consider-using-f-string
warnings whenever I run pylint, where I've used % formatting for strings. I understand why Pylint doesn't want to use the old % formatting, but I also get this error when I try to use string.format() instead. Take the following code as an example:
"""Example module"""
def some_long_complicated_function(a, b):
"""Do something"""
return a + b
def main():
"""Main function"""
a = 2
b = 3
percent_string = "The result of %s + %s is %s" % (
a, b, some_long_complicated_function(a, b)
)
format_string = "The result of {} + {} is {}".format(
a, b, some_long_complicated_function(a, b)
)
f_string = f"The result of {a} + {b} is {some_long_complicated_function(a, b)}"
print(percent_string)
print(format_string)
print(f_string)
if __name__ == "__main__":
main()
When I run pylint on this code, I get the following output:
************* Module pyexample
./pyexample.py:11:21: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)
./pyexample.py:15:20: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)
------------------------------------------------------------------
Your code has been rated at 8.46/10 (previous run: 6.15/10, +2.31)
There are instances like this where I don't want to use an f-string, because I think it actually hampers - not helps - readability, especially in cases like these where I may be writing long function calls inline within the string. In these places I'd rather use string.format(), because you can nicely separate out the format specifiers {}
from the functions to generate the strings I want by putting them on a separate line. With f-strings, my lines may end up being too long and I have to resort to using line continuation characters, which again harms the readability IMO.
The problem is, Pylint doesn't like string.format() - it only wants me to use f-strings. I know that this is a 'Convention' not 'Error', but my code has to pass Pylint 100%. I could waive this message, but that's not good practice and there are places in my code where I do want to swap out the %-string formats.
My question:
Is there a way to configure Pylint so that when I run it, it will not flag a consider-using-f-string
warning when I use string.format() (only when I use % strings)? I've had a look in the rc-file but I can't see any obvious setting like this.
Or is the only way to fix this to waive the warning entirely?