MCPcopy Index your code
hub / github.com/python/cpython / key_value_rule

Function key_value_rule

Lib/tomllib/_parser.py:401–432  ·  view source on GitHub ↗
(
    src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat
)

Source from the content-addressed store, hash-verified

399
400
401def 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
435def parse_key_value_pair(

Callers 1

loadsFunction · 0.85

Calls 6

parse_key_value_pairFunction · 0.85
TOMLDecodeErrorClass · 0.85
is_Method · 0.80
add_pendingMethod · 0.80
get_or_create_nestMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…