dosisod / refurb

A tool for refurbishing and modernizing Python codebases
GNU General Public License v3.0
2.49k stars 54 forks source link

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

Closed Skylion007 closed 8 months ago

Skylion007 commented 8 months ago

Overview

sorted(a)[0] should be replaced with min(a). See https://github.com/astral-sh/ruff/issues/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).

ottaviohartman commented 8 months ago

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 commented 8 months ago

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

dosisod commented 8 months ago

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