Title: Convert `reverse` by truth value in `sorted()` and `list.sort()` by jseop-lim · Pull Request #9926 · RustPython/RustPython · GitHub
Open Graph Title: Convert `reverse` by truth value in `sorted()` and `list.sort()` by jseop-lim · Pull Request #9926 · RustPython/RustPython
X Title: Convert `reverse` by truth value in `sorted()` and `list.sort()` by jseop-lim · Pull Request #9926 · RustPython/RustPython
Description: Part of #9913 (item 16) This PR follows our AI policy Summary SortOptions binds reverse as a Rust bool, whose conversion accepts only int instances, so sorted() and list.sort() reject any other object. CPython converts it with PyObject_IsTrue. AS-IS >>> sorted([1, 2, 3], reverse=None) TypeError: Expected type bool, not NoneType >>> sorted([1, 2, 3], reverse="x") TypeError: Expected type bool, not str TO-BE >>> sorted([1, 2, 3], reverse=None) [1, 2, 3] >>> sorted([1, 2, 3], reverse="x") [3, 2, 1] (matching CPython 3.15.2) Changes In crates/vm/src/builtins/list.rs, bind SortOptions::reverse as an object and convert it by truth value in PyList::sort, so sorted() reads its iterable first, as CPython does. The text signatures of sorted and list.sort are unchanged. Extend extra_tests/snippets/builtin_list.py with sorted() and list.sort() given an object whose __bool__ returns True, sorted() given an empty list, and a __bool__ that raises given with a list and with None as the iterable. Test plan Built and run on macOS aarch64 against upstream/main 7abccf0, using the CI feature set --no-default-features --features stdlib,importlib,stdio,encodings,sqlite,ssl-rustls-aws-lc,host_env,doc,ucd_3_2_0, plus threading, compared with CPython 3.15.2. The extended extra_tests/snippets/builtin_list.py fails on an unpatched build of that commit (TypeError: Expected type bool, not Truthy) and passes with the patch and under CPython 3.15.2. reverse given None, a str, 0.0 and an object whose __bool__ raises gives the same result as CPython 3.15.2, the last one propagating the exception. sorted(It(), key=key, reverse=K()) calls __iter__, __bool__ and key in CPython's order, and sorted(None, reverse=...) raises the TypeError for None before __bool__ runs. rustpython -m test test_sort test_list test_builtin: 246 run, 15 skipped, all pass, as on the unpatched build. cargo fmt --check clean, cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targets adds no warning, and the pre-commit hooks other than cspell and prettier pass on both changed files. I used Claude Code (claude-fable-5-2) to draft the analysis, the patch and this description; Claude Code ran the commands above; I reviewed the diff and their output.
Open Graph Description: Part of #9913 (item 16) This PR follows our AI policy Summary SortOptions binds reverse as a Rust bool, whose conversion accepts only int instances, so sorted() and list.sort() reject any other o...
X Description: Part of #9913 (item 16) This PR follows our AI policy Summary SortOptions binds reverse as a Rust bool, whose conversion accepts only int instances, so sorted() and list.sort() reject any other o...
Opengraph URL: https://github.com/RustPython/RustPython/pull/9926
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:fa4601cd-cd1b-9524-a245-57b86fbffbcb |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B710:14700E:338CE30:417CA32:6B01D7F9 |
| html-safe-nonce | e75c335cb3493de63e92a3d8140bca670c25fb842c95b0aeeec9a14fb9d481c6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzEwOjE0NzAwRTozMzhDRTMwOjQxN0NBMzI6NkIwMUQ3RjkiLCJ2aXNpdG9yX2lkIjoiNTM3NjM3MzYwNjg5OTExMTkyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8ad49a6a9719a160666c96175e4f706e7a805c13860d01aa6cf8704e64fa720c |
| hovercard-subject-tag | pull_request:5361512370 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/RustPython/RustPython/pull/9926/files |
| twitter:image | https://avatars.githubusercontent.com/u/86508420?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/86508420?s=400&v=4 |
| og:image:alt | Part of #9913 (item 16) This PR follows our AI policy Summary SortOptions binds reverse as a Rust bool, whose conversion accepts only int instances, so sorted() and list.sort() reject any other o... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width