-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent Unpacking Value Error Messages: Too Many Values to Unpack (Expected X) #128660
Comments
We recently improved those message sin #122244. Can you check with Python 3.14 to see if those new messages are now better? |
Concerning the exact failures:
Actually, it's correct. The are too many values to unpack (1,2, 3) but we only have two destination variables. What you unpack is the tuple.
Same here. keys() returns a string which you can unpack and behaves as a sequence (so you need as many destination variables as the length of string to have no error). |
Thank you, Bénédikt Tran. As per your feedback, I checked the code with 3.14. I checked with both 3.14.0.a3 (before 3.14.0.0a4 was released) and 3.14.0.a4 versions of Python. The messages have been improved. Please see the attached image for my observations. I think, in case an attempt is made to unpack a single value into multiple variables, the message can be improved to "expected X, got 1." |
Concerning this error message, it is actually correct. A string is iterable and thus can be unpacked. for i, j in counter.keys(): ... is equivalent to
And |
Considering the weird case with strings is actually working as expected and that error messages were improved in 3.14, I'll close this one as |
Feature or enhancement
Proposal:
Inconsistent Unpacking Error Messages: Too Many Values to Unpack (Expected X)
Description:
When attempting to unpack values from a sequence (e.g., dictionary keys or a tuple), Python sometimes raises a ValueError that is misleading and could be clarified. The current error messages are inconsistent, which can cause confusion for users. Specifically, the error message "too many values to unpack (expected X)" is raised in two different unpacking situations, and this wording is not intuitive.
Problem:
Python raises the error message "too many values to unpack (expected X)" in cases where:
The problem is that the error message "too many values to unpack" implies that there are more values than variables, but it's actually the reverse in some cases. This can be confusing because the error message doesn't explicitly describe the issue—whether it's too many variables or not enough values.
Examples:
Unpacking a Dictionary's Keys:
Expected Behavior:
ValueError: too many values to unpack (expected 2)
Explanation of Issue:
i
andj
) are expected.Unpacking a Tuple with More Values than Variables:
Expected Behavior:
ValueError: too many values to unpack (expected 2)
Explanation of Issue:
Suggested Fix:
Improve the Error Message for Case 1:
Clarify the Error Message for Case 2:
Additional Notes:
Steps to Reproduce:
Environment:
Disclosure:
I have taken help from OpenAI's ChatGPT to intuitively understand Python error messages and in making this report while going through
_Brett Slatkin's Effective Python (2nd edition)_
book. The dictionarycounters
is from the book, thefor
loop is my own andmy_tuple
as well as the report content (review and some modifications by me) are from ChatGPT. Python's error messages could be improved for better clarity, and a review to make them more intuitive would be beneficial.Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: