Skip to content

Commit a987eb1

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

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

peps/pep-0791.rst

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

0 commit comments

Comments
 (0)