Skip to content

Commit 74a4f8c

Browse files
loic-simonhugovkblurb-it[bot]
authored
gh-134551: Add t-strings support to pprint (#134577)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 5ce0fe8 commit 74a4f8c

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,11 @@ pprint
15511551
:func:`pprint.pformat`, :func:`pprint.pp`. If true, the output will be
15521552
formatted similar to pretty-printed :func:`json.dumps` when
15531553
*indent* is supplied.
1554-
(Contributed by Stefan Todoran and Semyon Moroz in :gh:`112632`.)
1554+
(Contributed by Stefan Todoran, Semyon Moroz and Hugo van Kemenade in
1555+
:gh:`112632`.)
1556+
1557+
* Add t-string support to :mod:`pprint`.
1558+
(Contributed by Loïc Simon and Hugo van Kemenade in :gh:`134551`.)
15551559

15561560

15571561
sre_*

Lib/pprint.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,61 @@ def _pprint_user_string(self, object, stream, indent, allowance, context, level)
735735

736736
_dispatch[_collections.UserString.__repr__] = _pprint_user_string
737737

738+
def _pprint_template(self, object, stream, indent, allowance, context, level):
739+
cls_name = object.__class__.__name__
740+
if self._expand:
741+
indent += self._indent_per_level
742+
else:
743+
indent += len(cls_name) + 1
744+
745+
items = (
746+
("strings", object.strings),
747+
("interpolations", object.interpolations),
748+
)
749+
stream.write(self._format_block_start(cls_name + "(", indent))
750+
self._format_namespace_items(
751+
items, stream, indent, allowance, context, level
752+
)
753+
stream.write(
754+
self._format_block_end(")", indent - self._indent_per_level)
755+
)
756+
757+
def _pprint_interpolation(self, object, stream, indent, allowance, context, level):
758+
cls_name = object.__class__.__name__
759+
if self._expand:
760+
indent += self._indent_per_level
761+
items = (
762+
("value", object.value),
763+
("expression", object.expression),
764+
("conversion", object.conversion),
765+
("format_spec", object.format_spec),
766+
)
767+
stream.write(self._format_block_start(cls_name + "(", indent))
768+
self._format_namespace_items(
769+
items, stream, indent, allowance, context, level
770+
)
771+
stream.write(
772+
self._format_block_end(")", indent - self._indent_per_level)
773+
)
774+
else:
775+
indent += len(cls_name)
776+
items = (
777+
object.value,
778+
object.expression,
779+
object.conversion,
780+
object.format_spec,
781+
)
782+
stream.write(cls_name + "(")
783+
self._format_items(
784+
items, stream, indent, allowance, context, level
785+
)
786+
stream.write(")")
787+
788+
t = t"{0}"
789+
_dispatch[type(t).__repr__] = _pprint_template
790+
_dispatch[type(t.interpolations[0]).__repr__] = _pprint_interpolation
791+
del t
792+
738793
def _safe_repr(self, object, context, maxlevels, level):
739794
# Return triple (repr_string, isreadable, isrecursive).
740795
typ = type(object)

Lib/test/test_pprint.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,86 @@ def test_user_string(self):
15151515
'jumped over a '
15161516
'lazy dog'}""")
15171517

1518+
def test_template(self):
1519+
d = t""
1520+
self.assertEqual(pprint.pformat(d),
1521+
"Template(strings=('',), interpolations=())")
1522+
self.assertEqual(pprint.pformat(d), repr(d))
1523+
self.assertEqual(pprint.pformat(d, width=1),
1524+
"""\
1525+
Template(strings=('',),
1526+
interpolations=())""")
1527+
name = "World"
1528+
d = t"Hello {name}"
1529+
self.assertEqual(pprint.pformat(d),
1530+
"""\
1531+
Template(strings=('Hello ', ''),
1532+
interpolations=(Interpolation('World', 'name', None, ''),))""")
1533+
ver = {3.13: False, 3.14: True}
1534+
d = t"Hello { {"name": "Python", "version": ver}!s:z}!"
1535+
self.assertEqual(pprint.pformat(d, width=1),
1536+
"""\
1537+
Template(strings=('Hello ',
1538+
'!'),
1539+
interpolations=(Interpolation({'name': 'Python',
1540+
'version': {3.13: False,
1541+
3.14: True}},
1542+
' '
1543+
'{"name": '
1544+
'"Python", '
1545+
'"version": '
1546+
'ver}',
1547+
's',
1548+
'z'),))""")
1549+
1550+
def test_expand_template(self):
1551+
d = t""
1552+
self.assertEqual(
1553+
pprint.pformat(d, expand=True),
1554+
"Template(strings=('',), interpolations=())",
1555+
)
1556+
name = "World"
1557+
d = t"Hello {name}"
1558+
self.assertEqual(
1559+
pprint.pformat(d, width=40, indent=4, expand=True),
1560+
"""\
1561+
Template(
1562+
strings=('Hello ', ''),
1563+
interpolations=(
1564+
Interpolation(
1565+
value='World',
1566+
expression='name',
1567+
conversion=None,
1568+
format_spec='',
1569+
),
1570+
),
1571+
)""",
1572+
)
1573+
ver = {3.13: False, 3.14: True}
1574+
d = t"Hello { {"name": "Python", "version": ver}!s:z}!"
1575+
self.assertEqual(
1576+
pprint.pformat(d, width=40, indent=4, expand=True),
1577+
"""\
1578+
Template(
1579+
strings=('Hello ', '!'),
1580+
interpolations=(
1581+
Interpolation(
1582+
value={
1583+
'name': 'Python',
1584+
'version': {
1585+
3.13: False,
1586+
3.14: True,
1587+
},
1588+
},
1589+
expression=' {"name": "Python", '
1590+
'"version": ver}',
1591+
conversion='s',
1592+
format_spec='z',
1593+
),
1594+
),
1595+
)""",
1596+
)
1597+
15181598
def test_expand_dataclass(self):
15191599
@dataclasses.dataclass
15201600
class DummyDataclass:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add t-strings support to pprint functions

0 commit comments

Comments
 (0)