Skip to content

Request: Despite dump's width parameter, don't create inline lists/dicts when they are flat with a single item #64

@AndydeCleyre

Description

@AndydeCleyre

Hi, and thanks as always!

While the resulting line is shorter than the width limit, it's without the tradeoff of fewer lines.

I'm requesting that either this behavior change, or that a new parameter be offered, to forgo inlining when the structure has a length of 1 and there is no nesting (either a list with one item which is a string, or a dict with one entry whose value is a string).

Another way of putting it: don't use the inline form when the alternative is already a single line.

Current behavior:

>>> from nestedtext import dumps
>>> print(dumps({'mode': 'plain'}, width=20))
{mode: plain}

>>> print(dumps({'navigation': {'mode': 'plain'}}, width=20))
navigation:
    {mode: plain}

>>> print(dumps({'mode': [1]}, width=20))
{mode: [1]}

>>> print(dumps([1], width=20))
[1]

>>> print(dumps([1, 2], width=20))
[1, 2]

>>> print(dumps([{'mode': 'one'}], width=20))
[{mode: one}]

Requested behavior:

>>> from nestedtext import dumps
>>> print(dumps({'mode': 'plain'}, width=20))
mode: plain

>>> print(dumps({'navigation': {'mode': 'plain'}}, width=20))
navigation:
    mode: plain

>>> print(dumps({'mode': [1]}, width=20))
{mode: [1]}

>>> print(dumps([1], width=20))
- 1

>>> print(dumps([1, 2], width=20))
[1, 2]

>>> print(dumps([{'mode': 'one'}], width=20))
[{mode: one}]

Diff of current and requested behavior from above:

--- current.py	2026-01-27 13:49:06.581031210 -0500
+++ requested.py	2026-01-27 13:48:56.801759721 -0500
@@ -1,16 +1,16 @@
 >>> from nestedtext import dumps
 >>> print(dumps({'mode': 'plain'}, width=20))
-{mode: plain}
+mode: plain
 
 >>> print(dumps({'navigation': {'mode': 'plain'}}, width=20))
 navigation:
-    {mode: plain}
+    mode: plain
 
 >>> print(dumps({'mode': [1]}, width=20))
 {mode: [1]}
 
 >>> print(dumps([1], width=20))
-[1]
+- 1
 
 >>> print(dumps([1, 2], width=20))
 [1, 2]

Side by side view of the same diff:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions