Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-90117: handle dict and mapping views in pprint#30135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
f7835ca
1eda2d5
9500a74
653cdea
884f224
48be1e9
9745491
7edb29d
7d2fa0f
32773ce
15dd266
6898bc4
67e3f14
65e0be2
c91af09
5b2341c
a9ff3df
4c67166
bfa9868
148b469
34abce7
09839a6
091c3bb
29655a0
7ecb456
646816a
7537a52
7a6769d
7dcb9cf
06babe4
58692d2
8e605c1
7e27e02
eb66934
1dc4ab2
f267452
3867338
1a609d0
bca6f6f
d020f86
ee7b521
7009887
9499115
692c0cd
90e069c
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -233,6 +233,34 @@ def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level | ||
_dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict | ||
def _pprint_dict_view(self, object, stream, indent, allowance, context, level, items=False): | ||
key = _safe_tuple if items else _safe_key | ||
write = stream.write | ||
write(object.__class__.__name__ + '([') | ||
if self._indent_per_level > 1: | ||
write((self._indent_per_level - 1) * ' ') | ||
length = len(object) | ||
if length: | ||
if self._sort_dicts: | ||
entries = sorted(object, key=key) | ||
else: | ||
entries = object | ||
self._format_items(entries, stream, indent, allowance + 1, | ||
context, level) | ||
merwok marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
write('])') | ||
dict_keys_view = type({}.keys()) | ||
devdanzin marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
_dispatch[dict_keys_view.__repr__] = _pprint_dict_view | ||
dict_values_view = type({}.values()) | ||
devdanzin marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
_dispatch[dict_values_view.__repr__] = _pprint_dict_view | ||
def _pprint_dict_items_view(self, object, stream, indent, allowance, context, level): | ||
self._pprint_dict_view(object, stream, indent, allowance, context, level, True) | ||
merwok marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
dict_items_view = type({}.items()) | ||
_dispatch[dict_items_view.__repr__] = _pprint_dict_items_view | ||
def _pprint_list(self, object, stream, indent, allowance, context, level): | ||
stream.write('[') | ||
self._format_items(object, stream, indent, allowance + 1, | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -231,6 +231,7 @@ def test_same_as_repr(self): | ||
set(), set2(), set3(), | ||
frozenset(), frozenset2(), frozenset3(), | ||
{}, dict2(), dict3(), | ||
{}.keys(), {}.values(), {}.items(), | ||
self.assertTrue, pprint, | ||
-6, -6, -6-6j, -1.5, "x", b"x", bytearray(b"x"), | ||
(3,), [3], {3: 6}, | ||
@@ -240,6 +241,7 @@ def test_same_as_repr(self): | ||
set({7}), set2({7}), set3({7}), | ||
frozenset({8}), frozenset2({8}), frozenset3({8}), | ||
dict2({5: 6}), dict3({5: 6}), | ||
{5: 6}.keys(), {5: 6}.values(), {5: 6}.items(), | ||
range(10, -11, -1), | ||
True, False, None, ..., | ||
): | ||
@@ -296,6 +298,36 @@ def test_basic_line_wrap(self): | ||
for type in [dict, dict2]: | ||
self.assertEqual(pprint.pformat(type(o)), exp) | ||
o = range(100) | ||
exp = 'dict_keys([%s])' % ',\n '.join(map(str, o)) | ||
keys = dict.fromkeys(o).keys() | ||
self.assertEqual(pprint.pformat(keys), exp) | ||
o = range(100) | ||
exp = 'dict_values([%s])' % ',\n '.join(map(str, o)) | ||
values = {v: v for v in o}.values() | ||
self.assertEqual(pprint.pformat(values), exp) | ||
o = range(100) | ||
exp = 'dict_items([%s])' % ',\n '.join("(%s, %s)" % (i, i) for i in o) | ||
items = {v: v for v in o}.items() | ||
self.assertEqual(pprint.pformat(items), exp) | ||
o = range(100) | ||
exp = 'odict_keys([%s])' % ',\n '.join(map(str, o)) | ||
keys = collections.OrderedDict.fromkeys(o).keys() | ||
self.assertEqual(pprint.pformat(keys), exp) | ||
o = range(100) | ||
exp = 'odict_values([%s])' % ',\n '.join(map(str, o)) | ||
values = collections.OrderedDict({v: v for v in o}).values() | ||
self.assertEqual(pprint.pformat(values), exp) | ||
o = range(100) | ||
exp = 'odict_items([%s])' % ',\n '.join("(%s, %s)" % (i, i) for i in o) | ||
items = collections.OrderedDict({v: v for v in o}).items() | ||
self.assertEqual(pprint.pformat(items), exp) | ||
o = range(100) | ||
exp = '[%s]' % ',\n '.join(map(str, o)) | ||
for type in [list, list2]: | ||
@@ -447,6 +479,28 @@ def test_mapping_proxy(self): | ||
('lazy', 7), | ||
('dog', 8)]))""") | ||
def test_dict_views(self): | ||
for dict_class in (dict, collections.OrderedDict): | ||
empty = dict_class() | ||
short = dict_class(zip('abcde', 'abcde')) | ||
long = dict_class((chr(x), chr(x)) for x in range(65, 91)) | ||
prefix = "dict" if dict_class is dict else "odict" | ||
for d in empty, short, long: | ||
merwok marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
is_short = len(d) < 6 | ||
joiner = ", " if is_short else ",\n " | ||
k = d.keys() | ||
v = d.values() | ||
i = d.items() | ||
self.assertEqual(pprint.pformat(k, sort_dicts=True), | ||
prefix + "_keys([%s])" % | ||
joiner.join(repr(key) for key in sorted(k))) | ||
self.assertEqual(pprint.pformat(v, sort_dicts=True), | ||
prefix + "_values([%s])" % | ||
joiner.join(repr(val) for val in sorted(v))) | ||
self.assertEqual(pprint.pformat(i, sort_dicts=True), | ||
prefix + "_items([%s])" % | ||
joiner.join(repr(item) for item in sorted(i))) | ||
merwok marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def test_empty_simple_namespace(self): | ||
ns = types.SimpleNamespace() | ||
formatted = pprint.pformat(ns) | ||
@@ -860,6 +914,10 @@ def test_sort_unorderable_values(self): | ||
'frozenset({' + ','.join(map(repr, skeys)) + '})') | ||
self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))), | ||
'{' + ','.join('%r:None' % k for k in skeys) + '}') | ||
self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys).keys())), | ||
'dict_keys([' + ','.join('%r' % k for k in skeys) + '])') | ||
self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys).items())), | ||
'dict_items([' + ','.join('(%r,None)' % k for k in skeys) + '])') | ||
# Issue 10017: TypeError on user-defined types as dict keys. | ||
self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}), | ||