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