diff options
author | Tushar Sadhwani <tushar.sadhwani000@gmail.com> | 2024-09-10 15:07:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-10 15:07:30 (GMT) |
commit | 3597642ed57d184511ca2dbd1a382ffe8e280ac4 (patch) | |
tree | ef753d92fbd15a9e79f3891ac025c4be690b9a68 /Lib | |
parent | 07f0bf5aa4ca34e692c16e14129d79c161ee206f (diff) | |
download | cpython-3597642ed57d184511ca2dbd1a382ffe8e280ac4.zip cpython-3597642ed57d184511ca2dbd1a382ffe8e280ac4.tar.gz cpython-3597642ed57d184511ca2dbd1a382ffe8e280ac4.tar.bz2 |
gh-122239: Add actual count in unbalanced unpacking error message when possible (#122244)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_unpack.py | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/Lib/test/test_unpack.py b/Lib/test/test_unpack.py index 515ec12..adb30c0 100644 --- a/Lib/test/test_unpack.py +++ b/Lib/test/test_unpack.py @@ -18,6 +18,13 @@ Unpack list >>> a == 4 and b == 5 and c == 6 True +Unpack dict + + >>> d = {4: 'four', 5: 'five', 6: 'six'} + >>> a, b, c = d + >>> a == 4 and b == 5 and c == 6 + True + Unpack implied tuple >>> a, b, c = 7, 8, 9 @@ -66,14 +73,14 @@ Unpacking tuple of wrong size >>> a, b = t Traceback (most recent call last): ... - ValueError: too many values to unpack (expected 2) + ValueError: too many values to unpack (expected 2, got 3) Unpacking tuple of wrong size >>> a, b = l Traceback (most recent call last): ... - ValueError: too many values to unpack (expected 2) + ValueError: too many values to unpack (expected 2, got 3) Unpacking sequence too short @@ -140,8 +147,52 @@ Unpacking to an empty iterable should raise ValueError >>> () = [42] Traceback (most recent call last): ... - ValueError: too many values to unpack (expected 0) + ValueError: too many values to unpack (expected 0, got 1) + +Unpacking a larger iterable should raise ValuleError, but it +should not entirely consume the iterable + >>> it = iter(range(100)) + >>> x, y, z = it + Traceback (most recent call last): + ... + ValueError: too many values to unpack (expected 3) + >>> next(it) + 4 + +Unpacking unbalanced dict + + >>> d = {4: 'four', 5: 'five', 6: 'six', 7: 'seven'} + >>> a, b, c = d + Traceback (most recent call last): + ... + ValueError: too many values to unpack (expected 3, got 4) + +Ensure that custom `__len__()` is NOT called when showing the error message + + >>> class LengthTooLong: + ... def __len__(self): + ... return 5 + ... def __getitem__(self, i): + ... return i*2 + ... + >>> x, y, z = LengthTooLong() + Traceback (most recent call last): + ... + ValueError: too many values to unpack (expected 3) + +For evil cases like these as well, no actual count to be shown + + >>> class BadLength: + ... def __len__(self): + ... return 1 + ... def __getitem__(self, i): + ... return i*2 + ... + >>> x, y, z = BadLength() + Traceback (most recent call last): + ... + ValueError: too many values to unpack (expected 3) """ __test__ = {'doctests' : doctests} |