Which toml package to use in python?
See also: toml-lang and PEP 680
The verions of the packages tested in this report.
| Version | |
|---|---|
| toml | 0.10.2 |
| tomli/tomli_w | 2.5.0; tomli_w: 1.2.0 |
| tomlkit | 0.15.1 |
| rtoml | 0.13.0 |
| qtoml | 0.3.1 |
| tomllib | (Python 3.13.14) |
| tomlclass | 0.3.0 |
How the package dumps None value in python
Literally <package>.dumps(None)
| Dumped value or error | |
|---|---|
| toml | 'NoneType' object is not iterable |
| tomli/tomli_w | 'NoneType' object has no attribute 'items' |
| tomlkit | Expecting Mapping or TOML Table or Container, <class 'NoneType'> given |
| rtoml | "null" --- rtoml v0.11+ supports dumping None to a desired string: rtoml.dumps(data, none_value='@None'):"@None" |
| qtoml | 'NoneType' object has no attribute 'items' |
| tomllib | module 'tomllib' has no attribute 'dumps' |
| tomlclass | None cannot be represented in TOML |
How the package dumps key-value pair with value None
Literally <package>.dumps({"key": None})
| Dumped value or error | |
|---|---|
| toml | |
| tomli/tomli_w | Object of type 'NoneType' is not TOML serializable |
| tomlkit | Unable to convert an object of <class 'NoneType'> to a TOML item |
| rtoml | key = "null" --- rtoml v0.11+ supports dumping None to a desired string: rtoml.dumps(data, none_value='@None'):key = "@None" |
| qtoml | TOML cannot encode None |
| tomllib | module 'tomllib' has no attribute 'dumps' |
| tomlclass | None cannot be represented in TOML |
How the package dumps a list with None value in it.
Literally <package>.dumps({"key": [1, 2, 3, None, 5]})
| Dumped value or error | |
|---|---|
| toml | key = [ 1, 2, 3, "None", 5,] |
| tomli/tomli_w | Object of type 'NoneType' is not TOML serializable |
| tomlkit | Unable to convert an object of <class 'NoneType'> to a TOML item |
| rtoml | key = [1, 2, 3, "null", 5] --- rtoml v0.11+ supports dumping None to a desired string: rtoml.dumps(data, none_value='@None'):key = [1, 2, 3, "@None", 5] |
| qtoml | bad type '<class 'NoneType'>' for dump_value |
| tomllib | module 'tomllib' has no attribute 'dumps' |
| tomlclass | None cannot be represented in TOML |
How the package loads None-like value in string
Literally <package>.loads('v1 = "null" v2 = "None"')
| Loaded as | |
|---|---|
| toml | {'v1': 'null', 'v2': 'None'} |
| tomli/tomli_w | module 'tomli_w' has no attribute 'loads' |
| tomlkit | {'v1': 'null', 'v2': 'None'} |
| rtoml | {'v1': 'null', 'v2': 'None'} --- rtoml v0.11+ supports loading custom None values: rtoml.loads(data, none_value='None'):{'v1': 'null', 'v2': None} rtoml.loads(data, none_value='null'):{'v1': None, 'v2': 'None'} |
| qtoml | {'v1': 'null', 'v2': 'None'} |
| tomllib | {'v1': 'null', 'v2': 'None'} |
| tomlclass | {'v1': 'null', 'v2': 'None'} |
How the package dumps a python dictionary with a heterogenous array.
Literally <package>.dumps({"v": [1, 1.2, True, "string"]})
| Dumped value or error | |
|---|---|
| toml | v = [ 1, 1.2, true, "string",] |
| tomli/tomli_w | v = [ 1, 1.2, true, "string", ] |
| tomlkit | v = [1, 1.2, true, "string"] |
| rtoml | v = [1, 1.2, true, "string"] |
| qtoml | v = [1, 1.2, true, 'string'] |
| tomllib | Dumping not supported |
| tomlclass | v = [1, 1.2, true, "string"] |
How the package loads a toml string with a heterogenous array.
Literally <package>.loads('v = [1, 1.2, True, "string"]')
| Loaded as | |
|---|---|
| toml | Not a homogeneous array (line 2 column 1 char 1) |
| tomli/tomli_w | {'v': [1, 1.2, True, 'string']} |
| tomlkit | {'v': [1, 1.2, True, 'string']} |
| rtoml | {'v': [1, 1.2, True, 'string']} |
| qtoml | {'v': [1, 1.2, True, 'string']} |
| tomllib | {'v': [1, 1.2, True, 'string']} |
| tomlclass | {'v': [1, 1.2, True, 'string']} |
How the package dumps a python dictionary with a nested array.
Literally <package>.dumps({"v": [[1], [1, 2]]})
| Dumped value or error | |
|---|---|
| toml | v = [ [ 1,], [ 1, 2,],] |
| tomli/tomli_w | v = [ [ 1, ], [ 1, 2, ], ] |
| tomlkit | v = [[1], [1, 2]] |
| rtoml | v = [[1], [1, 2]] |
| qtoml | v = [[1], [1, 2]] |
| tomllib | Dumping not supported |
| tomlclass | v = [[1], [1, 2]] |
How the package loads a toml string with a nested array.
Literally <package>.loads('v = [[1], [1, 2]]')
| Loaded as | |
|---|---|
| toml | {'v': [[1], [1, 2]]} |
| tomli/tomli_w | {'v': [[1], [1, 2]]} |
| tomlkit | {'v': [[1], [1, 2]]} |
| rtoml | {'v': [[1], [1, 2]]} |
| qtoml | {'v': [[1], [1, 2]]} |
| tomllib | {'v': [[1], [1, 2]]} |
| tomlclass | {'v': [[1], [1, 2]]} |
Whether the package preserves the order of the keys while dumps a python dictionary.
Thus, whether <package>.dumps({"c": 1, "a": 2, "b": 3}) yields a string
like c = 1\na = 2\nb = 3\n.
| Order kept? | |
|---|---|
| toml | Kept |
| tomli/tomli_w | Kept |
| tomlkit | Kept |
| rtoml | Kept |
| qtoml | Kept |
| tomllib | Dumping not supported |
| tomlclass | Kept |
Whether the package preserves the order of the keys while loads a TOML string.
Thus, whether <package>.loads('c = 1\na = 2\nb = 3\n') yields
a dictionary with keys in the order of ['c', 'a', 'b'].
| Order kept? | |
|---|---|
| toml | Kept |
| tomli/tomli_w | Kept |
| tomlkit | Kept |
| rtoml | Kept |
| qtoml | Kept |
| tomllib | Kept |
| tomlclass | Kept |
How the package dumps Unicode in python
Literally, <package>.dumps({"你好": "世界"})
| Dumped value | |
|---|---|
| toml | "你好" = "世界" |
| tomli/tomli_w | "你好" = "世界" |
| tomlkit | "你好" = "世界" |
| rtoml | "你好" = "世界" |
| qtoml | '你好' = '世界' |
| tomllib | Dumping not supported |
| tomlclass | "你好" = "世界" |
How the package loads a file with unicode.
The file was created by:
## Create a file with unicode content
with open(self.datafile, "w", encoding="utf-8") as f:
f.write('"你好" = "世界"\n')
## Use `<package>.load()` to load the file
with open(self.datafile, "r", encoding="utf-8") as f:
loaded = <package>.load(f)| Loaded as | |
|---|---|
| toml | {'你好': '世界'} |
| tomli/tomli_w | bytes object expected; got str When loaded with rb:{'你好': '世界'} |
| tomlkit | {'你好': '世界'} |
| rtoml | {'你好': '世界'} |
| qtoml | {'你好': '世界'} |
| tomllib | File must be opened in binary mode, e.g. use open('foo.toml', 'rb')When loaded with rb:{'你好': '世界'} |
| tomlclass | {'你好': '世界'} |
Test the compliance with the standard test suites for valid toml files here:
The tests come up with a JSON counterpart that can be used to valid whether loading the toml file yields the same result as the JSON counterpart.
| Result (toml-test v1.5.0) | |
|---|---|
| toml | array/mixed-int-array.toml Not a homogeneous array (line 1 column 1 char 0) array/mixed-int-float.toml Not a homogeneous array (line 1 column 1 char 0) array/mixed-int-string.toml Not a homogeneous array (line 1 column 1 char 0) array/mixed-string-table.toml list index out of range array/nested-double.toml Not a homogeneous array (line 1 column 1 char 0) array/string-with-comma-2.toml string index out of range comment/tricky.toml Parsed as unexpected data. datetime/datetime.toml Parsed as unexpected data. datetime/local-time.toml Parsed as unexpected data. datetime/no-seconds.toml invalid literal for int() with base 0: '13:37' (line 2 column 1 char 46) float/zero.toml Weirdness with leading zeroes or underscores in your number. (line 4 column 1 char 47) inline-table/key-dotted-1.toml Parsed as unexpected data. inline-table/key-dotted-5.toml Not a homogeneous array (line 2 column 1 char 20) inline-table/multiline.toml Invalid inline table value encountered (line 1 column 1 char 0) inline-table/newline.toml Key name found without value. Reached end of line. (line 5 column 2 char 98) key/dotted-1.toml Parsed as unexpected data. key/dotted-2.toml Found invalid character in key name: '"'. Try quoting the key name. (line 7 column 11 char 166) key/dotted-empty.toml Duplicate keys! (line 2 column 1 char 17) key/escapes.toml Parsed as unexpected data. key/quoted-unicode.toml Duplicate keys! (line 3 column 1 char 19) key/unicode.toml Found invalid character in key name: ''. Try quoting the key name. (line 5 column 2 char 67) spec/array-0.toml Not a homogeneous array (line 8 column 1 char 261) spec/keys-4.toml Found invalid character in key name: 'c'. Try quoting the key name. (line 2 column 8 char 57) spec/local-time-0.toml Parsed as unexpected data. string/ends-in-whitespace-escape.toml Reserved escape sequence used (line 6 column 1 char 28) string/escape-esc.toml Reserved escape sequence used (line 1 column 1 char 0) string/hex-escape.toml Reserved escape sequence used (line 3 column 1 char 35) string/multiline-quotes.toml Unterminated string found. Reached end of file. (line 27 column 1 char 664) string/raw-multiline.toml Unbalanced quotes (line 20 column 50 char 532) table/empty-name.toml Can't have a keygroup with an empty name (line 1 column 1 char 0) 157/187 (83.96%) passed |
| tomli/tomli_w | key/unicode.toml Invalid statement (at line 3, column 1) 186/187 (99.47%) passed |
| tomlkit | key/unicode.toml Empty key at line 3 col 0 186/187 (99.47%) passed |
| rtoml | datetime/no-seconds.toml expected a colon, found a newline at line 2 column 26 inline-table/newline.toml expected a table key, found a newline at line 3 column 21 key/unicode.toml unexpected character found: \u{20ac} at line 3 column 1spec/table-9.toml duplicate key: apple for key fruit at line 8 column 1string/escape-esc.toml invalid escape character in string: e at line 1 column 9string/hex-escape.toml invalid escape character in string: x at line 3 column 21table/array-within-dotted.toml duplicate key: apple for key fruit at line 4 column 1180/187 (96.26%) passed |
| qtoml | comment/after-literal-no-ws.toml can't parse type (line 1, column 4) comment/tricky.toml can't parse type (line 11, column 7) datetime/datetime.toml Didn't find expected newline (line 4, column 18) datetime/milliseconds.toml Didn't find expected newline (line 2, column 27) datetime/no-seconds.toml can't parse type (line 2, column 20) inline-table/newline.toml ' ' cannot begin key (line 3, column 20) key/unicode.toml '€' cannot begin key (line 3, column 0) spec/string-4.toml Didn't find expected newline (line 7, column 62) spec/string-7.toml Didn't find expected newline (line 7, column 50) string/escape-esc.toml \e not a valid escape (line 1, column 33) string/hex-escape.toml \x not a valid escape (line 3, column 43) string/multiline-quotes.toml Didn't find expected newline (line 4, column 26) string/raw-multiline.toml Didn't find expected newline (line 22, column 3) 174/187 (93.05%) passed |
| tomllib | datetime/no-seconds.toml Expected newline or end of document after a statement (at line 2, column 23) inline-table/newline.toml Invalid initial character for a key part (at line 3, column 21) key/unicode.toml Invalid statement (at line 3, column 1) string/escape-esc.toml Unescaped '' in a string (at line 1, column 10) string/hex-escape.toml Unescaped '' in a string (at line 3, column 22) 182/187 (97.33%) passed |
| tomlclass | datetime/no-seconds.toml expected end of line after key/value pair (line 2, column 23): without-seconds-1 = 13:37 inline-table/newline.toml invalid key character '\n' (line 3, column 21): trailing-comma-1 = { key/unicode.toml invalid key character '€' (line 3, column 1): € = 'Euro' string/escape-esc.toml invalid escape \e (line 1, column 9): esc = "\e There is no escape! \e" string/hex-escape.toml invalid escape \x (line 3, column 21): whitespace = "\x20 \x09 \x1b \x0d\x0a" 182/187 (97.33%) passed |
Test the compliance with the standard test suites for invalid toml files here:
Not OK: The toml file is parsed without error, but expected to fail.OK: All files are failed to parse, as expected. Showing the last parsing error.
| Result (toml-test v1.5.0) | |
|---|---|
| toml | Not OK: array/double-comma-1.toml incorrectly parsed. Not OK: array/double-comma-2.toml incorrectly parsed. Not OK: array/extending-table.toml incorrectly parsed. Not OK: array/no-close-1.toml incorrectly parsed. Not OK: array/no-close-2.toml incorrectly parsed. Not OK: array/no-close-3.toml incorrectly parsed. Not OK: array/no-close-4.toml incorrectly parsed. Not OK: array/no-close-5.toml incorrectly parsed. Not OK: array/no-close-6.toml incorrectly parsed. Not OK: array/no-close-7.toml incorrectly parsed. Not OK: 63 more items incorrectly parsed. 298/371 (80.32%) passed |
| tomli/tomli_w | Not OK: datetime/no-secs.toml incorrectly parsed. Not OK: inline-table/linebreak-1.toml incorrectly parsed. Not OK: inline-table/linebreak-2.toml incorrectly parsed. Not OK: inline-table/linebreak-3.toml incorrectly parsed. Not OK: inline-table/linebreak-4.toml incorrectly parsed. Not OK: inline-table/trailing-comma.toml incorrectly parsed. Not OK: local-datetime/no-secs.toml incorrectly parsed. Not OK: local-time/no-secs.toml incorrectly parsed. Not OK: string/basic-byte-escapes.toml incorrectly parsed. 362/371 (97.57%) passed |
| tomlkit | Not OK: control/bare-cr.toml incorrectly parsed. Not OK: control/comment-cr.toml incorrectly parsed. Not OK: control/multi-cr.toml incorrectly parsed. Not OK: control/rawmulti-cd.toml incorrectly parsed. Not OK: datetime/no-secs.toml incorrectly parsed. Not OK: inline-table/linebreak-1.toml incorrectly parsed. Not OK: inline-table/linebreak-2.toml incorrectly parsed. Not OK: inline-table/linebreak-3.toml incorrectly parsed. Not OK: inline-table/linebreak-4.toml incorrectly parsed. Not OK: inline-table/trailing-comma.toml incorrectly parsed. Not OK: 3 more items incorrectly parsed. 358/371 (96.50%) passed |
| rtoml | Not OK: control/bare-cr.toml incorrectly parsed. Not OK: control/comment-cr.toml incorrectly parsed. Not OK: control/comment-del.toml incorrectly parsed. Not OK: control/multi-cr.toml incorrectly parsed. Not OK: control/rawmulti-cd.toml incorrectly parsed. Not OK: datetime/offset-overflow-hour.toml incorrectly parsed. Not OK: datetime/offset-overflow-minute.toml incorrectly parsed. Not OK: integer/positive-bin.toml incorrectly parsed. Not OK: integer/positive-hex.toml incorrectly parsed. Not OK: integer/positive-oct.toml incorrectly parsed. 361/371 (97.30%) passed |
| qtoml | Not OK: control/bare-cr.toml incorrectly parsed. Not OK: control/comment-cr.toml incorrectly parsed. Not OK: control/comment-del.toml incorrectly parsed. Not OK: control/comment-ff.toml incorrectly parsed. Not OK: control/comment-lf.toml incorrectly parsed. Not OK: control/comment-null.toml incorrectly parsed. Not OK: control/comment-us.toml incorrectly parsed. Not OK: control/multi-cr.toml incorrectly parsed. Not OK: control/rawmulti-cd.toml incorrectly parsed. Not OK: datetime/offset-overflow-minute.toml incorrectly parsed. Not OK: 14 more items incorrectly parsed. 347/371 (93.53%) passed |
| tomllib | OK: table/with-pound.toml Expected ']' at the end of a table declaration (at line 1, column 5) 371/371 (100%) passed |
| tomlclass | Not OK: control/bare-cr.toml incorrectly parsed. Not OK: control/comment-cr.toml incorrectly parsed. Not OK: control/multi-cr.toml incorrectly parsed. Not OK: control/rawmulti-cd.toml incorrectly parsed. 367/371 (98.92%) passed |
Test the compliance with python tomllib test data (since python 3.11) for valid toml files here:
https://github.com/python/cpython/tree/3.11/Lib/test/test_tomllib/data/valid
The tests come up with a JSON counterpart that can be used to valid whether loading the toml file yields the same result as the JSON counterpart.
| Result (cpython tag 3.12.4) | |
|---|---|
| toml | apostrophes-in-literal-string.toml Unbalanced quotes (line 1 column 50 char 49) five-quotes.toml Unterminated string found. Reached end of file. (line 7 column 1 char 97) dates-and-times/datetimes.toml Parsed as unexpected data. multiline-basic-str/ends-in-whitespace-escape.toml Reserved escape sequence used (line 6 column 1 char 28) 8/12 (66.67%) passed |
| tomli/tomli_w | OK, 12/12 (100%) passed |
| tomlkit | OK, 12/12 (100%) passed |
| rtoml | OK, 12/12 (100%) passed |
| qtoml | apostrophes-in-literal-string.toml Didn't find expected newline (line 3, column 3) five-quotes.toml Didn't find expected newline (line 3, column 3) dates-and-times/datetimes.toml Didn't find expected newline (line 1, column 19) 9/12 (75.00%) passed |
| tomllib | OK, 12/12 (100%) passed |
| tomlclass | OK, 12/12 (100%) passed |
Test the compliance with python tomllib test data (since python 3.11) for invalid toml files here:
https://github.com/python/cpython/tree/main/Lib/test/test_tomllib/data/invalid
Not OK: The toml file is parsed without error, but expected to fail.OK: All files are failed to parse, as expected. Showing the last parsing error.
| Result (cpython tag 3.12.4) | |
|---|---|
| toml | Not OK: invalid-comment-char.toml incorrectly parsed. Not OK: array/file-end-after-val.toml incorrectly parsed. Not OK: array/unclosed-after-item.toml incorrectly parsed. Not OK: array/unclosed-empty.toml incorrectly parsed. Not OK: dotted-keys/extend-defined-table-with-subtable.toml incorrectly parsed. Not OK: dotted-keys/extend-defined-table.toml incorrectly parsed. Not OK: inline-table/overwrite-value-in-inner-table.toml incorrectly parsed. Not OK: inline-table/unclosed-empty.toml incorrectly parsed. Not OK: multiline-basic-str/carriage-return.toml incorrectly parsed. 41/50 (82.00%) passed |
| tomli/tomli_w | OK: table/redefine-2.toml Cannot declare ('t1', 't2', 't3') twice (at line 3, column 10) 50/50 (100%) passed |
| tomlkit | Not OK: multiline-basic-str/carriage-return.toml incorrectly parsed. 49/50 (98.00%) passed |
| rtoml | Not OK: multiline-basic-str/carriage-return.toml incorrectly parsed. 49/50 (98.00%) passed |
| qtoml | Not OK: invalid-comment-char.toml incorrectly parsed. Not OK: non-scalar-escaped.toml incorrectly parsed. Not OK: dotted-keys/extend-defined-table-with-subtable.toml incorrectly parsed. Not OK: dotted-keys/extend-defined-table.toml incorrectly parsed. Not OK: inline-table/override-val-with-table.toml incorrectly parsed. Not OK: inline-table/overwrite-value-in-inner-table.toml incorrectly parsed. Not OK: multiline-basic-str/carriage-return.toml incorrectly parsed. Not OK: table/redefine-1.toml incorrectly parsed. Not OK: table/redefine-2.toml incorrectly parsed. 41/50 (82.00%) passed |
| tomllib | OK: table/redefine-2.toml Cannot declare ('t1', 't2', 't3') twice (at line 3, column 10) 50/50 (100%) passed |
| tomlclass | Not OK: multiline-basic-str/carriage-return.toml incorrectly parsed. 49/50 (98.00%) passed |
Test the speed of loading and dumping the loaded using data
provided by rtoml
https://github.com/samuelcolvin/rtoml/raw/main/tests/data.toml
| Loading speed | Dumping speed | |
|---|---|---|
| toml | Excluded (heterogeneous arrays not supported) | Excluded (heterogeneous arrays not supported) |
| tomli/tomli_w | 1.03s (5000 iterations) | 1.06s (5000 iterations) |
| tomlkit | 35.32s (5000 iterations) | 1.43s (5000 iterations) |
| rtoml | 0.59s (5000 iterations) | 0.15s (5000 iterations) |
| qtoml | 6.52s (5000 iterations) | 2.70s (5000 iterations) |
| tomllib | 2.72s (5000 iterations) | Dumping not supported |
| tomlclass | 3.45s (5000 iterations) | 3.79s (5000 iterations) |
Test the speed of loading and dumping the loaded using data
provided by tomli
https://github.com/hukkin/tomli/raw/master/benchmark/data.toml
| Loading speed | Dumping speed | |
|---|---|---|
| toml | Excluded (heterogeneous arrays not supported) | Excluded (heterogeneous arrays not supported) |
| tomli/tomli_w | 0.72s (5000 iterations) | 0.67s (5000 iterations) |
| tomlkit | 15.04s (5000 iterations) | 0.71s (5000 iterations) |
| rtoml | 0.46s (5000 iterations) | 0.23s (5000 iterations) |
| qtoml | 4.63s (5000 iterations) | 1.79s (5000 iterations) |
| tomllib | 1.86s (5000 iterations) | Dumping not supported |
| tomlclass | 2.46s (5000 iterations) | 2.46s (5000 iterations) |
pip install -U toml-benchtoml-benchtoml-bench --datadir /tmp/toml-benchtoml-bench --report ./README.mdtoml-bench --comver 1.0.0toml-bench --iter 5000git clone https://github.com/pwwang/toml-bench.git
cd toml-bench
# See https://python-poetry.org/docs/cli/#add
# for how to specify a version constraint
poetry add "tomli=2.0.0"
poetry update
poetry install
poetry run toml-bench