dosisod/refurb

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

Skylion007 opened this issue · 3 comments

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).

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).

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

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