I recently had a very subtle bug with an OrderedDict
in my Python code at work. I constructed the contents of this object from a SQL query that was output in a specific order (can you spot the bug?):
qs = models.MyModel.objects.all().order_by("-order")
data = OrderedDict({x.id: x.name for x in qs})
My expectation was output like the following, which I was seeing on my development system (Python 3.6):
OrderedDict([(4, 'Four'), (3, 'Three'), (2, 'Two'), (1, 'One')])
However, on my official sandbox test system (which we use for internal testing, running Python 3.5), I was seeing output like this:
OrderedDict([(1, 'One'), (2, 'Two'), (3, 'Three'), (4, 'Four')])
There are actually two issues in play here, and it took me a while to figure out what was going on.
- First, I'm constructing the
OrderedDict
element incorrectly. I'm using a dictionary comprehension as the initialization data for the object's constructor. Dictionaries are (until recently) not guaranteed to preserve insertion order when iterated over. This is where my order was being screwed up. - Second, the above behavior for dictionary order preservation is an implementation detail that changed in Python 3.6. As of 3.6 (in the CPython implementation), dictionaries now preserve the insertion order when iterated over. My development system, running on 3.6, was therefore outputting things as I expected them. The sandbox system, still running 3.5, did not. What an annoyance!
I've learned two valuable lessons here: (a) make sure you're running on the same levels of code in various places, and (b) don't initialize an OrderedDict
with a dictionary comprehension.