Make textwrap reject nonsensical indent/width combinations

I was looking at some of the open PRs for textwrap, and there are some cases in which the caller’s request can’t be satisfied.

For example, an indent which can’t fit in the width:

>>> textwrap.wrap("abcdef", width=2, subsequent_indent="  ")
['ab', '  c', '  d', '  e', '  f']

I find that result to be wrong but there is no possible correct result.

This can also produce strange behavior, including a hang if you use whitespace in the right way.

If you do certain other nonsensical things, you get a ValueError:

>>> textwrap.wrap("abcdef", width=1, max_lines=2)
... # traceback
ValueError: placeholder too large for max width

I think at a minimum we should make it an error to give an indent too big for the width. Does anyone find that change objectionable? Maybe there are other cases like this which should become more strict in textwrap?

4 Likes