【问题标题】:W504 line break after binary operator二元运算符后的 W504 换行符
【发布时间】:2021-08-28 16:43:10
【问题描述】:

我的情况如下:

ok = (not a > 10 and
      not b < 10 and
      not c > 99 and
      d == 99)

flake8 用错误消息抱怨这一行:

W504 二元运算符后的换行符

当我移动操作符时,它会抛出一个不同的错误:

ok = (not a > 10
      and not b < 10
      and not c > 99
      and d == 99)

W503 二元运算符前的换行符

我尝试了多个建议(例如,this),但 flake8 仍然抱怨换行符。我的代码中的实际情况很长,所以我不能把它放在一行中,而且我的团队更喜欢在() 中包含长行而不是使用\。

【问题讨论】:

  • 将and 移到下一行?
  • 是的,试过了,但是会抛出W503 line break before binary operator。为了清楚起见,更新我的问题。
  • 您需要在 flake8 配置中禁用 W504 或 W503,如果您不使用 `/`,则不可能同时使用两者
  • Black also introduces such line brakes,我猜没有禁用任何错误。所以,我想知道也许有一种方法可以在不禁用任何警告的情况下通过 flake8。
  • 2 cmets 与您提出的问题没有直接关系,但可能会有所帮助 - 首先,在检查多个条件是否为真时,您可能需要使用 all 例如:ok = all([not a &gt; 10, not b &lt; 10 ...]):。其次,根据De Morgan's Law,not conditional and not conditional 与not (conditional or conditional) 相同。不检查它,我怀疑 flake8 的问题是 2 个二元运算符。为什么不试试any:ok = not any([a &gt; 10, b &lt; 10, ...])

标签: python pep8 flake8


【解决方案1】:
ok = (
    not a > 10 and
    not b < 10 and
    not c > 99 and
    d == 99
)

这应该可以解决问题。

【讨论】:

    【解决方案2】:

    你已经在你的配置中设置了ignore =——你应该使用extend-ignore =

    W504 和 W503 相互冲突(并且默认情况下都被禁用)——通过设置 ignore 你已经重新启用它们。 extend-ignore 没有这个问题,因为它增加了默认的忽略代码集


    免责声明:我是当前的 flake8 维护者

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      相关资源
      最近更新 更多