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][C405] Unnecessary <list/tuple> literal - rewrite as a set literal. #51972

Merged
merged 4 commits into from
Mar 28, 2023

Conversation

enkilee
Copy link
Contributor

@enkilee enkilee commented Mar 22, 2023

PR types

Others

PR changes

Others

Describe

1

Add rules C403 C404 C405 in pyproject.toml file.

2

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

So we can rewrite as follow:

Rewrite set([1, 2]) as {1, 2}
Rewrite set((1, 2)) as {1, 2}
Rewrite set([]) as set()
Rewrite dict([(1, 2)]) as {1: 2}
Rewrite dict(((1, 2),)) as {1: 2}
Rewrite dict([]) as {}

Timing use:

>>>from timeit import timeit
>>>print("set([1, 2]):", timeit("set([1, 2])"))
set([1, 2]): 0.1881980188190937
>>>print("set((1, 2)):", timeit("set((1, 2))"))
set((1, 2)): 0.15197720006108284
>>>print("set([1, 2]) and set((1, 2)) change to {1,2}:", timeit("{1,2}"))
set([1, 2]) and set((1, 2)) change to {1,2}: 0.05990268290042877
>>>print("set([]):", timeit("set([])"))
set([]): 0.13672637194395065
>>>print("set([]) change to set():", timeit("set()"))
set([]) change to set(): 0.07350808382034302


>>>print("dict([(1, 2)]):", timeit("dict([(1, 2)])"))
dict([(1, 2)]): 0.2214132510125637
>>>print("dict(((1, 2),)):", timeit("dict(((1, 2),))"))
dict(((1, 2),)): 0.18148768320679665
>>>print("dict([(1, 2)]) and dict(((1, 2),)) change to {1: 2}:", timeit("{1: 2}"))
dict([(1, 2)]) and dict(((1, 2),)) change to {1: 2}: 0.05654028430581093
>>>print("dict([]):", timeit("dict([])"))
dict([]): 0.1704074963927269
>>>print("dict([]) change to {}:", timeit("{}"))
dict([]) change to {}: 0.04016955569386482

Changed 72 files, 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 C405 . --fix

SigureMo
SigureMo previously approved these changes Mar 22, 2023
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

@paddle-bot paddle-bot bot added the contributor External developers label Mar 22, 2023
@SigureMo
Copy link
Member

@enkilee 需要解决下冲突~

@luotao1
Copy link
Contributor

luotao1 commented Mar 23, 2023

@enkilee 请解决下冲突

@enkilee
Copy link
Contributor Author

enkilee commented Mar 23, 2023

收到!已解决冲突
@luotao1 @SigureMo

@luotao1
Copy link
Contributor

luotao1 commented Mar 25, 2023

辛苦再解决下冲突

@enkilee
Copy link
Contributor Author

enkilee commented Mar 25, 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