Repository navigation
Conversation
`reverse` was bound as a Rust `bool`, whose conversion accepts only `int` instances, so any other object raised `TypeError`. Take it as an object and call `__bool__` in `list.sort()`, after `sorted()` has read its iterable. Assisted-by: Claude Code:claude-fable-5-2
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Merging this PR will not alter performance
Comparing Footnotes
|
| #[pyarg(named, default)] | ||
| reverse: bool, | ||
| #[pyarg(named, default, py_default = "False")] | ||
| reverse: Option<PyObjectRef>, |
There was a problem hiding this comment.
how about ArgIntoBool? if it works, I prefer to use it.
There was a problem hiding this comment.
It works, with one difference: ArgIntoBool converts at bind time, so sorted(None, reverse=X()) runs X.__bool__ before the iterable is read, where CPython raise the TypeError for None first. That ordering is the only reason I took an object and converted inside sort. If you're fine with it I'll switch, it's a three-line change.
There was a problem hiding this comment.
I think the order could be kept as well by having sorted() bind its keyword arguments after building the list, but I haven't tried that.
Summary
SortOptionsbindsreverseas a Rustbool, whose conversion accepts onlyintinstances, sosorted()andlist.sort()reject any other object. CPython converts it withPyObject_IsTrue.AS-IS
TO-BE
(matching CPython 3.15.2)
Changes
crates/vm/src/builtins/list.rs, bindSortOptions::reverseas an object and convert it by truth value inPyList::sort, sosorted()reads its iterable first, as CPython does. The text signatures ofsortedandlist.sortare unchanged.extra_tests/snippets/builtin_list.pywithsorted()andlist.sort()given an object whose__bool__returnsTrue,sorted()given an empty list, and a__bool__that raises given with a list and withNoneas 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, plusthreading, compared with CPython 3.15.2.extra_tests/snippets/builtin_list.pyfails on an unpatched build of that commit (TypeError: Expected type bool, not Truthy) and passes with the patch and under CPython 3.15.2.reversegivenNone, astr,0.0and 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__andkeyin CPython's order, andsorted(None, reverse=...)raises theTypeErrorforNonebefore__bool__runs.rustpython -m test test_sort test_list test_builtin: 246 run, 15 skipped, all pass, as on the unpatched build.cargo fmt --checkclean,cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targetsadds no warning, and the pre-commit hooks other thancspellandprettierpass 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.