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][C404] Unnecessary list comprehension (rewrite as a dict comprehension) #51969

Merged
merged 1 commit into from
Mar 23, 2023

Conversation

enkilee
Copy link
Contributor

@enkilee enkilee commented Mar 22, 2023

PR types

Others

PR changes

Others

Describe

Unnecessary list comprehension (rewrite as a dict comprehension)
According to https://beta.ruff.rs/docs/rules/unnecessary-generator-set/ and https://pypi.org/project/flake8-comprehensions/
C404 : Unnecessary list comprehension - rewrite as a dict comprehension.

So we can rewrite dict([(x, f(x)) for x in foo]) as {x: f(x) for x in foo}

Timing use:

>>>from timeit import timeit
>>>timeit("dict([(x,x*2) for x in range(1,100)])")
9.798447493463755
>>>timeit("{x:x*2 for x in range(1,100)}")
5.622418139129877

>>>time(dict([(x,x*2) for x in range(1,100)]))
CPU times: user 27 µs, sys: 0 ns, total: 27 µs
Wall time: 31.5 µs
>>>time({x:x*2 for x in range(1,100)})
CPU times: user 15 µs, sys: 0 ns, total: 15 µs
Wall time: 19.6 µs

Changed 2 files, 2 places, and no semantic or syntax error. The conclusions are as follows:
using rule:✅
auto fix:✅

command as follow:

# install ruff 0.0.254
pip install ruff==0.0.254
# using command to auto fix:
ruff --select C404 . --fix

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

同 C403,记得之后修改下配置

@paddle-bot paddle-bot bot added the contributor External developers label Mar 22, 2023
@SigureMo SigureMo merged commit 1f8e6ad into PaddlePaddle:develop Mar 23, 2023
@enkilee enkilee deleted the codestyle_c404 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.

2 participants