This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author RalfM
Recipients RalfM
Date 2020-08-30.22:53:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1598827998.17.0.2637020471.issue41671@roundup.psfhosted.org>
In-reply-to
Content
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> def func1():
...     """This is func1.
...     """
...     pass
...
>>> inspect.getdoc(func1)
'This is func1.\n    '
>>>
>>> def func2():
...     """Line1
...        Line2 
...            
...            """
...
>>> inspect.getdoc(func2)
'Line1\nLine2 \n    \n    '

Note: The blank line between "Line2 " and the closing """ contains 11 spaces.

The algorithm given in PEP 257 returns what I would expect, i.e. 
'This is func1.'
and
'Line1\nLine2'
respectively.

Strictly speaking, inspect.cleandoc doesn't claim to implement PEP 257.
However, there is a comment "# Remove any trailing or leading blank lines." in the code of inspect.cleandoc, and this is obviously not done.

Looking at the code, the reason seems to be twofold:

1. When removing the indentation, PEP 257 also does a .rstrip() on the lines, inspect.cleandoc doesn't.
As a consequence, in inspect.cleandoc trailing lines with many spaces will still contain spaces after the indentation has been removed, thus are not empty and the "while lines and not lines[-1]" doesn't remove them.
That explains func2 above.

2. If all lines but the first are blank (as in func1 above), indent / margin will be sys.maxint / sys.maxsize and no indentation will be removed.
PEP 257 copies dedented lines to a new list. If no indentation needs to be removed, nothing but the first line will be copied, and so the trailing lines are gone.
inspect.cleandoc dedents lines inplace. If no indentation needs to be removed the trailing lines with spaces remain and, as they contain spaces, the "while lines and not lines[-1]" doesn't remove them.

There is another difference between PEP 257 and inspect.cleandoc: PEP 257 removes trailing whitespace on every line, inspect.cleandoc preserves it.
I don't know whether that's intentional.

I see this behaviour in 3.7 and 3.8, and the inspect.cleandoc code is unchanged in 3.9.0rc1.
History
Date User Action Args
2020-08-30 22:53:18RalfMsetrecipients: + RalfM
2020-08-30 22:53:18RalfMsetmessageid: <1598827998.17.0.2637020471.issue41671@roundup.psfhosted.org>
2020-08-30 22:53:18RalfMlinkissue41671 messages
2020-08-30 22:53:17RalfMcreate