Skip to content

Other consequences of textwrap.wrap() changes in 3.15 #140627

@avylove

Description

@avylove

Bug report

Bug description:

It seems like the change in #139902 had other effects than what the PR intended. If those changes are intentional, it would be good if they were documented. And if they were not intentional, then this seem like a bug. This change has since been backported (#139902, #139903), though I don't believe those backports have been released.

Given the following code:

import textwrap
textwrap.wrap(' ABCDEFG', width=1)

In 3.14.0, this returns [' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G']
In 3.15.0a1, this returns ['A', 'B', 'C', 'D', 'E', 'F', 'G']

Since the PR only mentions trailing whitespace, it appears this change was unintended.

The documentation reads as follows.

drop_whitespace
(default: True) If true, whitespace at the beginning and ending of every line (after wrapping but before indenting) is dropped. Whitespace at the beginning of the paragraph, however, is not dropped if non-whitespace follows it. If whitespace being dropped takes up an entire line, the whole line is dropped.

I am not sure how to interpret this. This line would seem to indicate the space should be preserved.

Whitespace at the beginning of the paragraph, however, is not dropped if non-whitespace follows it.

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions