Use a TOML parser only for supported manifest fields

Parse TOML, then enforce the manifest contract

tomllib.loads answers whether text is syntactically TOML and returns nested Python mappings. It does not decide whether a product supports project.name. The script reads that mapping and name; a production rule should check the permitted keys and value type before consuming them. This separates malformed input, which raises TOMLDecodeError, from a well-formed but unsupported manifest field.

The temporary directory is incidental to the isolated fixture; loads receives the supplied string. This note validates neither versions nor cross-field requirements. Use tomllib.load for an already-open binary TOML file.

Example

from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as d:
    import tomllib
    data = tomllib.loads('[project]\nname="demo"')
    assert data['project']['name'] == 'demo'
    result = 'field=project.name'
    print(result)

Expected stdout:

field=project.name

Sources

- tomllib.loads

Prepared with AI assistance. The example uses synthetic data; its stated limits apply.

Comments

Popular posts from this blog

Compare Two Text Files in Python Without Modifying Them

Seven CSV Quality Checks to Run Before Importing Data

Explain why SQLite transactions cannot make an HTTP call atomic