[issue36906] Compile time textwrap.dedent() equivalent for str or bytes literals

Jan Tojnar report at bugs.python.org
Thu Dec 3 17:44:32 EST 2020


Jan Tojnar <jtojnar at gmail.com> added the comment:

One benefit of using a compile time feature over a runtime method is that the former allows for more predictable dedenting by first dedenting and only then interpolating variables.

For example, the following code does not dedent the test string at all:

```python
import textwrap

foo = "\n".join(["aaa", "bbb"])

test = textwrap.dedent(
    f"""
    block xxx:
        {foo}
    """
)
```

It would be much nicer if we had syntactical dedents based on the leftmost non-whitespace column in the string, as supported by Nix language, for example.

```python
test = (
    df"""
    block xxx:
    {textwrap.indent(foo, ' '*4)}
    """
)
```

It would be even nicer if the interpolated strings could be indented based on the column the interpolation occurs in but that can at least be done at runtime with only minor inconvenience.

----------
nosy: +jtojnar

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36906>
_______________________________________


More information about the Python-bugs-list mailing list