Skip to content

Commit bb917d8

Browse files
hukkinencukouStanFromIreland
authored
gh-142956: Update tomllib to parse TOML 1.1.0 (#144243)
Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent 74c1f41 commit bb917d8

File tree

18 files changed

+182
-85
lines changed

18 files changed

+182
-85
lines changed

Doc/library/tomllib.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
.. module:: tomllib
55
:synopsis: Parse TOML files.
66

7-
.. versionadded:: 3.11
8-
97
.. moduleauthor:: Taneli Hukkinen
108
.. sectionauthor:: Taneli Hukkinen
119

1210
**Source code:** :source:`Lib/tomllib`
1311

1412
--------------
1513

16-
This module provides an interface for parsing TOML 1.0.0 (Tom's Obvious Minimal
14+
This module provides an interface for parsing TOML 1.1.0 (Tom's Obvious Minimal
1715
Language, `https://toml.io <https://toml.io/en/>`_). This module does not
1816
support writing TOML.
1917

18+
.. versionadded:: 3.11
19+
The module was added with support for TOML 1.0.0.
20+
21+
.. versionchanged:: next
22+
Added TOML 1.1.0 support.
23+
See the :ref:`What's New <whatsnew315-tomllib-1-1-0>` for details.
24+
25+
2026
.. seealso::
2127

2228
The :pypi:`Tomli-W package <tomli-w>`

Doc/whatsnew/3.15.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,56 @@ tkinter
867867
with outdated names.
868868
(Contributed by Serhiy Storchaka in :gh:`143754`)
869869

870+
871+
.. _whatsnew315-tomllib-1-1-0:
872+
873+
tomllib
874+
-------
875+
876+
* The :mod:`tomllib` module now supports TOML 1.1.0.
877+
This is a backwards compatible update, meaning that all valid TOML 1.0.0
878+
documents are parsed the same way.
879+
880+
The changes, according to the `official TOML changelog`_, are:
881+
882+
- Allow newlines and trailing commas in inline tables.
883+
884+
Previously an inline table had to be on a single line and couldn't end
885+
with a trailing comma. This is now relaxed so that the following is valid:
886+
887+
.. syntax highlighting needs TOML 1.1.0 support in Pygments,
888+
see https://github.com/pygments/pygments/issues/3026
889+
890+
.. code-block:: text
891+
892+
tbl = {
893+
key = "a string",
894+
moar-tbl = {
895+
key = 1,
896+
},
897+
}
898+
899+
- Add ``\xHH`` notation to basic strings for codepoints under 255,
900+
and the ``\e`` escape for the escape character:
901+
902+
.. code-block:: text
903+
904+
null = "null byte: \x00; letter a: \x61"
905+
csi = "\e["
906+
907+
- Seconds in datetime and time values are now optional.
908+
The following are now valid:
909+
910+
.. code-block:: text
911+
912+
dt = 2010-02-03 14:15
913+
t = 14:15
914+
915+
(Contributed by Taneli Hukkinen in :gh:`142956`.)
916+
917+
.. _official TOML changelog: https://github.com/toml-lang/toml/blob/main/CHANGELOG.md
918+
919+
870920
types
871921
------
872922

Lib/test/test_tomllib/burntsushi.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,8 @@
77
import datetime
88
from typing import Any
99

10-
# Aliases for converting TOML compliance format [1] to BurntSushi format [2]
11-
# [1] https://github.com/toml-lang/compliance/blob/db7c3211fda30ff9ddb10292f4aeda7e2e10abc4/docs/json-encoding.md # noqa: E501
12-
# [2] https://github.com/BurntSushi/toml-test/blob/4634fdf3a6ecd6aaea5f4cdcd98b2733c2694993/README.md # noqa: E501
13-
_aliases = {
14-
"boolean": "bool",
15-
"offset datetime": "datetime",
16-
"local datetime": "datetime-local",
17-
"local date": "date-local",
18-
"local time": "time-local",
19-
}
20-
21-
22-
def convert(obj): # noqa: C901
10+
11+
def convert(obj):
2312
if isinstance(obj, str):
2413
return {"type": "string", "value": obj}
2514
elif isinstance(obj, bool):
@@ -53,31 +42,25 @@ def convert(obj): # noqa: C901
5342
def normalize(obj: Any) -> Any:
5443
"""Normalize test objects.
5544
56-
This normalizes primitive values (e.g. floats), and also converts from
57-
TOML compliance format [1] to BurntSushi format [2].
58-
59-
[1] https://github.com/toml-lang/compliance/blob/db7c3211fda30ff9ddb10292f4aeda7e2e10abc4/docs/json-encoding.md # noqa: E501
60-
[2] https://github.com/BurntSushi/toml-test/blob/4634fdf3a6ecd6aaea5f4cdcd98b2733c2694993/README.md # noqa: E501
61-
"""
45+
This normalizes primitive values (e.g. floats)."""
6246
if isinstance(obj, list):
6347
return [normalize(item) for item in obj]
6448
if isinstance(obj, dict):
6549
if "type" in obj and "value" in obj:
6650
type_ = obj["type"]
67-
norm_type = _aliases.get(type_, type_)
6851
value = obj["value"]
69-
if norm_type == "float":
52+
if type_ == "float":
7053
norm_value = _normalize_float_str(value)
71-
elif norm_type in {"datetime", "datetime-local"}:
54+
elif type_ in {"datetime", "datetime-local"}:
7255
norm_value = _normalize_datetime_str(value)
73-
elif norm_type == "time-local":
56+
elif type_ == "time-local":
7457
norm_value = _normalize_localtime_str(value)
7558
else:
7659
norm_value = value
7760

78-
if norm_type == "array":
61+
if type_ == "array":
7962
return [normalize(item) for item in value]
80-
return {"type": norm_type, "value": norm_value}
63+
return {"type": type_, "value": norm_value}
8164
return {k: normalize(v) for k, v in obj.items()}
8265
raise AssertionError("Burntsushi fixtures should be dicts/lists only")
8366

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"local-dt": {"type":"datetime-local","value":"1988-10-27t01:01:01"},
3+
"local-dt-no-seconds": {"type":"datetime-local","value":"2025-04-18t20:05:00"},
34
"zulu-dt": {"type":"datetime","value":"1988-10-27t01:01:01z"}
45
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
local-dt=1988-10-27t01:01:01
2+
local-dt-no-seconds=2025-04-18T20:05
23
zulu-dt=1988-10-27t01:01:01z
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
{"t":
2-
{"type":"time-local","value":"00:00:00.999999"}}
2+
{"type":"time-local","value":"00:00:00.999999"},
3+
"t2":
4+
{"type":"time-local","value":"00:00:00"}}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
t=00:00:00.99999999999999
1+
t=00:00:00.99999999999999
2+
t2=00:00

Lib/test/test_tomllib/data/valid/empty-inline-table.json renamed to Lib/test/test_tomllib/data/valid/inline-table/empty-inline-table.json

File renamed without changes.

Lib/test/test_tomllib/data/valid/empty-inline-table.toml renamed to Lib/test/test_tomllib/data/valid/inline-table/empty-inline-table.toml

File renamed without changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"multiline": {
3+
"a": {
4+
"type": "integer",
5+
"value": "1"
6+
},
7+
"b": {
8+
"type": "integer",
9+
"value": "2"
10+
},
11+
"c": [
12+
{
13+
"type": "integer",
14+
"value": "1"
15+
},
16+
{
17+
"type": "integer",
18+
"value": "2"
19+
},
20+
{
21+
"type": "integer",
22+
"value": "3"
23+
}
24+
],
25+
"d": {
26+
"type": "integer",
27+
"value": "3"
28+
},
29+
"e": {
30+
"type": "integer",
31+
"value": "4"
32+
},
33+
"f": {}
34+
}
35+
}

0 commit comments

Comments
 (0)