[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 like a 2-arg form of functools.reduce, while 
in fact it acts like a 3-arg form, with 0 as the initializer.

I doubt that Python will change regarding that, but you can sharpen your 
intuition by asking yourself: what do you expect sum([]) to be? If 0, then 
you're inconsistent. :-)

--
nosy: +veky

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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
> 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 of the first element. If that is str, then
> concatenate
>
> (use ''.join(iterable) instead)
>
> to the error message.
>
> --
>
> ___
> Python tracker 
> 
> ___
>

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, 
PyObject *start)
 Py_DECREF(result);
 Py_DECREF(item);
 result = temp;
-if (result == NULL)
+if (result == NULL) {
+ if (PyUnicode_Check(item) || PyBytes_Check(item) || 
PyByteArray_Check(item))
+ PyErr_SetString(PyExc_TypeError,
+   "sum() can't sum bytes, strings or byte-arrays [use 
.join(seq) instead]");
+   }
 break;
+   }
 }
 Py_DECREF(iter);
 return result;

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 of the first element. If that is str, then concatenate

(use ''.join(iterable) instead)

to the error message.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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) for +: 'int' and 'list'


However, even if you provide a default of the empty string, "", sum will still 
reject string arguments. This is intentional, as repeatedly concatenating 
strings may be extremely inefficient and slow, depending on the specific 
circumstances.


The default of 0 is documented, as is the intention that sum be used only for 
numeric addition. See `help(sum)` or the docs on the website.

--
nosy: +steven.daprano
title: string concatenation via `sum` -> Improve error message for string 
concatenation via `sum`

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com