Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-142956: Updatetomllib to parse TOML 1.1.0#144243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
hukkin wants to merge8 commits intopython:main
base:main
Choose a base branch
Loading
fromhukkin:toml-1.1
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletionDoc/library/tomllib.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,10 +13,14 @@

--------------

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

.. versionchanged:: next
Module updated to support TOML 1.1.0. Initially the module supported TOML 1.0.0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Would it be possible to elaborate changes between TOML 1.0 and TOML 1.1? Maybe just give some examples.

I suggest to document this change in What's New in Python 3.15.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

That's a good point. I suggest:

  • Copying the first four points fromTOML changelog to What's New (with attribution link)
  • Linking to the What's New entry from here. (In the long term, a changelog will be increasingly distracting.)



.. seealso::

The :pypi:`Tomli-W package <tomli-w>`
Expand Down
33 changes: 8 additions & 25 deletionsLib/test/test_tomllib/burntsushi.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,19 +7,8 @@
importdatetime
fromtypingimportAny

# Aliases for converting TOML compliance format [1] to BurntSushi format [2]
# [1] https://github.com/toml-lang/compliance/blob/db7c3211fda30ff9ddb10292f4aeda7e2e10abc4/docs/json-encoding.md # noqa: E501
# [2] https://github.com/BurntSushi/toml-test/blob/4634fdf3a6ecd6aaea5f4cdcd98b2733c2694993/README.md # noqa: E501
_aliases= {
"boolean":"bool",
"offset datetime":"datetime",
"local datetime":"datetime-local",
"local date":"date-local",
"local time":"time-local",
}


defconvert(obj):# noqa: C901

defconvert(obj):
ifisinstance(obj,str):
return {"type":"string","value":obj}
elifisinstance(obj,bool):
Expand DownExpand Up@@ -53,31 +42,25 @@ def convert(obj): # noqa: C901
defnormalize(obj:Any)->Any:
"""Normalize test objects.
This normalizes primitive values (e.g. floats), and also converts from
TOML compliance format [1] to BurntSushi format [2].
[1] https://github.com/toml-lang/compliance/blob/db7c3211fda30ff9ddb10292f4aeda7e2e10abc4/docs/json-encoding.md # noqa: E501
[2] https://github.com/BurntSushi/toml-test/blob/4634fdf3a6ecd6aaea5f4cdcd98b2733c2694993/README.md # noqa: E501
"""
This normalizes primitive values (e.g. floats)."""
ifisinstance(obj,list):
return [normalize(item)foriteminobj]
ifisinstance(obj,dict):
if"type"inobjand"value"inobj:
type_=obj["type"]
norm_type=_aliases.get(type_,type_)
value=obj["value"]
ifnorm_type=="float":
iftype_=="float":
norm_value=_normalize_float_str(value)
elifnorm_typein {"datetime","datetime-local"}:
eliftype_in {"datetime","datetime-local"}:
norm_value=_normalize_datetime_str(value)
elifnorm_type=="time-local":
eliftype_=="time-local":
norm_value=_normalize_localtime_str(value)
else:
norm_value=value

ifnorm_type=="array":
iftype_=="array":
return [normalize(item)foriteminvalue]
return {"type":norm_type,"value":norm_value}
return {"type":type_,"value":norm_value}
return {k:normalize(v)fork,vinobj.items()}
raiseAssertionError("Burntsushi fixtures should be dicts/lists only")

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
{
"local-dt": {"type":"datetime-local","value":"1988-10-27t01:01:01"},
"local-dt-no-seconds": {"type":"datetime-local","value":"2025-04-18t20:05:00"},
"zulu-dt": {"type":"datetime","value":"1988-10-27t01:01:01z"}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
local-dt=1988-10-27t01:01:01
local-dt-no-seconds=2025-04-18T20:05
zulu-dt=1988-10-27t01:01:01z
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
{"t":
{"type":"time-local","value":"00:00:00.999999"}}
{"type":"time-local","value":"00:00:00.999999"},
"t2":
{"type":"time-local","value":"00:00:00"}}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
t=00:00:00.99999999999999
t=00:00:00.99999999999999
t2=00:00
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
{
"multiline": {
"a": {
"type": "integer",
"value": "1"
},
"b": {
"type": "integer",
"value": "2"
},
"c": [
{
"type": "integer",
"value": "1"
},
{
"type": "integer",
"value": "2"
},
{
"type": "integer",
"value": "3"
}
],
"d": {
"type": "integer",
"value": "3"
},
"e": {
"type": "integer",
"value": "4"
},
"f": {}
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
multiline = {
"a" =1,"b" =2,
c = [
1,
2,
3,
],# comment
d =3,
e =4,f = {
# comment
},
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
{
"escape": {"type":"string","value":"\u001B"},
"tab": {"type":"string","value":"\t"},
"upper-j": {"type":"string","value":"J"},
"upper-j-2": {"type":"string","value":"J"}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
escape = "\e"
tab = "\x09"
upper-j = "\x4a"
upper-j-2 = "\x4A"
19 changes: 1 addition & 18 deletionsLib/test/test_tomllib/test_data.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,12 +8,6 @@

from . import burntsushi, tomllib


class MissingFile:
def __init__(self, path: Path):
self.path = path


DATA_DIR = Path(__file__).parent / "data"

VALID_FILES = tuple((DATA_DIR / "valid").glob("**/*.toml"))
Expand All@@ -22,10 +16,7 @@ def __init__(self, path: Path):
_expected_files = []
for p in VALID_FILES:
json_path = p.with_suffix(".json")
try:
text = json.loads(json_path.read_bytes().decode())
except FileNotFoundError:
text = MissingFile(json_path)
text = json.loads(json_path.read_bytes().decode())
_expected_files.append(text)
VALID_FILES_EXPECTED = tuple(_expected_files)

Expand All@@ -49,14 +40,6 @@ def test_invalid(self):
def test_valid(self):
for valid, expected in zip(VALID_FILES, VALID_FILES_EXPECTED):
with self.subTest(msg=valid.stem):
if isinstance(expected, MissingFile):
# For a poor man's xfail, assert that this is one of the
# test cases where expected data is known to be missing.
assert valid.stem in {
"qa-array-inline-nested-1000",
"qa-table-inline-nested-1000",
}
continue
toml_str = valid.read_bytes().decode()
actual = tomllib.loads(toml_str)
actual = burntsushi.convert(actual)
Expand Down
59 changes: 32 additions & 27 deletionsLib/tomllib/_parser.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,39 +18,40 @@
TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Iterable
from typing import IO, Any
from typing import IO, Any, Final

from ._types import Key, ParseFloat, Pos

ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
ASCII_CTRL: Final = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))

# Neither of these sets include quotation mark or backslash. They are
# currently handled as separate cases in the parser functions.
ILLEGAL_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t")
ILLEGAL_MULTILINE_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t\n")
ILLEGAL_BASIC_STR_CHARS: Final = ASCII_CTRL - frozenset("\t")
ILLEGAL_MULTILINE_BASIC_STR_CHARS: Final = ASCII_CTRL - frozenset("\t\n")

ILLEGAL_LITERAL_STR_CHARS = ILLEGAL_BASIC_STR_CHARS
ILLEGAL_MULTILINE_LITERAL_STR_CHARS = ILLEGAL_MULTILINE_BASIC_STR_CHARS
ILLEGAL_LITERAL_STR_CHARS: Final = ILLEGAL_BASIC_STR_CHARS
ILLEGAL_MULTILINE_LITERAL_STR_CHARS: Final = ILLEGAL_MULTILINE_BASIC_STR_CHARS

ILLEGAL_COMMENT_CHARS = ILLEGAL_BASIC_STR_CHARS
ILLEGAL_COMMENT_CHARS: Final = ILLEGAL_BASIC_STR_CHARS

TOML_WS = frozenset(" \t")
TOML_WS_AND_NEWLINE = TOML_WS | frozenset("\n")
BARE_KEY_CHARS = frozenset(
TOML_WS: Final = frozenset(" \t")
TOML_WS_AND_NEWLINE: Final = TOML_WS | frozenset("\n")
BARE_KEY_CHARS: Final = frozenset(
"abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
)
KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset("\"'")
HEXDIGIT_CHARS = frozenset("abcdef" "ABCDEF" "0123456789")
KEY_INITIAL_CHARS: Final = BARE_KEY_CHARS | frozenset("\"'")
HEXDIGIT_CHARS: Final = frozenset("abcdef" "ABCDEF" "0123456789")

BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType(
BASIC_STR_ESCAPE_REPLACEMENTS: Final = MappingProxyType(
{
"\\b": "\u0008", # backspace
"\\t": "\u0009", # tab
"\\n": "\u000A", # linefeed
"\\f": "\u000C", # form feed
"\\r": "\u000D", # carriage return
"\\n": "\u000a", # linefeed
"\\f": "\u000c", # form feed
"\\r": "\u000d", # carriage return
"\\e": "\u001b", # escape
'\\"': "\u0022", # quote
"\\\\": "\u005C", # backslash
"\\\\": "\u005c", # backslash
}
)

Expand DownExpand Up@@ -133,7 +134,7 @@ def load(fp: IO[bytes], /, *, parse_float: ParseFloat = float) -> dict[str, Any]
return loads(s, parse_float=parse_float)


def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # noqa: C901
def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]:
"""Parse TOML from a string."""

# The spec allows converting "\r\n" to "\n", even in string
Expand DownExpand Up@@ -208,10 +209,10 @@ class Flags:
"""Flags that map to parsed keys/namespaces."""

# Marks an immutable namespace (inline array or inline table).
FROZEN = 0
FROZEN: Final = 0
# Marks a nest that has been explicitly created and can no longer
# be opened using the "[table]" syntax.
EXPLICIT_NEST = 1
EXPLICIT_NEST: Final = 1

def __init__(self) -> None:
self._flags: dict[str, dict[Any, Any]] = {}
Expand DownExpand Up@@ -257,8 +258,8 @@ def is_(self, key: Key, flag: int) -> bool:
cont = inner_cont["nested"]
key_stem = key[-1]
if key_stem in cont:
cont = cont[key_stem]
return flag incont["flags"] or flag incont["recursive_flags"]
inner_cont = cont[key_stem]
return flag ininner_cont["flags"] or flag ininner_cont["recursive_flags"]
return False


Expand DownExpand Up@@ -515,7 +516,7 @@ def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos
nested_dict = NestedDict()
flags = Flags()

pos =skip_chars(src, pos, TOML_WS)
pos =skip_comments_and_array_ws(src, pos)
if src.startswith("}", pos):
return pos + 1, nested_dict.dict
while True:
Expand All@@ -530,16 +531,18 @@ def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos
if key_stem in nest:
raise TOMLDecodeError(f"Duplicate inline table key {key_stem!r}", src, pos)
nest[key_stem] = value
pos =skip_chars(src, pos, TOML_WS)
pos =skip_comments_and_array_ws(src, pos)
c = src[pos : pos + 1]
if c == "}":
return pos + 1, nested_dict.dict
if c != ",":
raise TOMLDecodeError("Unclosed inline table", src, pos)
pos += 1
pos = skip_comments_and_array_ws(src, pos)
if src.startswith("}", pos):
return pos + 1, nested_dict.dict
if isinstance(value, (dict, list)):
flags.set(key, Flags.FROZEN, recursive=True)
pos += 1
pos = skip_chars(src, pos, TOML_WS)


def parse_basic_str_escape(
Expand All@@ -561,6 +564,8 @@ def parse_basic_str_escape(
pos += 1
pos = skip_chars(src, pos, TOML_WS_AND_NEWLINE)
return pos, ""
if escape_id == "\\x":
return parse_hex_char(src, pos, 2)
if escape_id == "\\u":
return parse_hex_char(src, pos, 4)
if escape_id == "\\U":
Expand DownExpand Up@@ -660,7 +665,7 @@ def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]:
pos += 1


def parse_value( # noqa: C901
def parse_value(
src: str, pos: Pos, parse_float: ParseFloat
) -> tuple[Pos, Any]:
try:
Expand Down
26 changes: 16 additions & 10 deletionsLib/tomllib/_re.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,16 +10,20 @@

TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any
from typing import Any, Final

from ._types import ParseFloat

# E.g.
# - 00:32:00.999999
# - 00:32:00
_TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
_TIME_RE_STR: Final = r"""
([01][0-9]|2[0-3]) # hours
:([0-5][0-9]) # minutes
(?:
:([0-5][0-9]) # optional seconds
(?:\.([0-9]{1,6})[0-9]*)? # optional fractions of a second
)?
"""

RE_NUMBER = re.compile(
RE_NUMBER: Final = re.compile(
r"""
0
(?:
Expand All@@ -38,8 +42,8 @@
""",
flags=re.VERBOSE,
)
RE_LOCALTIME= re.compile(_TIME_RE_STR)
RE_DATETIME = re.compile(
RE_LOCALTIME: Final= re.compile(_TIME_RE_STR, flags=re.VERBOSE)
RE_DATETIME: Final = re.compile(
rf"""
([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27
(?:
Expand DownExpand Up@@ -74,7 +78,8 @@ def match_to_datetime(match: re.Match[str]) -> datetime | date:
year, month, day = int(year_str), int(month_str), int(day_str)
if hour_str is None:
return date(year, month, day)
hour, minute, sec = int(hour_str), int(minute_str), int(sec_str)
hour, minute = int(hour_str), int(minute_str)
sec = int(sec_str) if sec_str else 0
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
if offset_sign_str:
tz: tzinfo | None = cached_tz(
Expand DownExpand Up@@ -103,8 +108,9 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:

def match_to_localtime(match: re.Match[str]) -> time:
hour_str, minute_str, sec_str, micros_str = match.groups()
sec = int(sec_str) if sec_str else 0
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
return time(int(hour_str), int(minute_str),int(sec_str), micros)
return time(int(hour_str), int(minute_str),sec, micros)


def match_to_number(match: re.Match[str], parse_float: ParseFloat) -> Any:
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2026 Movatter.jp