Skip to content

Commit efa0814

Browse files
committed
reflow
1 parent 69d2b23 commit efa0814

File tree

1 file changed

+68
-38
lines changed

1 file changed

+68
-38
lines changed

peps/pep-0822.rst

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ Post-History: `05-Jan-2026 <https://discuss.python.org/t/105519>`__,
1212
Abstract
1313
========
1414

15-
This PEP proposes to add a feature that automatically removes indentation from multiline string literals.
15+
This PEP proposes to add a feature that automatically removes indentation from
16+
multiline string literals.
1617

17-
Dedented multiline strings use a new prefix "d" (shorthand for "dedent") before the opening quote of a multiline string literal.
18+
Dedented multiline strings use a new prefix "d" (shorthand for "dedent") before
19+
the opening quote of a multiline string literal.
1820

1921
Example (spaces are visualized as ``_``):
2022

@@ -42,15 +44,20 @@ When writing multiline string literals within deeply indented Python code,
4244
users are faced with the following choices:
4345

4446
* Accept that the content of the string literal will be left-aligned.
45-
* Use multiple single-line string literals concatenated together instead of a multiline string literal.
47+
* Use multiple single-line string literals concatenated together instead of
48+
a multiline string literal.
4649
* Use ``textwrap.dedent()`` to remove indentation.
4750

48-
All of these options have drawbacks in terms of code readability and maintainability.
51+
All of these options have drawbacks in terms of code readability and
52+
maintainability.
4953

5054
* Left-aligned multiline strings look awkward and tend to be avoided.
51-
In practice, many places including Python's own test code choose other methods.
52-
* Concatenated single-line string literals are more verbose and harder to maintain.
53-
* ``textwrap.dedent()`` is implemented in Python so it requires some runtime overhead.
55+
In practice, many places including Python's own test code choose other
56+
methods.
57+
* Concatenated single-line string literals are more verbose and harder to
58+
maintain.
59+
* ``textwrap.dedent()`` is implemented in Python so it requires some runtime
60+
overhead.
5461
It cannot be used in hot paths where performance is critical.
5562

5663
This PEP aims to provide a built-in syntax for dedented multiline strings that
@@ -76,10 +83,11 @@ However, this approach has several drawbacks:
7683
they cannot be dedented.
7784
* f-strings may interpolate expressions as multiline string without indent.
7885
In such case, f-string + ``str.dedent()`` cannot dedent the whole string.
79-
* t-strings do not create ``str`` objects, so they cannot use the ``str.dedent()`` method.
80-
While adding a ``dedent()`` method to ``string.templatelib.Template`` is an option,
81-
it would lead to inconsistency since t-strings and f-strings are very similar but
82-
would have different behaviors regarding dedentation.
86+
* t-strings do not create ``str`` objects, so they cannot use the
87+
``str.dedent()`` method.
88+
While adding a ``dedent()`` method to ``string.templatelib.Template`` is an
89+
option, it would lead to inconsistency since t-strings and f-strings are very
90+
similar but would have different behaviors regarding dedentation.
8391

8492
The ``str.dedent()`` method can still be useful for non-literal strings,
8593
so this PEP does not preclude that idea.
@@ -102,17 +110,21 @@ This newline is not included in the resulting string.
102110

103111
The amount of indentation to be removed is determined by the whitespace
104112
(``' '`` or ``'\t'``) preceding the closing triple quotes.
105-
Mixing spaces and tabs in indentation raises a ``TabError``, similar to Python's
106-
own indentation rules.
113+
Mixing spaces and tabs in indentation raises a ``TabError``, similar to
114+
Python's own indentation rules.
107115

108-
The dedentation process removes the determined amount of leading whitespace from every line in the string.
109-
Lines that are shorter than the determined indentation become just an empty line (e.g. ``"\n"``).
116+
The dedentation process removes the determined amount of leading whitespace
117+
from every line in the string.
118+
Lines that are shorter than the determined indentation become just an empty
119+
line (e.g. ``"\n"``).
110120
Otherwise, if the line does not start with the determined indentation,
111121
Python raises an ``IndentationError``.
112122

113-
Unless combined with the "r" prefix, backslash escapes are processed after removing indentation.
123+
Unless combined with the "r" prefix, backslash escapes are processed after
124+
removing indentation.
114125
So you cannot use ``\\t`` to create indentation.
115-
And you can use line continuation (backslash at the end of line) and remove indentation from the continued line.
126+
And you can use line continuation (backslash at the end of line) and remove
127+
indentation from the continued line.
116128

117129
Examples:
118130

@@ -199,37 +211,44 @@ Examples:
199211
print(type(s)) # <class 'string.templatelib.Template'>
200212
print(s.strings) # ('Hello, ', '!\n')
201213
print(s.values) # ('World',)
202-
print(s.interpolations) # (Interpolation('World', '"world".title()', None, ''),)
214+
print(s.interpolations)
215+
# (Interpolation('World', '"world".title()', None, ''),)
203216
204217
205218
How to Teach This
206219
=================
207220

208221
In the tutorial, we can introduce d-string with triple quote string literals.
209222
Additionally, we can add a note in the ``textwrap.dedent()`` documentation,
210-
providing a link to the d-string section in the language reference or the relevant part of the tutorial.
223+
providing a link to the d-string section in the language reference or
224+
the relevant part of the tutorial.
211225

212226

213227
Other Languages having Similar Features
214228
========================================
215229

216230
Java 15 introduced a feature called `text blocks <https://openjdk.org/jeps/378>`__.
217-
Since Java had not used triple qutes before, they introduced triple quotes for multiline string literals with automatic indent removal.
231+
Since Java had not used triple qutes before, they introduced triple quotes for
232+
multiline string literals with automatic indent removal.
218233

219-
C# 11 also introduced a similar feature called `raw string literals <https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-11.0/raw-string-literal>`__.
234+
C# 11 also introduced a similar feature called
235+
`raw string literals <https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-11.0/raw-string-literal>`__.
220236

221237
`Julia <https://docs.julialang.org/en/v1/manual/strings/#Triple-Quoted-String-Literals>`__ and
222238
`Swift <https://docs.swift.org/swift-book/documentation/the-swift-programming-language/stringsandcharacters/#Multiline-String-Literals>`__
223239
also support triple-quoted string literals that automatically remove indentation.
224240

225241
PHP 7.3 introduced `Flexible Heredoc and Nowdoc Syntaxes <https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes>`__
226-
Although it uses closing marker (e.g. ``<<<END ... END``) instead of triple quote,
227-
it removes indent from text too.
242+
Although it uses closing marker (e.g. ``<<<END ... END``) instead of
243+
triple quote, it removes indent from text too.
228244

229-
Java and Julia uses the least-indented line to determine the amount of indentation to be removed.
230-
Swift, C#, and PHP uses the indentation of the closing triple quotes or closing marker.
245+
Java and Julia uses the least-indented line to determine the amount of
246+
indentation to be removed.
247+
Swift, C#, and PHP uses the indentation of the closing triple quotes or
248+
closing marker.
231249

232-
This PEP chose the Swift and C# approach because it is simpler and easier to explain.
250+
This PEP chose the Swift and C# approach because it is simpler and easier to
251+
explain.
233252

234253

235254
Reference Implementation
@@ -245,8 +264,10 @@ Rejected Ideas
245264
``str.dedent()`` method
246265
-----------------------
247266

248-
As mentioned in the Rationale section, this PEP doesn't reject the idea of a ``str.dedent()`` method.
249-
A faster version of ``textwrap.dedent()`` implemented in C would be useful for runtime dedentation.
267+
As mentioned in the Rationale section, this PEP doesn't reject the idea of a
268+
``str.dedent()`` method.
269+
A faster version of ``textwrap.dedent()`` implemented in C would be useful for
270+
runtime dedentation.
250271

251272
However, d-string is more suitable for multiline string literals because:
252273

@@ -258,28 +279,37 @@ However, d-string is more suitable for multiline string literals because:
258279
Triple-backtick
259280
---------------
260281

261-
It is considered that `using triple backticks <https://discuss.python.org/t/40679>`__
282+
It is considered that
283+
`using triple backticks <https://discuss.python.org/t/40679>`__
262284
for dedented multiline strings could be an alternative syntax.
263-
This notation is familiar to us from Markdown. While there were past concerns about certain keyboard layouts,
285+
This notation is familiar to us from Markdown. While there were past concerns
286+
about certain keyboard layouts,
264287
nowadays many people are accustomed to typing this notation.
265288

266-
However, this notation conflicts when embedding Python code within Markdown or vice versa.
289+
However, this notation conflicts when embedding Python code within Markdown or
290+
vice versa.
267291
Therefore, considering these drawbacks, increasing the variety of quote
268-
characters is not seen as a superior idea compared to adding a prefix to string literals.
292+
characters is not seen as a superior idea compared to adding a prefix to
293+
string literals.
269294

270295

271296
``__future__`` import
272297
---------------------
273298

274-
Instead of adding a prefix to string literals, the idea of using a ``__future__``
275-
import to change the default behavior of multiline string literals was also considered.
299+
Instead of adding a prefix to string literals, the idea of using a
300+
``__future__`` import to change the default behavior of multiline
301+
string literals was also considered.
276302
This could help simplify Python's grammar in the future.
277303

278-
But rewriting all existing complex codebases to the new notation may not be straightforward.
279-
Until all multiline strings in that source code are rewritten to the new notation, automatic dedentation cannot be utilized.
304+
But rewriting all existing complex codebases to the new notation may not be
305+
straightforward.
306+
Until all multiline strings in that source code are rewritten to
307+
the new notation, automatic dedentation cannot be utilized.
280308

281-
Until all users can rewrite existing codebases to the new notation, two types of Python syntax will coexist indefinitely.
282-
Therefore, `many people preferred the new string prefix <https://discuss.python.org/t/90988/54>`__ over the ``__future__`` import.
309+
Until all users can rewrite existing codebases to the new notation,
310+
two types of Python syntax will coexist indefinitely.
311+
Therefore, `many people preferred the new string prefix <https://discuss.python.org/t/90988/54>`__
312+
over the ``__future__`` import.
283313

284314

285315
Copyright

0 commit comments

Comments
 (0)