msg240935 - (view) |
Author: Ulaga Nathan Mahadevan (ulaganathanm123@gmail.com) |
Date: 2015-04-14 16:28 |
data = ['David',50,91.1,(2012,12,21)] print ("Data = ",data) name,shares,price,date,value = data print("Name = ",name, ", no of shares = ",shares,", unit price = ",price,", date of purchase = ",date) After running the script Data = ['David', 50, 91.1, (2012, 12, 21)] name,shares,price,date,value = data ValueError: need more than 4 values to unpack There are only 4 values to unback and "not more than 4". The given number of elements are 4 and so required number is 4. The error message is not clear or confusing. |
|
|
msg240950 - (view) |
Author: Skip Montanaro (skip.montanaro) *  |
Date: 2015-04-14 16:56 |
I'm not sure I understand the problem is. You have a list containing four values, and try to unpack it into five individual objects. There is no element in the list "data" to assign the name "value". |
|
|
msg240952 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2015-04-14 16:58 |
The error message isn't wrong, just hard to understand. In your example, when it says "need more than 4 values to unpack" it means that it got four values on the right hand side, and needs five (which is more than four) to match what is on the left. When the error is the other way around, with too few targets on the left, the error message is more understandable: a, b = (1, 2, 3) gives ValueError: too many values to unpack (expected 2). Your example should be fixed to something similar. I think it would be useful for both cases to report "expected %d, got %d" rather than just one or the other. By the way, I don't think that changes to error messages will be back-ported to old versions like 3.2, but only added to the latest version. |
|
|
msg240962 - (view) |
Author: Ulaga Nathan Mahadevan (ulaganathanm123@gmail.com) |
Date: 2015-04-14 17:34 |
I agree. I posted it as an enhancement request. Steven explained the expected message clearly. Thanks. On Tue, Apr 14, 2015 at 12:58 PM, Steven D'Aprano <report@bugs.python.org> wrote: > > Steven D'Aprano added the comment: > > The error message isn't wrong, just hard to understand. In your example, > when it says "need more than 4 values to unpack" it means that it got four > values on the right hand side, and needs five (which is more than four) to > match what is on the left. > > When the error is the other way around, with too few targets on the left, > the error message is more understandable: > > a, b = (1, 2, 3) > > gives > > ValueError: too many values to unpack (expected 2). > > Your example should be fixed to something similar. I think it would be > useful for both cases to report "expected %d, got %d" rather than just one > or the other. > > > By the way, I don't think that changes to error messages will be > back-ported to old versions like 3.2, but only added to the latest version. > > ---------- > keywords: +easy > nosy: +steven.daprano > > _______________________________________ > Python tracker <report@bugs.python.org> > <http://bugs.python.org/issue23949> > _______________________________________ > |
|
|
msg240998 - (view) |
Author: William Orr (worr) * |
Date: 2015-04-14 19:35 |
I've updated the error message to just include the expected number of arguments. I think this makes it way more readable. |
|
|
msg240999 - (view) |
Author: Arnon Yaari (wiggin15) * |
Date: 2015-04-14 19:41 |
Adding alternative patch for suggestion. |
|
|
msg241081 - (view) |
Author: Martin Panter (martin.panter) *  |
Date: 2015-04-15 04:15 |
Either option would be an improvement. I think I prefer .diff by Arnon. I guess it would then look like this: >>> name,shares,price,date,value = data Traceback (most recent call last): File "", line 1, in ValueError: not enough values to unpack (expected 5, got 4) |
|
|
msg241105 - (view) |
Author: Skip Montanaro (skip.montanaro) *  |
Date: 2015-04-15 14:29 |
I like the idea of the "too many" and "not enough" messages being symmetric. If it's not too hard, I would prefer them to both identify the number of expected and received values. I don't have a recent checkout at hand though, so it's non-trivial for me to produce a patch. The best I can do easily is toss peanuts from the gallery. |
|
|
msg241116 - (view) |
Author: Arnon Yaari (wiggin15) * |
Date: 2015-04-15 15:27 |
I couldn't find a way to add 'got %d' to the 'too many values' message. This would either require going over the rest of the iterator (which will take more time and may never return) or trying to figure out if it has a 'len' member. I didn't find any place in the current code that does something like this and writing one myself is too error prone (there are so many cases...). PyObject_Size returns an error if I try to use it for this purpose. I found another message with "need more than ..." that I improved in the same way. This message is for when trying to execute: >>> a, *(b, c, d) = 1, (2, 3), (4, 5) Now it outputs the message for the star unpacking: ValueError: not enough values to unpack (expected 3, got 2) which is confusing in this use case but still better than before. Oh my! |
|
|
msg241128 - (view) |
Author: Arnon Yaari (wiggin15) * |
Date: 2015-04-15 16:31 |
I fixed the tests (that's important!) and changed the latter message to specify it is referring to the starred target (the term "starred target" appears in the docs in simple_stmts.rst). Looks like the number in that message was wrong before! The test looked like this: >>> a, *b, c, d, e = 0, 1, 2 ValueError: need more than 3 values to unpack Need more than 3? Not exactly. The new message looks like this: >>> a, *b, c, d, e = 0, 1, 2 ValueError: not enough values to unpack into starred target (expected at least 4, got 3) |
|
|
msg241138 - (view) |
Author: Arnon Yaari (wiggin15) * |
Date: 2015-04-15 18:21 |
Sorry, I got confused (which proves the point of the ticket :)) * the "need more than 3 values" error message wasn't wrong, just confusing. * Including "into starred target" in the message was incorrect, I removed it. * The message with "expected at least ..." (when there is a star target) didn't show if there weren't enough values to fill the arguments before the star argument. I fixed this now too and added a test. |
|
|
msg241161 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2015-04-15 21:09 |
New changeset e55cc0834e9c by R David Murray in branch 'default': #23949: Improve tuple unpacking error messages. https://hg.python.org/cpython/rev/e55cc0834e9c |
|
|
msg324474 - (view) |
Author: Lorenz Mende (LorenzMende) * |
Date: 2018-09-02 08:24 |
This issue shall be closed, as the solution is already on master with 4171bbe687904582329c7977d571686953275b45. |
|
|