Skip to content

Regression: impossible to dump multiline strings with leading newline #655

Closed

Description

@sattlerc

Pull request #551 changed the behaviour of raw, non-escaped string values. Running

import tomlkit
s = tomlkit.string(
  "\n  aligned\n  strings\n",
  multiline=True,
  literal=True,
  escape=False,
)
print(tomlkit.dumps(s))

used to output

'''
  aligned
  strings
'''

as expected, but now outputs

'''

  aligned
  strings
'''

(note the extra newline). In fact, it is now impossible to produce the desired output.

Proposed fix: hide the behaviour change of #551 under the escape flag.

Activity

  1. sattlerc commented on Oct 14, 2026

    @sattlerc
    ContributorAuthor

    #658 is a clean fix that does not change the API.

    But the situation is not ideal because under escape=False the value of the string does not match the parsing of the original.

    More principled solutions that would change the API:

    • Have the string function take two optional string arguments value and original, one of which must be given. If original is None, it is computed from value as currently with escape=True. If value is None, it is parsed from original. If both are given, one might have a sanity check that the parsing of original agrees with value.
    • Have two separate String building functions, one taking a parsed string and one taking an unparsed string.
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions