This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Number of elements display in error message is wrong while unpacking in traceback
Type: enhancement Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: LorenzMende, martin.panter, python-dev, skip.montanaro, steven.daprano, ulaganathanm123@gmail.com, wiggin15, worr
Priority: normal Keywords: easy, patch

Created on 2015-04-14 16:28 by ulaganathanm123@gmail.com, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
maxvalueerror.patch worr, 2015-04-14 19:35 review
issue23949.diff wiggin15, 2015-04-15 15:27
issue23949-1.diff wiggin15, 2015-04-15 16:31
issue23949-2.diff wiggin15, 2015-04-15 16:37
issue23949-3.diff wiggin15, 2015-04-15 16:38
issue23949-4.patch wiggin15, 2015-04-15 18:21
Messages (13)
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) * (Python triager) 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) * (Python committer) 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) * (Python committer) Date: 2015-04-15 04:15
Either option would be an improvement. I think I prefer issue23949.diff by Arnon. I guess it would then look like this:

>>> name,shares,price,date,value = data
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 5, got 4)
msg241105 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) 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) (Python triager) 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.
History
Date User Action Args
2022-04-11 14:58:15adminsetgithub: 68137
2018-09-02 09:09:42serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: resolved
2018-09-02 08:24:17LorenzMendesetnosy: + LorenzMende
messages: + msg324474
2015-04-15 21:09:13python-devsetnosy: + python-dev
messages: + msg241161
2015-04-15 18:21:03wiggin15setfiles: + issue23949-4.patch

messages: + msg241138
2015-04-15 16:38:36wiggin15setfiles: + issue23949-3.diff
2015-04-15 16:37:16wiggin15setfiles: + issue23949-2.diff
2015-04-15 16:31:38wiggin15setfiles: + issue23949-1.diff

messages: + msg241128
2015-04-15 15:27:14wiggin15setfiles: + issue23949.diff

messages: + msg241116
2015-04-15 15:26:12wiggin15setfiles: - issue23949.diff
2015-04-15 14:29:59skip.montanarosetmessages: + msg241105
2015-04-15 04:15:13martin.pantersetnosy: + martin.panter
messages: + msg241081
2015-04-14 19:41:53wiggin15setfiles: + issue23949.diff
versions: + Python 3.5, - Python 3.2
nosy: + wiggin15

messages: + msg240999
2015-04-14 19:35:34worrsetfiles: + maxvalueerror.patch

nosy: + worr
messages: + msg240998

keywords: + patch
2015-04-14 17:34:23ulaganathanm123@gmail.comsetmessages: + msg240962
2015-04-14 16:58:09steven.dapranosetkeywords: + easy
nosy: + steven.daprano
messages: + msg240952

2015-04-14 16:56:13skip.montanarosetnosy: + skip.montanaro
messages: + msg240950
2015-04-14 16:28:57ulaganathanm123@gmail.comcreate