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.

classification
Title: If use textwrap.dedent with string formatting, may get unintended sentences.
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, xncbf12
Priority: normal Keywords:

Created on 2022-03-12 05:01 by xncbf12, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (12)
msg414972 - (view) Author: 김준환 (xncbf12) Date: 2022-03-12 05:01
If use textwrap.dedent with string formatting, may get unintended sentences.

For example, a sentence like this:

```
import textwrap

def get_something_string():
    return textwrap.dedent("""\
        test text2
        test text3
        test text4""")

textwrap.dedent(f"""\
    test text1
    {get_something_string()}
    test text5
    test text6""")
```

I look forward to the following results.

```
test text1
test text2
test text3
test text4
test text5
test text6
```

But the actual result is:
```
    test text1
    test text2
test text3
test text4
    test text5
    test text6
```

I understand that this code works in this way because the intent of the string defined inside the method is different from the intent of the string outside the method.

However, regular users, like me, will think of having a consistent intent result value. (because they believe dedent will clear a consistent intent)

If you are concerned that the existing code will be broken, I can implement the function using the flag parameter.

If you agree with my opinion, I will submit a PR.
msg414973 - (view) Author: 김준환 (xncbf12) Date: 2022-03-12 05:04
Sorry, it's an indent , not an intent .
msg414975 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-12 06:50
What is the flag you mention? What would it do?

This sounds like a new feature, which can only go in to 3.11.
msg414976 - (view) Author: 김준환 (xncbf12) Date: 2022-03-12 07:12
If it's only for version 3.11, what do you think of the default behavior of the dedent method being changed?
The method description says:

> Remove any common leading whitespace from every line in `text`.

```
def get_something_string():
     return textwrap.dedent("""\
         test text2
         test text3
         test text4""")

textwrap.dedent(f"""\
     test text1
     {get_something_string()} <<
     test text5
     test text6""")
```
I think it should work assuming that the indentation of {get_something_string()} and the indentation of test text2 are on the same line.
msg414977 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-12 07:16
What would the presence of "<<" do? You haven't described your proposal.
msg414978 - (view) Author: 김준환 (xncbf12) Date: 2022-03-12 07:17
"<<" This marks that line. ignore it
msg414979 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-12 07:21
What would x be equal to here:

def get_something_string():
     return textwrap.dedent("""\
         test text2
         test text3
         test text4""")

x = f"""\
     test text1
     {get_something_string()}
     test text5
     test text6"""

?

With 3.10 it is: '     test text1\n     test text2\ntest text3\ntest text4\n     test text5\n     test text6'

Are you proposing to change that?
msg414980 - (view) Author: 김준환 (xncbf12) Date: 2022-03-12 07:30
No, I know `textwrap.dedent` as a convenience function used for code readability. But to have the result I want, I currently need to do something like this:
If it's a method inside a class, it's even more ugly.

Class A:
     def get_something_string():
         return textwrap.dedent("""\
     test text2
     test text3
     test text4""")

x = f"""\
      test text1
      {A.get_something_string()}
      test text5
      test text6"""

The duplicate use of dedent is to show an example.

Thought I needed a consistent dedent for every line while using the string formatting feature.
msg414981 - (view) Author: 김준환 (xncbf12) Date: 2022-03-12 07:32
Sorry, Can't edit comments
Please see the code example below

```
Class A:
    def get_something_string():
        return textwrap.dedent("""\
    test text2
    test text3
    test text4""")


textwrap.dedent(f"""\
    test text1
    {A.get_something_string()}
    test text5
    test text6""")
```
msg414982 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-12 07:40
I’m sorry, I don’t understand your proposal. Please answer my question about what “x” would be equal to under your proposed change.
msg414983 - (view) Author: 김준환 (xncbf12) Date: 2022-03-12 07:50
x does not change.
sorry. I also haven't figured out the implementation in my head yet.

I want to provide some functionality to provide consistent indentation while using formatting .

For example

"""
    test
    {multi_line_text}
""".format(multi_line_text, consistent_indent=True)

It's just a suggestion.
msg416491 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-04-01 14:53
I'm going to close this. If you have a more concrete proposal, either re-open this or bring it up on python-ideas.
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91148
2022-04-01 14:53:25eric.smithsetstatus: pending -> closed
resolution: rejected
messages: + msg416491

stage: resolved
2022-03-27 13:28:21eric.smithsetstatus: open -> pending
2022-03-12 07:50:03xncbf12setmessages: + msg414983
2022-03-12 07:40:30eric.smithsetmessages: + msg414982
2022-03-12 07:32:33xncbf12setmessages: + msg414981
2022-03-12 07:30:38xncbf12setmessages: + msg414980
2022-03-12 07:21:41eric.smithsetmessages: + msg414979
2022-03-12 07:17:16xncbf12setmessages: + msg414978
2022-03-12 07:16:03eric.smithsetmessages: + msg414977
2022-03-12 07:12:13xncbf12setmessages: + msg414976
versions: - Python 3.7, Python 3.8, Python 3.9, Python 3.10
2022-03-12 06:50:08eric.smithsetnosy: + eric.smith
messages: + msg414975
2022-03-12 05:04:53xncbf12setmessages: + msg414973
2022-03-12 05:01:38xncbf12create