dot-format-in-exception (EM103)
源自 flake8-errmsg linter。
有时提供修复。
作用
检查在异常构造函数中对字符串字面量使用.format
调用。
为什么这不好?
Python 在默认的回溯中包含 raise
(并且像 Rich 和 IPython 这样的格式化程序也这样做)。
通过使用.format
调用,错误消息将在回溯中重复,这会降低回溯的可读性。
示例
给定
Python 将产生像这样的回溯
Traceback (most recent call last):
File "tmp.py", line 2, in <module>
raise RuntimeError("'{}' is incorrect".format(sub))
RuntimeError: 'Some value' is incorrect
相反,将字符串赋值给变量
这将产生像这样的回溯