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: Misleading grammatically of ValueError Message?
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: More descriptive error message than "too many values to unpack"
View: 39816
Assigned To: docs@python Nosy List: Jacob RR, ammar2, docs@python, iritkatriel, serhiy.storchaka, steven.daprano, veky
Priority: normal Keywords: patch

Created on 2020-04-06 08:16 by Jacob RR, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19446 closed ammar2, 2020-04-09 06:47
Messages (7)
msg365842 - (view) Author: Jacob RR (Jacob RR) Date: 2020-04-06 08:16
hi,

so I *think* that ValueError shows an error grammatically incorrect?
In python 2.7
>>> x = [1,2,3]
>>> f,x, a, b = [1,2,3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 3 values to unpack

Should have said: Received 3 values to unpack ?
The problem with that is the list size is 3 and the error says that I need more than 3 values to unpack which is logically wrong **IMHO** (don't kill me if im mistaken)

Now if I try to do something else, for example:

>>> x = [1,2,3]
>>> a, b = [1,2,3]

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

It says **too many** but I assign a few than the size of the list. am I the one who wrong here?

Now, I code in Python 3 I'm not a professional  like you, I'm novice and try to learn.. I'll get to the point, the same code in Python 3.7.6 (Anaconda, pip is disappoint me :< )

>>> a = [1,2,3]
>>> x,y = a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

Should said something else because it received less values and expected should say 3 and not 2, correct?


thanks for reading.




PS: Sorry I'm not a native speaker and I might be wrong and am very sorry if time wasted.
msg365844 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-04-06 09:51
I think the error messages could be improved.

In the first example: `f,x, a, b = [1,2,3]`

you are unpacking three values, but you need to unpack 4. The error message is not very helpful: 5 values is "more than 3" but it would be too many, you need not "more than 3" but exactly 4.

In the second example `a, b = [1,2,3]` it would be nice if it would tell you how many values you need to unpack.

Ideally, the message would say something like:

    Need to unpack 4 values but got 3  # first example
    Need to unpack 2 values but got 3  # second example

However I don't know if this is possible with the current parser, it might not be possible until the parser is changed (maybe in 3.9?)
msg366035 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2020-04-09 06:47
Jacob, let's skip the 2.7 part of the report since that is EOL now. For reference, the full error on the latest Python is:

>>> a, b, c, d = [1, 2, 3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 4, got 3)

>>> a, b = [1, 2, 3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)


The first example already behaves as Steven describes, it gives the expected and actual count. In the second example it's difficult to calculate the x for "expected 2, got x" in general. This is because the iterable being unpacked could be a generator. For example, consider:

>>> a, b = itertools.repeat(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

However, we could improve the message if the object being unpacked does expose a length. I've attached a PR that does this, it makes the error look like:

>>> a, b = [1, 2, 3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2, got 3)

Hopefully showing the amounts should help clarify why the error was thrown even if the wording seems a bit iffy.
msg366037 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-09 08:08
It was discussed in issue39816.

I do not think that calling len() and ignoring any exception is a good idea.

1. This may silence some exceptions (errors in the __len__ implementation, MemoryError, RecursionError, KeyboardInterrupt) which should not be silenced.

2. __len__() may have side effect.

3. __next__() may affects the result of __len__() (for example __next__() pops a value of the queue, and __len__() returns the size of the queue), so using the result of __len__() after calling __next__() may be misleading.

Since the original report was about 2.7 which is no longer maintained, I propose to close this issue as outdated.
msg366038 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-04-09 08:34
> Since the original report was about 2.7 which is no longer maintained, 
> I propose to close this issue as outdated.

For what it's worth, I'm okay with closing.
msg366085 - (view) Author: Vedran Čačić (veky) * Date: 2020-04-09 20:13
> It says **too many** but I assign a few than the size of the list. am I the one who wrong here?

Yes. The same as here:

> Should said something else because it received less values and expected should say 3 and not 2, correct?

You probably don't understand _values_. a,b and x,y on the left side are not values. They are names. The 1,2,3 on the right side are values. Those are counted.
msg377900 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-10-03 19:05
I think this can be closed as a duplicate of 39816.
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84383
2020-10-03 19:12:51ammar2setstatus: open -> closed
superseder: More descriptive error message than "too many values to unpack"
resolution: duplicate
stage: patch review -> resolved
2020-10-03 19:05:27iritkatrielsetnosy: + iritkatriel
messages: + msg377900
2020-04-09 20:13:05vekysetnosy: + veky
messages: + msg366085
2020-04-09 08:34:06steven.dapranosetmessages: + msg366038
2020-04-09 08:08:30serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg366037
2020-04-09 06:47:57ammar2setkeywords: + patch
stage: patch review
pull_requests: + pull_request18801
2020-04-09 06:47:47ammar2setnosy: + ammar2
messages: + msg366035
2020-04-06 09:51:57steven.dapranosetnosy: + steven.daprano
messages: + msg365844
2020-04-06 08:16:14Jacob RRcreate