Skip to content

Commit 1ff6afe

Browse files
committed
Add a pep for d-string
1 parent 45e6128 commit 1ff6afe

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

peps/pep-0791.rst

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
PEP: 791
2+
Title: Dedented Multiline Strings with Optional Language Hinting
3+
Author: Guillaume Gauvrit
4+
Discussions-To: Pending
5+
Status: Draft
6+
Type: Standards Track
7+
Created: 06-May-2025
8+
Python-Version: 3.15
9+
10+
11+
12+
Abstract
13+
========
14+
15+
This PEP proposes a new string prefix ``d`` for Python that enables automatic
16+
dedenting of multiline strings.
17+
Optionally, a language hint may be included immediately after the opening
18+
triple quotes to assist editors and tooling with syntax highlighting.
19+
20+
21+
Motivation
22+
==========
23+
24+
Writing readable, properly formatted multiline strings currently requires
25+
the use of ``textwrap.dedent``, adding verbosity and cognitive overhead.
26+
Developers often embed structured content (SQL, HTML, ...) within strings,
27+
but Python does not natively support lightweight language hinting.
28+
29+
Combining dedenting with an optional language hint improves both code
30+
readability and tooling support without impacting runtime behavior.
31+
32+
Additionally, this proposal complements the upcoming t-string
33+
feature (PEP 750), which would allow template strings with placeholders.
34+
By combining the d-string with t-strings, developers can easily format and
35+
manage indented content (like SQL queries or template code) while keeping
36+
everything neat and readable.
37+
38+
The d-string's dedenting behavior would work seamlessly with t-strings,
39+
enabling both automatic formatting and interpolation in a single workflow,
40+
streamlining code maintenance and readability.
41+
42+
43+
Rationale
44+
=========
45+
46+
- **Dedenting** greatly improves the usability of multiline strings.
47+
- **Language hinting** helps modern editors provide syntax highlighting,
48+
linting, and formatting.
49+
- Keeping these two features together makes sense, as language-specific blocks
50+
are often multi-line.
51+
- There is **no runtime penalty**: the hint is ignored after parsing.
52+
53+
54+
Specification
55+
=============
56+
57+
- ``d`` prefix can only be used with triple-quoted strings (``"""`` or
58+
``'''``).
59+
- Content inside is **automatically dedented** based on the smallest common
60+
leading whitespace.
61+
- Optionally, the first non-whitespace token immediately after the opening
62+
triple quotes will be treated as a **language hint** for editors/tooling.
63+
This is comparable to markdown code fence info string [1]_ in order to
64+
speficy a media, or Gherkin doc strings content type [2]_.
65+
- At runtime, the resulting value is a normal ``str`` object, identical
66+
to any regular string.
67+
68+
Syntax examples
69+
---------------
70+
71+
Dedented string without hint:
72+
73+
.. code-block:: python
74+
75+
d"""
76+
Some plain text
77+
across multiple lines
78+
"""
79+
80+
Dedented string with language hint:
81+
82+
.. code-block:: python
83+
84+
d"""sql
85+
SELECT *
86+
FROM users
87+
"""
88+
89+
Additional example with another language hint:
90+
91+
.. code-block:: python
92+
93+
d"""jinja2
94+
<div>
95+
<h1>{{ title }}</h1>
96+
</div>
97+
"""
98+
99+
In both cases, the hint ("sql", "jinja2") is purely for tooling and has
100+
no impact at runtime.
101+
It allows syntax highlighting, linting to improves the developer experience.
102+
103+
Invalid usage examples
104+
----------------------
105+
106+
.. code-block:: python
107+
108+
d"Just a single-line string" # Error: must use triple quotes
109+
110+
111+
Backwards Compatibility
112+
=======================
113+
114+
This proposal introduces a new string prefix ``d``.
115+
It does not affect any existing code.
116+
117+
118+
Security Implications
119+
=====================
120+
121+
Security considerations for this feature would mostly involve data injection,
122+
especially since we're dealing with string manipulation and possibly embedding
123+
SQL, HTML, or any templating languages.
124+
125+
Mitigation: While the d-string itself doesn't execute the content, users
126+
should be aware of security concerns when the content is later executed or
127+
rendered.
128+
129+
How to Teach This
130+
=================
131+
132+
The ``d`` prefix will be documented as part of the language standard.
133+
Code editors should update their syntax highlighting for d-string embeded
134+
language hinting.
135+
136+
137+
Reference Implementation
138+
========================
139+
140+
A reference implementation may be found at:
141+
https://github.com/mardiros/cpython/tree/features/d-string
142+
143+
Footnotes
144+
=========
145+
146+
.. [1] https://spec.commonmark.org/0.31.2/#example-142
147+
.. [2] https://cucumber.io/docs/gherkin/reference/#doc-strings
148+
149+
Copyright
150+
=========
151+
152+
This document is placed in the public domain or under the
153+
CC0-1.0-Universal license, whichever is more permissive.

0 commit comments

Comments
 (0)