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.

Title: Automatically dedent docstring constants by default
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, mbussonn, methane, pablogsal, remi.lapeyre
Priority: normal Keywords:

Created on 2019-05-30 18:00 by gregory.p.smith, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg343990 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2019-05-30 18:00
I'm spawning this issue of as a separate feature from (adding string dedent method and an optimization to do it at compile timer on constants).

It'd be great if docstrings were given a similar treatment.  Right now we carry the whitespace burden within the str constants for __doc__ stored in all code objects in the process.  This adds up.

This is not _quite_ the same as calling textwrap.dedent() or the upcoming str.dedent method on them at compile time.  We need to special case the first line of docstrings.  inspect.getdoc() is our library function that does this for us today.

Change of breaking things with this change?  not impossible, but extremely minor.  Something using docstrings as data _and_ depending on leading indentation whitespace preservation would not like this.  I am not aware of anything that would ever do that.
msg344128 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-05-31 21:35
Hi, I'm working on a PR. It should be ready in a couple of days. It's more involved than what I thought as to avoid importing inspect during compilation I will probably need to port cleandoc() in C.
msg344801 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-06-06 11:34
How about do `inspect.cleandoc()` instead of just a dedent?
Some method has docstring like this:

    """First line

    blah blah blah blah

        example code here

In such docstring, dedent can not strip indent well.

There is existing attempt (in Japanese):
msg344819 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-06-06 14:36
This is the function I inlined and as far as I can tell, my approach as been similar to the one you linked.

I'm still need to fix some issues as doctest was expecting to find the string before dedenting though.
Date User Action Args
2022-04-11 14:59:16adminsetgithub: 81283
2019-06-06 14:36:45remi.lapeyresetmessages: + msg344819
2019-06-06 11:34:15methanesetnosy: + methane
messages: + msg344801
2019-05-31 21:35:08remi.lapeyresetnosy: + remi.lapeyre
messages: + msg344128
2019-05-31 21:07:25pablogsalsetnosy: + pablogsal
2019-05-31 20:09:02mbussonnsetnosy: + mbussonn
2019-05-30 18:00:00gregory.p.smithcreate