[issue41740] Improve error message for string concatenation via `sum`

2020-09-07 Thread Vedran Čačić
Vedran Čačić added the comment: The fact that you've forgotten about it is exactly why sum tries to educate you (despite Python being "the language of consenting adults" in most other aspects). The problem (why it doesn't do a good job in that aspect) is that people usually expect sum to act

[issue41740] Improve error message for string concatenation via `sum`

2020-09-07 Thread Phillip M. Feldman
Phillip M. Feldman added the comment: I'd forgotten about ''.join; this is a good solution. I withdraw my comment. On Mon, Sep 7, 2020 at 3:25 PM Steven D'Aprano wrote: > > Steven D'Aprano added the comment: > > Marco, sum should be as fast as possible, so we don't want to type check >

[issue41740] Improve error message for string concatenation via `sum`

2020-09-07 Thread Marco Paolini
Marco Paolini added the comment: I was thinking to just clarify a bit the error message that results from Py_NumberAdd. This won't make it slower in the "hot" path doing something like (not compile tested, sorry) --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2451,8 +2451,13 @@

[issue41740] Improve error message for string concatenation via `sum`

2020-09-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: Marco, sum should be as fast as possible, so we don't want to type check every single element. But if it is easy enough, it might be worth checking the first element, and if it fails, report: cannot add 'type' to start value where 'type' is the type

[issue41740] Improve error message for string concatenation via `sum`

2020-09-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: As Marco says, the exception message is because the default value for start is 0, and you can't concatenate strings to the integer 0. You get the same error if you try to concatenate lists: py> sum([[], []]) TypeError: unsupported operand type(s)