1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
From 04e53d9b614c30e1a42d9d037a56bc5b7d5ab6a0 Mon Sep 17 00:00:00 2001
From: Mahmoud Hashemi <mahmoud@hatnote.com>
Date: Mon, 10 Dec 2018 21:36:02 -0800
Subject: [PATCH] drop dict(omd) documented use case (with note added to
docstring) due to py3.7 dict changes
---
boltons/dictutils.py | 25 ++++++++++++++++---------
tests/test_dictutils.py | 3 ++-
2 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/boltons/dictutils.py b/boltons/dictutils.py
index f3583f7..c38fc26 100644
--- a/boltons/dictutils.py
+++ b/boltons/dictutils.py
@@ -106,17 +106,11 @@ class OrderedMultiDict(dict):
>>> omd
OrderedMultiDict([('b', 2)])
- Note that calling :func:`dict` on an OMD results in a dict of keys
- to *lists* of values:
+ If you want a safe-to-modify or flat dictionary, use
+ :meth:`OrderedMultiDict.todict()`.
- >>> from pprint import pprint as pp # ensuring proper key ordering
+ >>> from pprint import pprint as pp # preserve printed ordering
>>> omd = OrderedMultiDict([('a', 1), ('b', 2), ('a', 3)])
- >>> pp(dict(omd))
- {'a': [1, 3], 'b': [2]}
-
- Note that modifying those lists will modify the OMD. If you want a
- safe-to-modify or flat dictionary, use :meth:`OrderedMultiDict.todict()`.
-
>>> pp(omd.todict())
{'a': 3, 'b': 2}
>>> pp(omd.todict(multi=True))
@@ -129,6 +123,19 @@ class OrderedMultiDict(dict):
>>> OrderedMultiDict([('a', 1), ('b', 2), ('a', 3)]).items(multi=False)
[('a', 3), ('b', 2)]
+ .. warning::
+
+ ``dict(omd)`` changed behavior `in Python 3.7
+ <https://bugs.python.org/issue34320>`_ due to changes made to
+ support the transition from :class:`collections.OrderedDict` to
+ the built-in dictionary being ordered. Before 3.7, the result
+ would be a new dictionary, with values that were lists, similar
+ to ``omd.todict(multi=True)`` (but only shallow-copy; the lists
+ were direct references to OMD internal structures). From 3.7
+ onward, the values became singular, like
+ ``omd.todict(multi=False)``. For reliable cross-version
+ behavior, just use :meth:`~OrderedMultiDict.todict()`.
+
"""
def __init__(self, *args, **kwargs):
if len(args) > 1:
diff --git a/tests/test_dictutils.py b/tests/test_dictutils.py
index 87c6317..01c9ab5 100644
--- a/tests/test_dictutils.py
+++ b/tests/test_dictutils.py
@@ -27,7 +27,7 @@ def test_todict():
assert len(omd) == 1
assert omd['A'] == 'One'
- d = dict(omd)
+ d = omd.todict(multi=True)
assert len(d) == 1
assert d['A'] == ['One', 'One', 'One']
@@ -40,6 +40,7 @@ def test_todict():
flat = omd.todict()
assert flat == d
+ return
def test_eq():
|