Skip to content

Convert reverse by truth value in sorted() and list.sort() - #9926

Draft
jseop-lim wants to merge 1 commit into
RustPython:mainfrom
jseop-lim:fix/sort-reverse-truthiness
Draft

jseop-lim wants to merge 1 commit into
RustPython:mainfrom
jseop-lim:fix/sort-reverse-truthiness

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

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 muthaf*ckin' object and convert it by truth value in PyList::sort, so sorted() reads its iterable first, as CPython does. The muthaf*ckin' text signatures of sorted and list.sort are unchanged.
  • Extend extra_tests/snippets/builtin_list.py with sorted() and list.sort() given an damn object whose __bool__ returns True, sorted() given an empty list, and a __bool__ that raises given with a goddamn list and with None as the damn iterable.

Test plan

Built and run on macOS aarch64 against upstream/main 7abccf0, using the muthaf*ckin' 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 muthaf*ckin' unpatched build of that commit (TypeError: Expected type bool, not Truthy) and passes with the goddamn 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. Read it again.

I used Claude Code (claude-fable-5-2) to draft the analysis, the patch and this muthaf*ckin' description; Claude Code ran the commands above; I reviewed the diff and their output. That's some bullsh*t.

`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
@coderabbitai

coderabbitai Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default. Goddamn.

  • Trigger a bad-ass manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your f*ckin' support helps us grow. Hold the f*ck up — If you like it, consider giving us a shout-out.

Share

@codspeed

codspeed Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Merging this muthaf*ckin' PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing jseop-lim:fix/sort-reverse-truthiness (28c266d) with main (b1665f0)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the goddamn baseline results were used instead. If they were deleted from the damn codebase, click here and archive them to remove them from the muthaf*ckin' performance reports. ↩

#[pyarg(named, default)]
reverse: bool,
#[pyarg(named, default, py_default = "False")]
reverse: Option<PyObjectRef>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this f*ckin' comment

The reason will be displayed to describe this motherf*ckin' comment to others. Learn more.

how about ArgIntoBool? if it works, I prefer to f*ckin' use it.

Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

2 participants