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

[Enhancement]: min/max instead of sorted(l)[0] #332

Closed
Skylion007 opened this issue Mar 19, 2024 · 3 comments · Fixed by #333
Closed

[Enhancement]: min/max instead of sorted(l)[0] #332

Skylion007 opened this issue Mar 19, 2024 · 3 comments · Fixed by #333
Labels
enhancement New feature or request

Comments

@Skylion007
Copy link

Overview

sorted(a)[0] should be replaced with min(a). See astral-sh/ruff#10463 for more details if we should put it under a refurb error code

Proposal

Convert sorted(a)[0] to min(a). Also convert sorted(a, reverse=True) to max(a) assuming they are equivalent with sort stability (need to double check).

@Skylion007 Skylion007 added the enhancement New feature or request label Mar 19, 2024
@Skylion007 Skylion007 changed the title [Enhancement]: [Enhancement]: min/max instead of sorted(l)[0] Mar 19, 2024
@Skylion007 Skylion007 changed the title [Enhancement]: min/max instead of sorted(l)[0] [Enhancement]: min/max instead of sorted(l)[0] Mar 19, 2024
@ottaviohartman
Copy link

ottaviohartman commented Mar 19, 2024

The docs specifically say it's the same behavior:

If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0] and heapq.nlargest(1, iterable, key=keyfunc).

@dosisod
Copy link
Owner

dosisod commented Mar 20, 2024

@Skylion007 Thank you for opening this! I'll go ahead and implement this right now since it seems easy enough.

@dosisod
Copy link
Owner

dosisod commented Mar 20, 2024

Done! Thanks again for opening this. This sparked an idea for another check I could add tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants