【发布时间】:2021-03-05 00:18:19
【问题描述】:
我使用 Python 3 并想用一个自定义类包装 argparse.ArgumentParser
formatter_class=argparse.RawDescriptionHelpFormatter 默认情况下。我可以成功地做到这一点,但是带有 Python 插件 (PyCharm) 的 IntelliJ IDEA 2017.1 会针对以下代码发出警告:
class CustomParser(argparse.ArgumentParser):
def __init__(self, formatter_class=argparse.RawDescriptionHelpFormatter, **kwargs):
# noinspection PyArgumentList
super().__init__(formatter_class=formatter_class, **kwargs) # warning in this line for the last argument if suppression comment above removed
如果使用 IntelliJ 抑制命令删除评论,则 kwargs 上的警告是“期望字典,得到字典”,但它正在工作。这是一个误报警告还是可以在没有警告的情况下做得更好?此警告背后是否存在真正有助于避免的问题?
附带问题:使用formatter_class = kwargs.pop('formatter_class', argparse.RawDescriptionHelpFormatter) 与在签名中显式定义命名参数有区别吗?根据PEP20,在签名中越明确越好,对吧?
【问题讨论】:
-
我现在也看到了另一个警告。如果我想用
CustomParser(formatter_class=argparse.HelpFormatter)更改默认格式化程序,我会收到警告“预期类型'Type[RawDescriptionHelpFormatter]',得到'Type[HelpFormatter]'”,可以用# noinspection PyTypeChecker抑制。这也是误报吗?
标签: python python-3.x intellij-idea super keyword-argument