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: passing a dict to bytes() gives unhelpful error message
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: eric.smith, marnanel, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-10-10 19:55 by marnanel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg327490 - (view) Author: Marnanel Thurman (marnanel) Date: 2018-10-10 19:55
bytes() doesn't accept a dict as parameter. If you attempt to pass one, you receive a TypeError with the baffling message "'str' object cannot be interpreted as an integer".

 >> bytes({'a':1})
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 TypeError: 'str' object cannot be interpreted as an integer
msg327492 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-10 20:55
bytes() accepts:

1. An integer.
2. An object supporting the buffer protocol.
3. An iterable of integers in the range 0 to 255.

Dict is an iterable. But iterating it produces string object which cannot be interpreted as an integer.
msg327501 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-10-10 22:22
You can in fact pass a dict to bytes(), as long as the keys are ints in the correct range:

>>> bytes({0:10, 1:20})
b'\x00\x01'

I'm not claiming it's very useful, but it does conform to the docs. I'm not sure the error message can be improved, so I suggest closing this issue.
msg327514 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-10-11 04:35
>  I'm not sure the error message can be improved, so I suggest closing this issue.

I concur.
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79136
2018-10-11 05:49:25rhettingersetstatus: open -> closed
stage: resolved
2018-10-11 04:35:20rhettingersetnosy: + rhettinger
messages: + msg327514

assignee: rhettinger
resolution: not a bug
2018-10-10 22:22:11eric.smithsetnosy: + eric.smith
messages: + msg327501
2018-10-10 20:55:14serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg327492
2018-10-10 19:55:15marnanelcreate