Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-134551: Add t-strings support to pprint#134577

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

Open
loic-simon wants to merge5 commits intopython:main
base:main
Choose a base branch
Loading
fromloic-simon:add-templates-to-pprint
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletionsLib/pprint.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -609,6 +609,29 @@ def _pprint_user_string(self, object, stream, indent, allowance, context, level)

_dispatch[_collections.UserString.__repr__] = _pprint_user_string

def _pprint_template(self, object, stream, indent, allowance, context, level):
cls_name = object.__class__.__name__
indent += len(cls_name) + 1
items = (("strings", object.strings),
("interpolations", object.interpolations))
stream.write(cls_name + '(')
self._format_namespace_items(items, stream, indent, allowance, context, level)
stream.write(')')

def _pprint_interpolation(self, object, stream, indent, allowance, context, level):
cls_name = object.__class__.__name__
indent += len(cls_name)
items = (object.value, object.expression,
object.conversion, object.format_spec)
stream.write(cls_name + '(')
self._format_items(items, stream, indent, allowance, context, level)
stream.write(')')

t = t"{0}"
_dispatch[type(t).__repr__] = _pprint_template
_dispatch[type(t.interpolations[0]).__repr__] = _pprint_interpolation
del t

def _safe_repr(self, object, context, maxlevels, level):
# Return triple (repr_string, isreadable, isrecursive).
typ = type(object)
Expand Down
1 change: 1 addition & 0 deletionsLib/test/.ruff.toml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ extend-exclude = [
"encoded_modules/module_koi8_r.py",
# SyntaxError because of t-strings
"test_annotationlib.py",
"test_pprint.py",
"test_string/test_templatelib.py",
"test_tstring.py",
# New grammar constructions may not yet be recognized by Ruff,
Expand Down
32 changes: 32 additions & 0 deletionsLib/test/test_pprint.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1472,6 +1472,38 @@ def test_user_string(self):
'jumped over a '
'lazy dog'}""")

def test_template(self):
d = t""
self.assertEqual(pprint.pformat(d),
"Template(strings=('',), interpolations=())")
self.assertEqual(pprint.pformat(d), repr(d))
self.assertEqual(pprint.pformat(d, width=1),
"""\
Template(strings=('',),
interpolations=())""")
name = "World"
d = t"Hello {name}"
self.assertEqual(pprint.pformat(d),
"""\
Template(strings=('Hello ', ''),
interpolations=(Interpolation('World', 'name', None, ''),))""")
ver = {3.13: False, 3.14: True}
d = t"Hello { {"name": "Python", "version": ver}!s:z}!"
self.assertEqual(pprint.pformat(d, width=1),
"""\
Template(strings=('Hello ',
'!'),
interpolations=(Interpolation({'name': 'Python',
'version': {3.13: False,
3.14: True}},
' '
'{"name": '
'"Python", '
'"version": '
'ver}',
's',
'z'),))""")


class DottedPrettyPrinter(pprint.PrettyPrinter):

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Add t-strings support to pprint functions
Loading

[8]ページ先頭

©2009-2025 Movatter.jp