Skip to content

textwrap.shorten() raises "placeholder too large" for text that already fits #160043

Open

Description

@tonghuaroot

Bug description

textwrap.shorten() raises ValueError for text that fits in width when
the placeholder is wider than width:

>>> import textwrap
>>> textwrap.shorten('ab', width=4)
Traceback (most recent call last):
  ...
ValueError: placeholder too large for max width

shorten's docstring says such text is returned unchanged: "If it then fits
in the width, it is returned as is." 'ab' fits in width 4.

The same happens in wrap() whenever max_lines is set and the text fits but
the placeholder is wider than width:

>>> textwrap.wrap('hello world', width=20, max_lines=3, subsequent_indent=' ' * 17)
Traceback (most recent call last):
  ...
ValueError: placeholder too large for max width
>>> textwrap.wrap('hello world', width=20, subsequent_indent=' ' * 17)
['hello world']

_wrap_chunks() checks the placeholder-fits guard eagerly, before wrapping,
whenever max_lines is set. But the placeholder is only used when the text is
actually truncated, so the guard fires on text that never needs it.

CPython versions tested on

3.14, 3.15, CPython main branch

Operating systems tested on

macOS

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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