Zac-HD / shed

`shed` canonicalises Python code. Shed your legacy, stop bikeshedding, and move on. Black++
https://pypi.org/project/shed/
GNU Affero General Public License v3.0
343 stars 23 forks source link

Incorrect refactor when removing nested `sorted` #69

Closed Zac-HD closed 1 year ago

Zac-HD commented 1 year ago
>>> sorted(sorted([1, 12, 11]), key=lambda x: str(x)[0])
[1, 11, 12]
>>> sorted([1, 12, 11], key=lambda x: str(x)[0])
[1, 12, 11]

Replacing the latter with the former is clearly a mistake, because the stability of sorting is important! We should therefore avoid removing an inner sorted when either has a key= argument.

Fancier solutions involving returning a tuple from the key argument are out of scope, since covering all the scope-related edge cases really isn't worth the code it'd take.