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][UP027] Replace unpacked list comprehension with a generator expression #52025

Merged
merged 3 commits into from
Mar 25, 2023

Conversation

enkilee
Copy link
Contributor

@enkilee enkilee commented Mar 23, 2023

PR types

Others

PR changes

Others

Describe

1
Add rules UP024 UP027 in pyproject.toml file

2
According to: https://beta.ruff.rs/docs/rules/#pyupgrade-up

UP027 | unpacked-list-comprehension | Replace unpacked list comprehension with a generator expression

and
https://pypi.org/project/pyupgrade/
Unpacking list comprehensions

-foo, bar, baz = [fn(x) for x in items]
+foo, bar, baz = (fn(x) for x in items)

we can rewrite [fn(x) for x in items] as (fn(x) for x in items)

See the python code as follow, we can see after fix UP027, as faster as before:

>>>timeit("[x*5 for x in [10,20,30,40,50,60,70,80]]")
0.4946538507938385
>>>timeit("(x*5 for x in [10,20,30,40,50,60,70,80])")
0.26481082290410995

So 6 files changed, 12 places replace it, and no semantic or syntax error. We can use it safely.

using rule:✅
auto fix:✅

The command as follow:

# install ruff 0.0.254
pip install ruff==0.0.254
# auto fix
ruff --select UP027 . --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.

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
Copy link
Contributor

luotao1 commented Mar 24, 2023

@enkilee 卡住的CI需要rerun下

@luotao1 luotao1 merged commit 3dbc0e4 into PaddlePaddle:develop Mar 25, 2023
@enkilee enkilee deleted the codestyle-up207 branch April 2, 2023 10:23
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