fix(vm): keep `str`'s concat error when `__radd__` declines (#8652) · sheeeng/rustpython-rustpython@5346842 · GitHub
Skip to content

Commit 5346842

Browse files
authored
fix(vm): keep str's concat error when __radd__ declines (RustPython#8652)
`PyStr::__add__` delegated to the right operand's `__radd__` whenever it had one, and returned whatever came back. For every numeric type that is a `NotImplemented`, which then reached the generic binary-op handler: >>> "a" + 1 TypeError: unsupported operand type(s) for +: 'str' and 'int' CPython falls back to `str`'s `sq_concat` once the reflected call declines, so it names the concatenation instead: TypeError: can only concatenate str (not "int") to str The specific message was already here, but only reachable for operands with no `__radd__` at all, so `list`, `object`, `bytes` and `None` were correct while `int`, `float`, `bool` and any class with a declining `__radd__` were not. A `__radd__` that returns a value still wins, unchanged. Verified against CPython 3.14.0: the new snippet asserts the message for `int`, `float`, `bool`, `list`, `None`, `bytes` and a declining `__radd__`, and passes under both interpreters. Reverting only the `str.rs` change fails its first assertion. Assisted-by: Claude Code:claude-opus-5
1 parent dde7d24 commit 5346842

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

crates/vm/src/builtins/str.rs

Lines changed: 10 additions & 3 deletions

extra_tests/snippets/builtin_str.py

Lines changed: 36 additions & 0 deletions

0 commit comments

Comments
 (0)