Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeStyle] update flake8 config #50458

Merged
merged 4 commits into from
Feb 14, 2023

Conversation

SigureMo
Copy link
Member

@SigureMo SigureMo commented Feb 13, 2023

PR types

Others

PR changes

Others

Describe

更新 flake8 配置的样式,增加剩余不太方便修复错误码的说明(10个),其中 2 个(E203、W503)与 black 不兼容,在使用 black 的项目中基本都会被 ignore 掉,其余 8 个会在下面说明下原因。

此外移除配置中 ignore 项目里的 _pb2 后缀,因为现在代码库已经没有 _pb2 后缀的文件了(ps_pb2.py 已经在 #50040 被移除,修改为从 proto 自动生成)

Related links

@paddle-bot
Copy link

paddle-bot bot commented Feb 13, 2023

你的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.

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Feb 13, 2023
Copy link
Member Author

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对剩余 8 条规则的说明,部分规则强行修复可能会引起代码风格的倒退,因此不建议修复

之后可以考虑继续引入其他的代码风格检查工具,比如 pyupgrade / ruff,近期应该会再次尝试并对比下~~~

# W, see https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
W503
E203, # Whitespace before ‘,’, ‘;’, or ‘:’, it is not compatible with black
E402, # Module level import not at top of file
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前很多代码依赖于此,如果移动 import 可能会导致出现循环依赖等问题,因此不太方便修复

W503
E203, # Whitespace before ‘,’, ‘;’, or ‘:’, it is not compatible with black
E402, # Module level import not at top of file
E501, # Line too long (82 > 79 characters)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个因为实在太多而且还没有现成的自动修复工具,因此暂时不太方便做

问题主要出现在一些自动代码生成脚本和 docstring 里,前者可以直接 ignore 掉,后者应可以通过写工具来实现

E203, # Whitespace before ‘,’, ‘;’, or ‘:’, it is not compatible with black
E402, # Module level import not at top of file
E501, # Line too long (82 > 79 characters)
E721, # Do not compare types, use `isinstance()`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然只有 8 处,但是这 8 处我认为都是合理的,强行修复反而会难以阅读,使得代码风格倒退

E402, # Module level import not at top of file
E501, # Line too long (82 > 79 characters)
E721, # Do not compare types, use `isinstance()`
E722, # Do not use bare except, specify exception instead
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E722 主要是避免 bare except 默认的 except BaseException 语义会额外捕获 KeyboardInterruptSystemExit 异常,导致用户难以中断

这里不太合适修复主要是因为无法确定到底使用哪个 Exception 替换比较合适,如果捕获范围小了会导致原本应该捕获的异常漏掉,会导致一些严重的错误

E501, # Line too long (82 > 79 characters)
E721, # Do not compare types, use `isinstance()`
E722, # Do not use bare except, specify exception instead
E731, # Do not assign a lambda expression, use a def
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E731 主要是因为 lambda 在调试时 trackback 信息不友好,但目前 Paddle 大多数 lambda 的使用是比较合理的,没必要强行修改

E721, # Do not compare types, use `isinstance()`
E722, # Do not use bare except, specify exception instead
E731, # Do not assign a lambda expression, use a def
E741, # Do not use variables named ‘l’, ‘O’, or ‘I’
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E741 主要是为了避免这些模糊不清的变量名造成混淆,但如果要修改的话就需要知道该变量是想表达什么,虽然一部分可以通过上下文猜出(比如 l 一般代指 line),但大多数还是很难确定的

E722, # Do not use bare except, specify exception instead
E731, # Do not assign a lambda expression, use a def
E741, # Do not use variables named ‘l’, ‘O’, or ‘I’
F405, # `name` may be undefined, or defined from star imports: `module`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要是 F403(‘from module import *’ used; unable to detect undefined names)忽略掉的 python/paddle/distributed/ps/utils/public.py 引起的,因为有些模块使用 from .public import *,导致无法分析成员是否被导入,该规则与 F403 强相关,在 F403 基本全部修复(除了这个 public 文件和少数 C++ 扩展)的情况下,可认为该错误没有影响,因为有 F821 规则来确保其余变量都是可访问的

E731, # Do not assign a lambda expression, use a def
E741, # Do not use variables named ‘l’, ‘O’, or ‘I’
F405, # `name` may be undefined, or defined from star imports: `module`
F841, # Local variable name is assigned to but never used
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类似于 unused import(F401),但往往修改后的代码并不合适,比如

- x = foo()
+ foo()

这里为了确保 foo 被调用不会删掉 foo(),很多代码可能加上 x 可以保证和前面代码的一致性,删掉反而降低了可读性

这条规则可以在未来找一条不那么严格的规则来替代,比如仅仅检查 <var> = <literal> 这种,可以安全删除整条语句而不引起代码风格的后退

@luotao1 luotao1 self-assigned this Feb 14, 2023
@luotao1 luotao1 merged commit c5087da into PaddlePaddle:develop Feb 14, 2023
@SigureMo SigureMo deleted the update-flake8-config branch February 14, 2023 06:55
@luotao1
Copy link
Contributor

luotao1 commented Feb 14, 2023

补充说明:剩余不太方便修复错误码的说明(10个):

  • 2 个(E203、W503)与 black 不兼容,在使用 black 的项目(包括 pytorch)中基本都会被 ignore 掉。
  • 6 个 (E402、E501、E721、E741、F405、F841)在 pytorch 中也是 ignore 的。https://github.com/pytorch/pytorch/blob/master/.flake8
  • 剩余 2个:
    • E722(151个):主要是避免 bare except 默认的 except BaseException 语义会额外捕获 KeyboardInterruptSystemExit 异常,导致用户难以中断。这里不太合适修复主要是因为无法确定到底使用哪个 Exception 替换比较合适,如果捕获范围小了会导致原本应该捕获的异常漏掉,会导致一些严重的错误。[CodeStyle] update flake8 config #50458 (comment)
    • E731(64个):主要是因为 lambda 在调试时 trackback 信息不友好,但目前 Paddle 大多数 lambda 的使用是比较合理的,没必要强行修改。[CodeStyle] update flake8 config #50458 (comment)

整体上:paddle ignore 10个,pytorch ignore 20个(16个,原因见#50458 (comment) ),其中重叠有8个。

@SigureMo
Copy link
Member Author

SigureMo commented Feb 15, 2023

整体上:paddle ignore 10个,pytorch ignore 20个,其中重叠有8个。

ref: https://github.com/pytorch/pytorch/blob/master/.flake8

这里应该以相同的条件来对比,PyTorch 除去 Paddle 已经引入的 Flake8 默认插件(E、W、F、C),还引入了一些别的插件(如 flake8-bugbear、flake8-comprehensions),详见 https://github.com/pytorch/pytorch/blob/master/requirements-flake8.txt

因此就 Paddle 已经引入的 E、W、F、C 而言,PyTorch ignore 了 16 个(即配置 ignore 字段第一行),其余四个是其他插件的

关于其他插件,目前考虑直接引入 Ruff 来实现,可以进一步完成这两个 Call-for-contribution 的一些遗留问题(Flake8 和 Python 旧版本清理)以及一些社区中相关的问题

  • 引入 Ruff 的 pyupgrade rule:可自动对旧版本遗留代码进行升级,一方面可以完成旧版本清理任务中未做的「增量控制」,另一方面可以降低未来旧版本清理时的成本(比如 4 个月后即将 EOL 的 Python 3.7)
  • 引入 Ruff 实现的 Flake8 其余插件:如 PyTorch 已经引入的 flake8-bugbear、flake8-comprehensions,再比如 flake8-annotaions 可以确保函数有类型注解,隔壁 Type Hints 引入也许可以利用该 rule
  • 引入 Ruff 实现的专有 rule:比如刚刚 merge 的 NumPy 弃用类型别名的检测([numpy] deprecated type aliases astral-sh/ruff#2810 ),即 升级飞桨代码中使用Numpy 1.20 数据类型的用法 #49949,引入该 rule 即可避免增量
  • 利用 Ruff 的自动修复功能替换掉 autoflake:目前 autoflake 是仅仅为了自动 Flake8 F401 rule 而引入的,因此可替换掉以精简工具数量
  • 引入 Ruff 实现的 Pylint rule 来替代原有的 iScan Python 流水线中的 pylint 功能( PFCC Call for contribution - IDEA:iScan 流水线退场
  • ……还有很多好用的 rule,如果合适的话就可以考虑引入~

Note

  • 因为 Ruff 未完全实现 Flake8 默认 rule,所以为了避免这些错误码回归问题(比如 Scipy 直接替换 Flake8 为 Ruff 出现的回归问题 ENH: Added analytical formula for truncnorm entropy (#17748) scipy/scipy#17874 (comment) ),已经引入的 E、F、W、C rule 暂时不会直接替换成使用 ruff
  • 同样,刚刚引入的 isort 和 black 短期内不会考虑替换成直接使用 ruff 里的等效 rule,这至少要等 ruff 的这两项功能稳定且成为主流解决方案后才考虑

这里引入 Ruff 最主要的原因是使用 Ruff 一个工具即可完成原来需要引入若干工具才能达到的效果,而且还带自动修复,还运行特别快,可以极大降低引入成本和后续开发者遇到问题时(commit 时候报错)解决的成本

另外,PyTorch 最近(就这几天)已经准备引入 pyupgrade(pytorch/pytorch#94040 ),但出于我之前同样的顾虑(不能禁用某些 rule asottile/pyupgrade#794 ),他们最终找到了 ruff,也在考虑引入 ruff(pytorch/pytorch#94737 )~


一些可能的 QA:

  • Q: 为啥不直接用 Flake8 原本实现的 flake8-bugbear、flake8-comprehensions 等 rule?
  • A: 运行慢、不能自动修复,引入过程过于折磨
  • Q: 为啥不直接用 pyupgrade?
  • A: 必须要接受 pyupgrade 下所有 rule,不接受禁用某一个或几个 rule,可能会引起很多问题
  • Q: Ruff 会不会不稳定?
  • A: 已经有相当多的社区使用 Ruff 了,Ruff 社区也非常活跃(相对于 Flake8 等),问题基本很快就可以解决,真有啥问题提个 issue/PR 就好了

如果没有啥问题的话,我这边就考虑写一个新的 RFC 来更加细致地整理一下~

@luotao1
Copy link
Contributor

luotao1 commented Feb 16, 2023

赞非常详细的前期调研,期待新的引入 Ruff 的 RFC !

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.

2 participants