(
src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat
)
| 398 | |
| 399 | |
| 400 | def key_value_rule( |
| 401 | src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat |
| 402 | ) -> Pos: |
| 403 | pos, key, value = parse_key_value_pair(src, pos, parse_float) |
| 404 | key_parent, key_stem = key[:-1], key[-1] |
| 405 | abs_key_parent = header + key_parent |
| 406 | |
| 407 | relative_path_cont_keys = (header + key[:i] for i in range(1, len(key))) |
| 408 | for cont_key in relative_path_cont_keys: |
| 409 | # Check that dotted key syntax does not redefine an existing table |
| 410 | if out.flags.is_(cont_key, Flags.EXPLICIT_NEST): |
| 411 | raise TOMLDecodeError(f"Cannot redefine namespace {cont_key}", src, pos) |
| 412 | # Containers in the relative path can't be opened with the table syntax or |
| 413 | # dotted key/value syntax in following table sections. |
| 414 | out.flags.add_pending(cont_key, Flags.EXPLICIT_NEST) |
| 415 | |
| 416 | if out.flags.is_(abs_key_parent, Flags.FROZEN): |
| 417 | raise TOMLDecodeError( |
| 418 | f"Cannot mutate immutable namespace {abs_key_parent}", src, pos |
| 419 | ) |
| 420 | |
| 421 | try: |
| 422 | nest = out.data.get_or_create_nest(abs_key_parent) |
| 423 | except KeyError: |
| 424 | raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None |
| 425 | if key_stem in nest: |
| 426 | raise TOMLDecodeError("Cannot overwrite a value", src, pos) |
| 427 | # Mark inline table and array namespaces recursively immutable |
| 428 | if isinstance(value, (dict, list)): |
| 429 | out.flags.set(header + key, Flags.FROZEN, recursive=True) |
| 430 | nest[key_stem] = value |
| 431 | return pos |
| 432 | |
| 433 | |
| 434 | def parse_key_value_pair( |
no test coverage detected