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: dict() allows keyword expansion with integer keys, e.g. dict(**{5:'v'})
Type: behavior Stage: needs patch
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Cyphase, eric.smith, r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2014-04-21 11:54 by Cyphase, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg216938 - (view) Author: (Cyphase) Date: 2014-04-21 11:54
Python 2.7.6: Affected
Python 3.2.3: Not affected

dict() allows keyword expansion using a dict() with integer keys, whereas attempting to do so with most other functions raises a TypeError with the message, "keywords must be strings". The same thing happens with collections.defaultdict(), but not collections.Counter() or collections.OrderedDict, presumably because they override dict.__init__().

>>> old_dict = {'a': 1, 2: 'b'}
>>> old_dict
{'a': 1, 2: 'b'}
>>> other_dict = {'c': 3, 4: 'd'}
>>> other_dict
>>> new_dict = dict(old_dict, **other_dict)
>>> new_dict
{'a': 1, 2: 'b', 4: 'd', 'c': 3}

I feel like this must be known, but I didn't find anything with a search.
msg216946 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-04-21 14:49
2.7 can't be changed for backward compatibility reasons, and python3 enforces the restriction in dict, as you observe.

I don't know if a python2 documentation note is worthwhile, but given the conversations at pycon about adding additional -3 warnings to 2.7, it seems like this is a place such a warning would be justified.
msg216993 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-04-22 07:50
IIRC, we've only created -3 warnings for things that would be incorrectly accepted by Python 3 (such as floor division using /).   

I don't think that applies here and isn't worth monkeying with Py2.7.  It goes in the harmless nuisance category, something that affects almost no one.
msg217269 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-04-27 15:01
I agree with Raymond: this isn't a practical problem worth solving. If it's causing an actual problem, please re-open this issue and give a use case.

Thanks.
msg217564 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-04-30 01:17
I'm not going to reopen it, but my point was that if we really are going to add additional -3 warnings to help people port 2.7, as discussed at the language summit, then this would be a candidate.
History
Date User Action Args
2022-04-11 14:58:02adminsetgithub: 65519
2014-04-30 01:17:19r.david.murraysetmessages: + msg217564
2014-04-27 15:01:23eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg217269

resolution: wont fix
2014-04-22 07:50:23rhettingersetnosy: + rhettinger
messages: + msg216993
2014-04-21 14:49:34r.david.murraysetnosy: + r.david.murray

messages: + msg216946
stage: needs patch
2014-04-21 11:54:59Cyphasecreate