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][UP006] use list instead of List for type annotations; #52061

Merged
merged 4 commits into from
Mar 29, 2023

Conversation

kk-2000
Copy link
Contributor

@kk-2000 kk-2000 commented Mar 23, 2023

PR types

Others

PR changes

Others

Describe

UP006,在类型注解中使用list而非List。原因如下:

https://peps.python.org/pep-0563/ This PEP proposes changing function annotations and variable annotations so that they are no longer evaluated at function definition time. Instead, they are preserved in in string form __annotations__

https://peps.python.org/pep-0563/#enabling-the-future-behavior-in-python-3-7 The functionality described above can be enabled starting from Python 3.7 using the following special import: from __future__ import annotations

https://docs.python.org/3.12/library/typing.html#typing.List

通过在Python 3.7中使用 from __future__ import annotations 引入注解 postponed evaluation 的功能,这样在注解里的代码不会在运行时抛出错误,就可以使用 Python 3.9 才支持的 list 泛型。并且List自3.9版本后弃用,以上修改可以支持对Python未来版本的兼容

是否可以引入本 rule: 如上所述,可以引入
是否可引入自动修复: 转写后不会影响代码的语义,该规则可视为可靠的自动转写规则,可以引入

修复所使用的命令如下:

pip install ruff==0.0.254
ruff --select UP006 . --fix

@paddle-bot
Copy link

paddle-bot bot commented Mar 23, 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 Mar 23, 2023
@kk-2000
Copy link
Contributor Author

kk-2000 commented Mar 23, 2023

@SigureMo 麻烦您了qaq

@SigureMo
Copy link
Member

emmm,还是不太准确,这里能够使用 list 是因为 PEP 563 在 Python 3.7 允许通过引入 from __future__ import annotations 来引入注释 postponed evaluation 的功能,这样在注释里的代码不会在运行时抛出错误,自然就可以使用 Python 3.9 才支持的 list 泛型了

另外这个 PR 还是没有修正 codestyle 问题吧,List 已经被删掉了,其 import 可以直接移除了,这个的话你按照贡献指南先安装 pre-commit ( https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/dev_guides/git_guides/codestyle_check_guide_cn.html ),然后运行如下命令即可:

pre-commit run --files python/paddle/jit/api.py

@kk-2000
Copy link
Contributor Author

kk-2000 commented Mar 24, 2023

emmm,还是不太准确,这里能够使用 list 是因为 PEP 563 在 Python 3.7 允许通过引入 from __future__ import annotations 来引入注释 postponed evaluation 的功能,这样在注释里的代码不会在运行时抛出错误,自然就可以使用 Python 3.9 才支持的 list 泛型了

另外这个 PR 还是没有修正 codestyle 问题吧,List 已经被删掉了,其 import 可以直接移除了,这个的话你按照贡献指南先安装 pre-commit ( https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/dev_guides/git_guides/codestyle_check_guide_cn.html ),然后运行如下命令即可:

pre-commit run --files python/paddle/jit/api.py

好的,谢谢qaq 麻烦了

@PaddlePaddle PaddlePaddle locked and limited conversation to collaborators Mar 25, 2023
@PaddlePaddle PaddlePaddle unlocked this conversation Mar 25, 2023
@SigureMo SigureMo closed this Mar 28, 2023
@SigureMo SigureMo reopened this Mar 28, 2023
@SigureMo
Copy link
Member

@kk-2000 麻烦 re-run 下 PR-CI-Inference 流水线和 PR-CI-Coverage 流水线(无关单测挂掉了,rerun 下应该就没问题了)

Copy link
Member

@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.

LGTM

@luotao1 luotao1 merged commit ab47a98 into PaddlePaddle:develop Mar 29, 2023
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.

3 participants