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: More descriptive error message than "too many values to unpack"
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, aeros, alexmojaki, malcomvx, nemeskeyd, serhiy.storchaka, xtreak
Priority: normal Keywords: patch

Created on 2020-03-01 19:35 by alexmojaki, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 19036 open ZackerySpytz, 2020-03-17 05:44
Messages (5)
msg363083 - (view) Author: Alex Hall (alexmojaki) Date: 2020-03-01 19:35
Based on the discussion in https://mail.python.org/archives/list/python-ideas@python.org/thread/C6QEAEEAELUHMLB23OBRSQK2UYU3AF5O/

When unpacking fails with an error such as:

ValueError: too many values to unpack (expected 2)

the name of the type of the unpacked object should be included, e.g.

ValueError: too many values to unpack (expected 2) from object of type 'str'

and if the type is exactly list or tuple, which are already special cased: https://github.com/python/cpython/blob/baf29b221682be0f4fde53a05ea3f57c3c79f431/Python/ceval.c#L2243-L2252

then the length can also be included:

ValueError: too many values to unpack (expected 2, got 3) from object of type 'tuple'
msg363093 - (view) Author: Kyle Stanley (aeros) * (Python committer) Date: 2020-03-01 20:32
+1, I've always found the "too many values to unpack" error messages be rather vague. Adding the type should make the message more clear, and easier to debug the error for users.

But, I think Chris Angelico raised a good point regarding the length (in the python-ideas thread):

> For your provided use-case, the length is almost irrelevant; the best
> way to spot the bug is to see "oh, it was trying to unpack a string,
> not a tuple". And that part can be done much more easily and safely.

So can we separate this into including the type, and including the length into two separate PRs? It can be part of the same issue, but I think including just the type for all of the unpacking error messages will be far easier to merge than merging the length one as well; considering that the former has significantly more added value.
msg363122 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-03-02 06:16
I concur with Chris and Kyle.
msg394295 - (view) Author: malcomvx (malcomvx) Date: 2021-05-25 06:01
Python functions can return multiple variables . These variables can be stored in variables directly. This is a unique property of Python , other programming languages such as C++ or Java do not support this by default.

The valueerror: too many values to unpack occurs during a multiple-assignment where you either don't have enough objects to assign to the variables or you have more objects to assign than variables. If for example myfunction() returned an iterable with three items instead of the expected two then you would have more objects than variables necessary to assign to.

http://net-informations.com/python/err/value.htm
msg400901 - (view) Author: Dávid Nemeskey (nemeskeyd) Date: 2021-09-02 08:29
+1. When starting out, I always got confused about this message ("too many values to unpack? Does that mean my function returned too many items, which could not be unpacked into the expected number? Oh no, the opposite...").

Also, please note that the stack trace usually shows the offending line (e.g. x, y, z = fn(...)), so it is easy to see how many values are _expected_. It would be even more helpful to (also) show how many we _got_.
History
Date User Action Args
2022-04-11 14:59:27adminsetgithub: 83997
2021-09-02 08:29:14nemeskeydsetnosy: + nemeskeyd
messages: + msg400901
2021-05-25 06:01:29malcomvxsetnosy: + malcomvx
messages: + msg394295
2020-10-03 19:12:51ammar2linkissue40202 superseder
2020-03-17 05:44:37ZackerySpytzsetkeywords: + patch
nosy: + ZackerySpytz

pull_requests: + pull_request18387
stage: patch review
2020-03-02 06:27:58xtreaksetnosy: + xtreak
2020-03-02 06:16:13serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg363122
2020-03-01 20:32:11aerossetnosy: + aeros
messages: + msg363093
2020-03-01 19:35:48alexmojakicreate