Visitar URL original
Forthcoming changes in TOML v1.1.0 will cause TOMLDecodeError. tomllib.loads doesn't support optional seconds in times. · Issue #103992 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260227154408/https://github.com/python/cpython/issues/103992
Skip to content
/ cpython Public

Forthcoming changes in TOML v1.1.0 will cause TOMLDecodeError. tomllib.loads doesn't support optional seconds in times.  #103992

@JamesParrott

Description

@JamesParrott

[Edit] I erroneously assumed the "minimal configuration file format", that I personally find to be a pleasant alternative to JSON, was done and dusted. But TOML is still very much under active development. On its main branch.

Python 3.12 is compliant with TOML v1.0.0 as far as I'm aware. But optional seconds, and other issues will arise if TOML v1.1.0 files are attempted to be loaded with tomllib. This is absolutely not a request to support TOML v1.1.0.

This issue is an informational advanced warning only, of changes coming down the line. Feel free to decide not to support them all (TOML v1.1 is completely unnecessary for my projects, and I won't be supporting it). But if so, it's still good to make a decision, and record that somewhere. If nothing else so it is clear what should be included in test data.

Background: In summer last year (2022) the TOML language's latest version relaxed the requirement for seconds to be specified in a time: toml-lang/toml@7e8a748

A test for optional seconds was also added to toml-test's development version: https://github.com/BurntSushi/toml-test/blob/master/tests/valid/datetime/no-seconds.toml

Implications: In Python 3.12, tomllib requires seconds to be present. Currently in the absence of seconds, the regex _TIME_RE_STR does not match. Otherwise both local variables called sec_str could not be defined. So if others decide to support TOML v1.1, this will affects all platforms.

Regex:

_TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"

sec_str:
hour, minute, sec = int(hour_str), int(minute_str), int(sec_str)

sec_str:
return time(int(hour_str), int(minute_str), int(sec_str), micros)

Bug report

Python 3.12.0a7 (tags/v3.12.0a7:b861ba4, Apr  4 2023, 16:33:41) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tomllib
>>> tomllib.loads('t=12:00:00\n')
{'t': datetime.time(12, 0)}
>>> tomllib.loads('t=12:00\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python312\Lib\tomllib\_parser.py", line 127, in loads
    raise suffixed_err(
tomllib.TOMLDecodeError: Expected newline or end of document after a statement (at line 1, column 5)
>>>

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions