Skip to content

Migrate from tomlkit to tomlrt - #49

Open
dimbleby wants to merge 1 commit into
mrijken:masterfrom
dimbleby:migrate-to-tomlrt
Open

dimbleby wants to merge 1 commit into
mrijken:masterfrom
dimbleby:migrate-to-tomlrt

Conversation

@dimbleby

@dimbleby dimbleby commented Aug 15, 2026

Copy link
Copy Markdown

Hi -

I have been writing tomlrt, which occupies roughly the same space as tomlkit - but hopefully has a simpler API, fewer bugs, better performance.

Honestly tomlkit looks as though it probably is mostly fine here, do just close this out if you are happy enough with it.

What I expect you get from this PR is

  • some modest code clean-ups (no unwrap(), ensure_table() is a convenience, standard exceptions)
  • correct preservation of line endings (by treating files as binary)
  • probably better formatting in niche edge cases
    • In my experience tomlrt does better than tomlkit here. But I am likely biased.
  • improved performance

all of which is perhaps not a lot; and it is always possible that future tomlkit versions will be faster and fix bugs anyway.

But since I have performed the experiment, I thought I would offer it to you.


Swap the TOML reader/writer for tomlrt:

  • tomlkit.parse / tomlkit.dumps become tomlrt.load / tomlrt.dump
  • tomlkit.exceptions.NonExistentKey becomes a plain KeyError
  • add_section uses Document.ensure_table, replacing the hand-rolled descent loop, and reports a clean error instead of a traceback when a path component cannot be descended through
  • tomlrt returns plain str / int / bool, so the hasattr(x, "unwrap") guard in get is no longer needed

Read and write in binary mode. Toml is always utf-8, but read_text() and write_text() use the locale encoding, which crashes on non-ascii content under a non-utf-8 locale, and would rewrite CRLF line endings to LF. Fixes #37.

Two test assertions change, both cosmetic: tomlrt emits a blank line before a new section header, and removing the last key leaves an empty file rather than a stray newline.

Swap the TOML reader/writer for tomlrt:

- tomlkit.parse / tomlkit.dumps become tomlrt.load / tomlrt.dump
- tomlkit.exceptions.NonExistentKey becomes a plain KeyError
- add_section uses Document.ensure_table, replacing the hand-rolled
  descent loop, and reports a clean error instead of a traceback when a
  path component cannot be descended through
- tomlrt returns plain str / int / bool, so the hasattr(x, "unwrap")
  guard in `get` is no longer needed

Read and write in binary mode. Toml is always utf-8, but read_text() and
write_text() use the locale encoding, which crashes on non-ascii content
under a non-utf-8 locale, and would rewrite CRLF line endings to LF.
Fixes mrijken#37.

Two test assertions change, both cosmetic: tomlrt emits a blank line
before a new section header, and removing the last key leaves an empty
file rather than a stray newline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with encoding of file in windows

1 participant