[Python-ideas] A directive for indentation type, stricter indentation parsing.

Chris Angelico rosuav at gmail.com
Tue Mar 26 07:01:04 EDT 2019


On Tue, Mar 26, 2019 at 9:49 PM Mikhail V <mikhailwas at gmail.com> wrote:
> Procedural removal is not cool, because it does not know the parent indentation
> of the statement that contains the text block, thus it can't resolve
> automatically
> string indents  that were intentionally indented to include extra space.
>
> E.g. this, where "s=" line is already inside an indented block:
>     s = """
>                 Hello
>             world"""
>
> Say I use 1 tab for 1 indent - what I want here is to remove 1 tab from
> each string line AND 1 tab that belongs to code formatting (current indent
> of the "s=" line). And if you try to guess it from the string  itself -
> it is impossible to resolve all cases, because you still need some criteria
> - for example you could use criteria "minimal indent inside the string
> is the indent" but
> this will again fail if you want extra shift on same level inside the string.
> E.g. this:
>     s = """
>                 Hello
>                 world"""
>
> Here I do not want to remove all indents but only as in above - only 1
> level inside string
> and 1 from parent line.
> Do you get it?
>
> So in other words if I want my string blocks aligned within containing
> blocks, it  becomes
> impossible to resolve all un-indenting cases automatically.
>

This is true if you put your closing quotes on the same line as the
last line of text, because then there's no information available. If,
instead, you put the final triple-quote delimiter on its own line, you
then have the indentation preserved, and can remove just that much
indentation from each line. So, yes, you CAN unindent automatically.

Point of note: PEP 257 recommends this for docstrings.

https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation

ChrisA


More information about the Python-ideas mailing list