如何在Python中的多行代码上执行类型忽略?

问题描述 投票:0回答:1

我最近开始使用 pylint 来捕获代码中的错误和不良样式。我发现有些事情我需要让 pylint 忽略,因为它在不应该的时候抱怨。我知道

# type: ignore
可以做到这一点,但我在使用一行不适合单行的代码时遇到问题。

我正在尝试对多行代码部分进行类型忽略。它看起来像:

if thing == this.that.something_else['name'] \ 
    and this.that.something_else['age'] == 0

我想做:

if thing == this.that.something_else['name'] \ # type: ignore
    and this.that.something_else['age'] == 0 # type: ignore

但是该行的前半部分以

\
结尾,因此第一种类型的忽略不起作用。我该如何编写才能使其忽略这两部分?

formatting pylint
1个回答
0
投票

你真的在使用 pylint 吗?如果是这种情况,那么

# type: ignore
并不是忽略 pylint 中误报的方法,而是
# pylint: disable=my-message-name
。您可以像这样在多行上禁用:

# pylint: disable=my-message-name
if thing == this.that.something_else['name'] \ 
    and this.that.something_else['age'] == 0
# pylint: enable=my-message-name

或者也许

# pylint: disable-next=my-message-name
if thing == this.that.something_else['name'] and this.that.something_else['age'] == 0

更多详细信息请参阅消息控制文档

© www.soinside.com 2019 - 2024. All rights reserved.