Skip to content

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Oct 28, 2022

PR types

Others

PR changes

Others

Describe

解决 Flake8 E712 问题(comparison to True should be ‘if cond is True:’ or ‘if cond:’)

  • x == True -> x / x is True
  • x != True -> not x / x is not True
  • x == False -> not x / x is False
  • x != False -> not x / x is not False

x == False 为例子,分情况考虑如何修改

  • 如果无需考虑是否严格与 False 相等,只是用于逻辑比较 -> 直接 not x(大多数情况)
  • 如果需要严格为 False,而 []None"" 等 falsy value 则需要用 is -> x is False
  • 如果比较的对象重载了 __eq__,则需要额外考虑修改方案,比如 x 为 bool 类型的 np.ndarray,修改为 ~x 即可

具体所使用工具及命令如下:

# ast-grep, docs: https://ast-grep.github.io/
# clone ast-grep to local
git clone https://github.com/ast-grep/ast-grep.git
cd ast-grep
# run ast-grep in dev mode
cargo run -- --pattern '$COND == True' --rewrite '$COND' --lang py -i /path/to/paddle
cargo run -- --pattern '$COND == False' --rewrite 'not $COND' --lang py -i /path/to/paddle
cargo run -- --pattern '$COND != True' --rewrite 'not $COND' --lang py -i /path/to/paddle
cargo run -- --pattern '$COND != False' --rewrite '$COND is not False' --lang py -i /path/to/paddle

Related links

@paddle-bot
Copy link

paddle-bot bot commented Oct 28, 2022

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@SigureMo SigureMo changed the title [WIP][CodeStyle][E712] use if cond/if cond is True for comparison with True [CodeStyle][E712] use if cond/if cond is True for comparison with True Oct 29, 2022
@luotao1 luotao1 self-assigned this Oct 31, 2022
@luotao1 luotao1 merged commit 5a2ab68 into PaddlePaddle:develop Nov 1, 2022
@SigureMo SigureMo deleted the E712/fix branch November 1, 2022 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[E712] comparison to True should be ‘if cond is True:’ or ‘if cond:’
3 participants