Skip to content

Commit 8f48ddb

Browse files
committed
Add trailing comma when expanded
1 parent c3dfaab commit 8f48ddb

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

Lib/pprint.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ def _pprint_list(self, object, stream, indent, allowance, context, level):
370370

371371
def _pprint_tuple(self, object, stream, indent, allowance, context, level):
372372
stream.write(self._format_block_start('(', indent))
373-
endchar = ',)' if len(object) == 1 else ')'
373+
if len(object) == 1 and not self._expand:
374+
endchar = ',)'
375+
else:
376+
endchar = ')'
374377
self._format_items(object, stream, indent, allowance + len(endchar),
375378
context, level)
376379
stream.write(self._format_block_end(endchar, indent))
@@ -547,6 +550,8 @@ def _format_dict_items(self, items, stream, indent, allowance, context,
547550
)
548551
if not last:
549552
write(delimnl)
553+
elif self._expand:
554+
write(',')
550555

551556
def _format_namespace_items(self, items, stream, indent, allowance, context, level):
552557
write = stream.write
@@ -571,6 +576,8 @@ def _format_namespace_items(self, items, stream, indent, allowance, context, lev
571576
)
572577
if not last:
573578
write(delimnl)
579+
elif self._expand:
580+
write(',')
574581

575582
def _format_items(self, items, stream, indent, allowance, context, level):
576583
write = stream.write
@@ -611,6 +618,8 @@ def _format_items(self, items, stream, indent, allowance, context, level):
611618
self._format(ent, stream, indent,
612619
allowance if last else 1,
613620
context, level)
621+
if last and self._expand:
622+
write(',')
614623

615624
def _repr(self, object, context, level):
616625
repr, readable, recursive = self.format(object, context.copy(),
@@ -679,6 +688,8 @@ def _pprint_chain_map(self, object, stream, indent, allowance, context, level):
679688
for i, m in enumerate(object.maps):
680689
if i == len(object.maps) - 1:
681690
self._format(m, stream, indent, allowance + 1, context, level)
691+
if self._expand:
692+
stream.write(',')
682693
stream.write(self._format_block_end(')', indent - self._indent_per_level))
683694
else:
684695
self._format(m, stream, indent, 1, context, level)

Lib/test/test_pprint.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ class DummyDataclass:
15441544
qux={'baz': 123, 'foo': 'bar'},
15451545
quux=['foo', 'bar', 'baz'],
15461546
corge=7,
1547-
garply=(1, 2, 3, 4)
1547+
garply=(1, 2, 3, 4),
15481548
)""")
15491549

15501550
def test_expand_dict(self):
@@ -1563,7 +1563,7 @@ def test_expand_dict(self):
15631563
'baz': 123,
15641564
'qux': {'foo': 'bar', 'baz': 123},
15651565
'quux': ['foo', 'bar', 'baz'],
1566-
'corge': 7
1566+
'corge': 7,
15671567
}""")
15681568

15691569
def test_expand_ordered_dict(self):
@@ -1580,7 +1580,7 @@ def test_expand_ordered_dict(self):
15801580
OrderedDict([
15811581
('foo', 1),
15821582
('bar', 12),
1583-
('baz', 123)
1583+
('baz', 123),
15841584
])""")
15851585

15861586
def test_expand_list(self):
@@ -1597,7 +1597,7 @@ def test_expand_list(self):
15971597
'foo',
15981598
'bar',
15991599
'baz',
1600-
'qux'
1600+
'qux',
16011601
]""")
16021602

16031603
def test_expand_tuple(self):
@@ -1618,7 +1618,15 @@ def test_expand_tuple(self):
16181618
'baz',
16191619
4,
16201620
5,
1621-
6
1621+
6,
1622+
)""")
1623+
1624+
def test_expand_single_element_tuple(self):
1625+
self.assertEqual(
1626+
pprint.pformat((1,), width=1, indent=4, expand=True),
1627+
"""\
1628+
(
1629+
1,
16221630
)""")
16231631

16241632
def test_expand_set(self):
@@ -1637,7 +1645,7 @@ def test_expand_set(self):
16371645
'baz',
16381646
'foo',
16391647
'qux',
1640-
(1, 2, 3)
1648+
(1, 2, 3),
16411649
}""")
16421650

16431651
def test_expand_frozenset(self):
@@ -1661,7 +1669,7 @@ def test_expand_frozenset(self):
16611669
'bar',
16621670
'baz',
16631671
'foo',
1664-
(1, 2, 3)
1672+
(1, 2, 3),
16651673
})""")
16661674

16671675
def test_expand_frozendict(self):
@@ -1674,7 +1682,7 @@ def test_expand_frozendict(self):
16741682
frozendict({
16751683
'baz': 123,
16761684
'foo': 'bar',
1677-
'qux': [1, 2]
1685+
'qux': [1, 2],
16781686
})""",
16791687
)
16801688

@@ -1717,7 +1725,7 @@ def test_expand_mappingproxy(self):
17171725
'corge': 7,
17181726
'foo': 'bar',
17191727
'quux': ['foo', 'bar', 'baz'],
1720-
'qux': {'baz': 123, 'foo': 'bar'}
1728+
'qux': {'baz': 123, 'foo': 'bar'},
17211729
})""")
17221730

17231731
def test_expand_namespace(self):
@@ -1740,8 +1748,8 @@ def test_expand_namespace(self):
17401748
baz=namespace(
17411749
x=321,
17421750
y='string',
1743-
d={'bar': 'baz', 'foo': True}
1744-
)
1751+
d={'bar': 'baz', 'foo': True},
1752+
),
17451753
)""")
17461754

17471755
def test_expand_defaultdict(self):
@@ -1755,7 +1763,7 @@ def test_expand_defaultdict(self):
17551763
"""\
17561764
defaultdict(<class 'list'>, {
17571765
'bar': {'baz': None, 'foo': 'bar'},
1758-
'foo': ['bar', 'baz', 'qux']
1766+
'foo': ['bar', 'baz', 'qux'],
17591767
})""")
17601768

17611769
def test_expand_counter(self):
@@ -1766,7 +1774,7 @@ def test_expand_counter(self):
17661774
'b': 4,
17671775
'c': 3,
17681776
'd': 2,
1769-
'e': 1
1777+
'e': 1,
17701778
})"""
17711779
self.assertEqual(pprint.pformat(dummy_counter, width=40, indent=4,
17721780
expand=True), expected)
@@ -1777,7 +1785,7 @@ def test_expand_counter(self):
17771785
'b': 4,
17781786
'c': 3,
17791787
'd': 2,
1780-
'e': 1
1788+
'e': 1,
17811789
})"""
17821790
self.assertEqual(pprint.pformat(dummy_counter, width=20, indent=2,
17831791
expand=True), expected2)
@@ -1810,11 +1818,11 @@ def test_expand_chainmap(self):
18101818
'quux': ['foo', 'bar', 'baz'],
18111819
'qux': {
18121820
'baz': 123,
1813-
'foo': 'bar'
1814-
}
1815-
}
1821+
'foo': 'bar',
1822+
},
1823+
},
18161824
},
1817-
{'garply': 'waldo'}
1825+
{'garply': 'waldo'},
18181826
)""")
18191827

18201828
def test_expand_deque(self):
@@ -1851,11 +1859,11 @@ def test_expand_deque(self):
18511859
'corge': 7,
18521860
'foo': 'bar',
18531861
'quux': ['foo', 'bar', 'baz'],
1854-
'qux': {'baz': 123, 'foo': 'bar'}
1862+
'qux': {'baz': 123, 'foo': 'bar'},
18551863
},
18561864
'foo',
18571865
'bar',
1858-
'baz'
1866+
'baz',
18591867
], maxlen=10)""")
18601868

18611869
def test_expand_userdict(self):
@@ -1879,7 +1887,7 @@ def __init__(self, *args, **kwargs):
18791887
'corge': 7,
18801888
'foo': 'bar',
18811889
'quux': ['foo', 'bar', 'baz'],
1882-
'qux': {'baz': 123, 'foo': 'bar'}
1890+
'qux': {'baz': 123, 'foo': 'bar'},
18831891
}""")
18841892

18851893
def test_expand_userlist(self):
@@ -1899,10 +1907,9 @@ def __init__(self, *args, **kwargs):
18991907
'first',
19001908
2,
19011909
{'key': 'value'},
1902-
[4, 5, 6]
1910+
[4, 5, 6],
19031911
]""")
19041912

1905-
19061913
def test_expand_dict_keys(self):
19071914
d = {"foo": 1, "bar": 2, "baz": 3, "qux": 4, "quux": 5}
19081915
self.assertEqual(
@@ -1913,7 +1920,7 @@ def test_expand_dict_keys(self):
19131920
'baz',
19141921
'foo',
19151922
'quux',
1916-
'qux'
1923+
'qux',
19171924
])""",
19181925
)
19191926

@@ -1927,7 +1934,7 @@ def test_expand_dict_values(self):
19271934
2,
19281935
3,
19291936
4,
1930-
5
1937+
5,
19311938
])""",
19321939
)
19331940

@@ -1941,7 +1948,7 @@ def test_expand_dict_items(self):
19411948
('baz', 3),
19421949
('foo', 1),
19431950
('quux', 5),
1944-
('qux', 4)
1951+
('qux', 4),
19451952
])""",
19461953
)
19471954

0 commit comments

Comments
 (0)