简体   繁体   English

Pylint 不喜欢 string.format() 并希望我使用 f-strings。 这是可以修复的吗?

[英]Pylint doesn't like string.format() and wants me to use f-strings. Is this fixable?

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.我已经升级到 pylint 2.15.2,每当我运行 pylint 时突然收到很多consider-using-f-string警告,其中我对字符串使用了 % 格式。 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.我理解为什么 Pylint 不想使用旧的 % 格式,但是当我尝试改用 string.format() 时,我也遇到了这个错误。 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:当我在此代码上运行 pylint 时,我得到以下 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.在某些情况下,我不想使用 f 字符串,因为我认为它实际上会妨碍 - 而不是帮助 - 可读性,尤其是在这样的情况下,我可能会在字符串中内联编写长 function 调用。 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.在这些地方,我宁愿使用 string.format(),因为您可以很好地将格式说明符{}从函数中分离出来,通过将它们放在单独的一行来生成我想要的字符串。 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.使用 f-strings,我的行可能会变得太长,我不得不求助于使用行连续字符,这再次损害了 IMO 的可读性。

The problem is, Pylint doesn't like string.format() - it only wants me to use f-strings.问题是,Pylint 不喜欢 string.format() - 它只希望我使用 f-strings。 I know that this is a 'Convention' not 'Error', but my code has to pass Pylint 100%.我知道这是一个“约定”而不是“错误”,但我的代码必须 100% 通过 Pylint。 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.我可以放弃此消息,但这不是好的做法,并且在我的代码中的某些地方我确实想要换出 %-string 格式。

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)?有没有一种方法可以配置 Pylint,这样当我运行它时,它不会在我使用 string.format() 时(仅当我使用 % strings 时)发出consider-using-f-string警告? I've had a look in the rc-file but I can't see any obvious setting like this.我查看了 rc 文件,但看不到任何明显的设置。

Or is the only way to fix this to waive the warning entirely?还是解决此问题以完全放弃警告的唯一方法?

If you just want to avoid long line or line continuation character, I usually choose to use parentheses:如果你只是想避免长行或行继续字符,我通常选择使用括号:

f_string = (f"The result of {a} + {b} is "
            f"{some_long_complicated_function(a, b)}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM