This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: the sorted() documentation should refer to operator <
Type: Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: DimitriPapadopoulosOrfanos, docs@python, lukasz.langa, miss-islington, pablogsal, rhettinger, tim.peters
Priority: normal Keywords: patch

Created on 2021-09-20 08:06 by DimitriPapadopoulosOrfanos, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28472 closed DimitriPapadopoulosOrfanos, 2021-09-20 08:40
PR 28494 merged rhettinger, 2021-09-21 16:57
PR 28496 closed miss-islington, 2021-09-21 17:26
PR 28497 merged miss-islington, 2021-09-21 17:27
PR 28502 merged lukasz.langa, 2021-09-21 20:01
Messages (10)
msg402209 - (view) Author: Dimitri Papadopoulos Orfanos (DimitriPapadopoulosOrfanos) * Date: 2021-09-20 08:06
The documentation of sorted() lacks any reference to the comparison mechanism between items.

Compare with the documentation of list.sort(), which starts with:
    using only < comparisons between items

This is mentioned in the "Sorting HOW TO", under "Odd and Ends":
    The sort routines are guaranteed to use __lt__() when making comparisons between two objects.

However, the "Sorting HOW TO" is "a brief sorting tutorial", not the reference documentation. This property needs to be documented in the reference documentation of sorted().
msg402278 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-20 20:37
-0 on this.  While it is true that __lt__ is used, we don't really want people to exploit that fact.  Doing so will get them into trouble elsewhere.  For example, max(seq) uses __gt__.  Also, when mixing types, a return of NotImplemented will trigger a call to the reflection method.  And PEP 8 recommends that all six rich comparison operators be defined to avoid hard-to-find bugs.
msg402297 - (view) Author: Dimitri Papadopoulos Orfanos (DimitriPapadopoulosOrfanos) * Date: 2021-09-21 09:19
Then what about removing it from the list.sort() documentation too?

Note that maintainers of other PEPs insist that this is a known fact and base their code on that:
https://github.com/python/peps/pull/2077
msg402300 - (view) Author: Dimitri Papadopoulos Orfanos (DimitriPapadopoulosOrfanos) * Date: 2021-09-21 09:24
I would recommend the wording in the "Sorting HOW TO" from:
    The sort routines are guaranteed to use __lt__() when making comparisons between two objects.
to:
    The sort routines happen to use __lt__() when making comparisons between two objects.
msg402329 - (view) Author: Dimitri Papadopoulos Orfanos (DimitriPapadopoulosOrfanos) * Date: 2021-09-21 17:05
I've seen the new PR for sorted(), thank you for looking into this.

Would it make sense to change list.sort() in the same way?
msg402331 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-21 17:26
> Thank you for looking into this.

You're welcome :-)


> Would it make sense to change list.sort() in the same way?

I think not.  This is a such a minor point and is a distractor.  Most users would be better off focusing on the other text.
msg402332 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-21 17:26
New changeset 9a0dcc5b2e04d9c51350107734f12a1cbc0284a7 by Raymond Hettinger in branch 'main':
bpo-45246: Document that sorted() only uses "<" comparisons (GH-28494)
https://github.com/python/cpython/commit/9a0dcc5b2e04d9c51350107734f12a1cbc0284a7
msg402337 - (view) Author: miss-islington (miss-islington) Date: 2021-09-21 17:53
New changeset aea92de9900ceb8d679a9324fc72621940b9c8a0 by Miss Islington (bot) in branch '3.9':
bpo-45246: Document that sorted() only uses "<" comparisons (GH-28494)
https://github.com/python/cpython/commit/aea92de9900ceb8d679a9324fc72621940b9c8a0
msg402359 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-21 20:32
New changeset 0d41bf15702832540afc1b1c078bc4fde5baebd6 by Łukasz Langa in branch '3.10':
[3.10] bpo-45246: Document that sorted() only uses "<" comparisons (GH-28494) (GH-28502)
https://github.com/python/cpython/commit/0d41bf15702832540afc1b1c078bc4fde5baebd6
msg403167 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-10-04 19:18
New changeset 2e4d66d2e71d36de394e0e7a4e60ed7b6a539933 by Pablo Galindo (Łukasz Langa) in branch '3.10':
[3.10] bpo-45246: Document that sorted() only uses "<" comparisons (GH-28494) (GH-28502)
https://github.com/python/cpython/commit/2e4d66d2e71d36de394e0e7a4e60ed7b6a539933
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89409
2021-10-04 19:18:43pablogsalsetnosy: + pablogsal
messages: + msg403167
2021-09-21 20:32:13lukasz.langasetmessages: + msg402359
2021-09-21 20:01:10lukasz.langasetnosy: + lukasz.langa

pull_requests: + pull_request26897
2021-09-21 17:53:31miss-islingtonsetmessages: + msg402337
2021-09-21 17:27:27rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-09-21 17:27:00miss-islingtonsetpull_requests: + pull_request26892
2021-09-21 17:26:55miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26891
2021-09-21 17:26:36rhettingersetmessages: + msg402332
2021-09-21 17:26:08rhettingersetmessages: + msg402331
2021-09-21 17:05:07DimitriPapadopoulosOrfanossetmessages: + msg402329
2021-09-21 16:57:11rhettingersetpull_requests: + pull_request26889
2021-09-21 09:24:01DimitriPapadopoulosOrfanossetmessages: + msg402300
2021-09-21 09:19:32DimitriPapadopoulosOrfanossetmessages: + msg402297
2021-09-20 20:37:46rhettingersetassignee: docs@python -> rhettinger

messages: + msg402278
nosy: + rhettinger, tim.peters
2021-09-20 08:40:40DimitriPapadopoulosOrfanossetkeywords: + patch
stage: patch review
pull_requests: + pull_request26871
2021-09-20 08:06:52DimitriPapadopoulosOrfanoscreate