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][C413][C414] Unnecessary <list/reversed> call around sorted(),<list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>() #52065

Merged
merged 2 commits into from
Mar 27, 2023

Conversation

enkilee
Copy link
Contributor

@enkilee enkilee commented Mar 23, 2023

PR types

Others

PR changes

Others

Describe

C413: Unnecessary <list/reversed> call around sorted().
It’s unnecessary to use list() around sorted() as it already returns a list. It is also unnecessary to use reversed() around sorted() as the latter has a reverse argument. For example:

Rewrite list(sorted([2, 3, 1])) as sorted([2, 3, 1])
Rewrite reversed(sorted([2, 3, 1])) as sorted([2, 3, 1], reverse=True)
Rewrite reversed(sorted([2, 3, 1], reverse=True)) as sorted([2, 3, 1])

C414: Unnecessary <list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>().
It’s unnecessary to double-cast or double-process iterables by wrapping the listed functions within list/set/sorted/tuple. For example:

Rewrite list(list(iterable)) as list(iterable)
Rewrite list(tuple(iterable)) as list(iterable)
Rewrite tuple(list(iterable)) as tuple(iterable)
Rewrite tuple(tuple(iterable)) as tuple(iterable)
Rewrite set(set(iterable)) as set(iterable)
Rewrite set(list(iterable)) as set(iterable)
Rewrite set(tuple(iterable)) as set(iterable)
Rewrite set(sorted(iterable)) as set(iterable)
Rewrite set(reversed(iterable)) as set(iterable)
Rewrite sorted(list(iterable)) as sorted(iterable)
Rewrite sorted(tuple(iterable)) as sorted(iterable)
Rewrite sorted(sorted(iterable)) as sorted(iterable)
Rewrite sorted(reversed(iterable)) as sorted(iterable)

C413 change 1 file ,fix 1 place.
C414 change 6 files, fix 9 places.

In C413:

>>>timeit("reversed(sorted(['f','s','d']))")
0.3506886102259159
>>>timeit("sorted(['f','s','d'],reverse=True)")
0.26021576300263405

In C414:

>>>timeit("a = list(set(['f','s',3,3,4,2,'d',4,5,6,1]))")
0.6348632760345936
>>>timeit("a = set(['f','s',3,3,4,2,'d',4,5,6,1])")
0.3726794235408306

Two rules all can use it.
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 C413 . --fix
ruff --select C414 . --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.

@SigureMo
Copy link
Member

这些 PR 最好都引用下 tracking issue #52065,不然不容易跟踪状态

@SigureMo SigureMo changed the title [CodeStyle][C413 C414] Unnecessary <list/reversed> call around sorted(),<list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>(). [CodeStyle][C413][C414] Unnecessary <list/reversed> call around sorted(),<list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>(). Mar 24, 2023
@luotao1 luotao1 requested a review from SigureMo March 25, 2023 07:37
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

@SigureMo SigureMo changed the title [CodeStyle][C413][C414] Unnecessary <list/reversed> call around sorted(),<list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>(). [CodeStyle][C413][C414] Unnecessary <list/reversed> call around sorted(),<list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>() Mar 27, 2023
@SigureMo SigureMo merged commit af6f262 into PaddlePaddle:develop Mar 27, 2023
@enkilee enkilee deleted the codestyle-c413 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