|
| 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 | +- Content inside is **automatically dedented** based on the smallest common |
| 59 | + leading whitespace. |
| 60 | +- Optionally, the first non-whitespace token immediately after the opening |
| 61 | + triple quotes will be treated as a **language hint** for editors/tooling. |
| 62 | + This is comparable to markdown code fence info string [1]_ in order to |
| 63 | + speficy a media, or Gherkin doc strings content type [2]_. |
| 64 | +- At runtime, the resulting value is a normal `str` object, identical |
| 65 | + to any regular string. |
| 66 | + |
| 67 | +Syntax examples |
| 68 | +--------------- |
| 69 | + |
| 70 | +Dedented string without hint: |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + d""" |
| 75 | + Some plain text |
| 76 | + across multiple lines |
| 77 | + """ |
| 78 | +
|
| 79 | +Dedented string with language hint: |
| 80 | + |
| 81 | +.. code-block:: python |
| 82 | +
|
| 83 | + d"""sql |
| 84 | + SELECT * |
| 85 | + FROM users |
| 86 | + """ |
| 87 | +
|
| 88 | +Additional example with another language hint: |
| 89 | + |
| 90 | +.. code-block:: python |
| 91 | +
|
| 92 | + d"""jinja2 |
| 93 | + <div> |
| 94 | + <h1>{{ title }}</h1> |
| 95 | + </div> |
| 96 | + """ |
| 97 | +
|
| 98 | +In both cases, the hint ("sql", "jinja2") is purely for tooling and has |
| 99 | +no impact at runtime. |
| 100 | +It allows syntax highlighting, linting to improves the developer experience. |
| 101 | + |
| 102 | +Invalid usage examples |
| 103 | +---------------------- |
| 104 | + |
| 105 | +.. code-block:: python |
| 106 | +
|
| 107 | + d"Just a single-line string" # Error: must use triple quotes |
| 108 | +
|
| 109 | +
|
| 110 | +Backwards Compatibility |
| 111 | +======================= |
| 112 | + |
| 113 | +This proposal introduces a new string prefix `d`. |
| 114 | +It does not affect any existing code. |
| 115 | + |
| 116 | + |
| 117 | +Security Implications |
| 118 | +===================== |
| 119 | + |
| 120 | +Security considerations for this feature would mostly involve data injection, |
| 121 | +especially since we're dealing with string manipulation and possibly embedding |
| 122 | +SQL, HTML, or any templating languages. |
| 123 | + |
| 124 | +Mitigation: While the d-string itself doesn't execute the content, users |
| 125 | +should be aware of security concerns when the content is later executed or |
| 126 | +rendered. |
| 127 | + |
| 128 | +How to Teach This |
| 129 | +================= |
| 130 | + |
| 131 | +The ``d`` prefix will be documented as part of the language standard. |
| 132 | +Code editors should update their syntax highlighting for d-string embeded |
| 133 | +language hinting. |
| 134 | + |
| 135 | + |
| 136 | +Reference Implementation |
| 137 | +======================== |
| 138 | + |
| 139 | +A reference implementation may be found at: |
| 140 | +https://github.com/mardiros/cpython/tree/features/d-string |
| 141 | + |
| 142 | +Footnotes |
| 143 | +========= |
| 144 | + |
| 145 | +.. [1] https://spec.commonmark.org/0.31.2/#example-142 |
| 146 | +.. [2] https://cucumber.io/docs/gherkin/reference/#doc-strings |
| 147 | +
|
| 148 | +Copyright |
| 149 | +========= |
| 150 | + |
| 151 | +This document is placed in the public domain or under the |
| 152 | +CC0-1.0-Universal license, whichever is more permissive. |
0 commit comments