Question presented to candidate: "Your service keeps its settings in a TOML file, and you are on Python 3.11. How do you read it with the standard library, what do you get back, and what can trip you up?"
What a strong answer should cover:
tomllibjoined the standard library in Python 3.11 (PEP 680);tomllib.load(f)reads a file opened in binary mode ("rb"),tomllib.loads(s)parses astr- The result is a plain
dict: tables become nested dicts, arrays become lists, arrays of tables ([[x]]) become lists of dicts, and dates and times becomedatetimeobjects - Invalid documents raise
tomllib.TOMLDecodeError, aValueErrorsubclass, with line and column; duplicate keys are errors, unlike JSON tomllibis read-only: there is nodump; use the third-partytomli-wto write ortomlkitto edit while keeping comments- Practical patterns:
parse_float=Decimal, defaults merged with|or.get, andtomlias a fallback on Python 3.10 and older - Typical use: reading
pyproject.tomlor an app config at start-up; TOML has no null, so a missing key means "not set"
Code / implementation expected: Yes β with open(path, "rb") as f: config = tomllib.load(f) and reading a nested value.