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: OrderedDict pseudo-literals (WIP)
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: llllllllll, rhettinger, serhiy.storchaka, skrah
Priority: normal Keywords: patch

Created on 2016-06-21 02:17 by llllllllll, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
odict_literals.patch llllllllll, 2016-06-21 02:17 review
Messages (9)
msg268954 - (view) Author: Joe Jevnik (llllllllll) * Date: 2016-06-21 02:17
This proposes the following syntax for creating OrderedDict literals:

OrderedDict[k1: v1, k2: v2, ...]

This is implemented by putting a metaclass on OrderedDict which has a getitem that turns the slices into a list of tuples (after validation).

The idea is taken from the library datashape where we have done this to implement pseudo literals for the Record type.

This is marked as  WIP because I am not quite sure why, but this patch is causing subclasses of OrderedDict to segfault in PyObject_SetItem.
msg268955 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-06-21 02:24
This needs to tabled pending the outcome of the python-dev discussion to make all dicts ordered dictionaries.

If it does get revived, there need to be clear motivating use cases.  In most of the use cases I've seen for ordered dicts, the key/value pairs are either generated by code or loaded from an external source.   The need for a dict literal is minimal at best (that is also why we don't have a literal for decimals).
msg268956 - (view) Author: Joe Jevnik (llllllllll) * Date: 2016-06-21 02:32
Assuming all dicts are ordered, there will be no need for this (as long as the parser preserves order) so I am okay with dropping this.

One of the main use cases I have for wanting nice literals is when I am working on tests. I find it very nice to be able to write out the expected results in a clear way. While the app code might not be using the literals, it improves the readability of the tests, which make it easier to see that the test is correct.
msg268960 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-06-21 03:44
BTW, there's no way this particular syntax suggestion is going in without a discussion on python-dev.  I think the square brackets will be a non-starter.
msg268962 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-06-21 03:52
One other thought is that the threshold for syntax changes to the language should be very high and should benefit the many, many who use Python, not just ourselves for something that might have been a little more convenient for a particular use.   We've never had a user request for this before, so it suggests that the need is minimal.

I'll leave this open for now, but think it was inappropriate for you to undo the status and resolution settings, especially after I referred you to an active discussion on python-dev that would make the question moot.
msg268963 - (view) Author: Joe Jevnik (llllllllll) * Date: 2016-06-21 03:59
Sorry about undoing the settings, that was unintentional. The UI around these settings is sort of unintuitive.

I was waiting to propose this on python-dev until I got it working, but I wanted to push up what I did have as a work in progress so I could try to get some help with the crash I am seeing. In the future I will upload the patch somewhere else until it is done.
msg268964 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-06-21 04:07
No worries, it just seemed a little brash ;-)

Go ahead and continue to post patch variants here.  Once it's working, going to python-dev is the right thing to do (to see whether it meets a threshold of need and to endlessly debate what the syntax would be).
msg268968 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-21 05:18
FYI the proposition about OrderedDict literals was raised on Python-Ideas or Python-Dev multiple times.

http://comments.gmane.org/gmane.comp.python.ideas/4888
http://comments.gmane.org/gmane.comp.python.ideas/6569
http://comments.gmane.org/gmane.comp.python.devel/121140
http://comments.gmane.org/gmane.comp.python.ideas/27218
http://comments.gmane.org/gmane.comp.python.ideas/33873
http://comments.gmane.org/gmane.comp.python.ideas/37439
msg305599 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2017-11-05 15:11
It looks like guaranteed ordered literals are going to happen (yay!):

   https://mail.python.org/pipermail/python-dev/2017-November/150144.html
History
Date User Action Args
2022-04-11 14:58:32adminsetgithub: 71546
2017-11-05 15:11:51skrahsetstatus: open -> closed

type: enhancement

nosy: + skrah
messages: + msg305599
resolution: later -> out of date
stage: resolved
2016-06-21 05:40:03rhettingersetassignee: rhettinger ->
2016-06-21 05:18:33serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg268968
2016-06-21 04:07:46rhettingersetmessages: + msg268964
2016-06-21 03:59:43llllllllllsetmessages: + msg268963
2016-06-21 03:52:26rhettingersetstatus: pending -> open

messages: + msg268962
components: + Interpreter Core, - Library (Lib)
2016-06-21 03:44:55rhettingersetstatus: open -> pending
assignee: rhettinger
resolution: later
messages: + msg268960
2016-06-21 02:32:27llllllllllsetstatus: closed -> open
resolution: later -> (no value)
messages: + msg268956
2016-06-21 02:24:30rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg268955

resolution: later
2016-06-21 02:17:07llllllllllcreate