gpt4 book ai didi

Python pylint(raise-format-tuple) 异常参数表明字符串格式可能是有意的

转载 作者:行者123 更新时间:2023-12-03 20:25:23 25 4
gpt4 key购买 nike

使用一个简单的自定义异常类定义为:

class MyError(Exception):
pass
这个电话:
foo = 'Some more info'
raise MyError("%s: there was an error", foo)
pylint给出:

Exception arguments suggest string formatting might be intended pylint(raising-format-tuple)


这个消息是什么意思?

最佳答案

根据您的 Python 版本,这些方法中的任何一种都会修复该消息。

foo = 'Some more info'
raise MyError("%s: there was an error" % foo )
raise MyError("{}: there was an error".format(foo))
raise MyError(f"{foo}: there was an error")
pylint时触发该消息看到 %s字符串中没有以下参数的标记。而不是用字符串 "Some more info: there was an error" 引发异常你会得到一个元组的异常,其中第一个元素是 ": there was an error"第二个是 foo的内容.这可能不是预期的效果。
在我使用的代码中,广泛使用了 logging ,我怀疑原作者将异常引发与 lazy logging 混淆了.

关于Python pylint(raise-format-tuple) 异常参数表明字符串格式可能是有意的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62543302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com