ADR 0073: A dict's values may be containers¶
Status: Final Type: Feature Created: 2026-11-06 Amends: 0030 (a dict value may be a list or a set, and a deep copy copies a dict's values) Related-ADRs: 0057 (a list of lists and a list of sets, the model this follows), 0024 (the release walk and the cycle policy), 0031 (member types from a position)
Context¶
ADR 0030's members had no outgoing reference: a dict's keys and values were numbers, strs, bytes, tuples of those, and later instances. A container as a value was refused on ADR 0024's grounds, a cascading free that could recurse and a constructible reference cycle. ADR 0057 found both grounds expired for a list of lists; they have expired here too. A static type nests finitely, so a release recurses at most as deep as the annotation is written. An instance it reaches is queued. A cycle through an instance leaks and is reported, which ADR 0024 already ships as the policy for the other containers that hold instances.
Shed Skin's sudoku5 keeps the exact-cover matrix as dict[tuple[str, tuple[int, int]], set[tuple[int, int, int]]] and the rows as dict[tuple[int, int, int], list[tuple[str, tuple[int, int]]]]. Grouping into dict[str, list[str]] with setdefault(k, []).append(v) is among the commonest shapes in Python. The owner decided that a dict's values may be containers (A188).
Decision¶
- A dict value may be a list or a set of any member type a list or a set holds. The value position is a dtype of its own, the one ADR 0057 minted for a list's element:
nested_list_dtype(T)forlist[T],set_elem_dtype(T)forset[T]. A read of one gives a list or a set with those members, recorded on the value as for any container. - The map runtime owns them: two more ownership codes, 4 for a list and 5 for a set, released and retained as the instance code 2 is, and two value families,
lstandset, for the put and the read. A read borrows, as container reads do; a store retains the new value before releasing the old. - Every operation a dict value has takes them:
d[k]andd[k] = v, a literal, a comprehension,getwith and without a default (alist[T] | None),setdefault,pop,del,items(),values(),print,copyandcopy.deepcopy. An empty[]orset()stored into the position takes the position's member types, as ADR 0031 has an empty container take them from any declared position. - A deep copy copies the values.
copy.deepcopyof a dict copies the values through the deep copy's memo, and keeps the keys. This also corrects instance values, which a deep copy shared until now (notes/accept-then-wrong.md§78). A key or a set element that is a number, a str,bytesor a tuple of those is kept, as CPython keeps an immutable object. One that is an instance, which CPython copies with its identity hash, is refused where the copy is visible and stops the program where it is not. - Not yet: a dict value that is a dict or a tuple, which the decision covers and which need a position dtype and a value family of their own;
==between dicts.
Consequences¶
tests/b_integration/test_dict_container_values.pyholds the operations against CPython on all backends in both modes with the leak counters at zero, andtest_asan.pya program that aliases, replaces, pops and drops values 300 times.- With a tuple holding a tuple as a key and a list element,
itertools.product(ADR 0059), a dict's list value read as a list in a subscript chain, and the annotations in the census table, sudoku5 compiles and prints CPython's output but for the timing line on c and llvm; qbe stops at its 64-move limit (A165). 26 of the census's 28 programs compile.
Alternatives considered¶
- A value position recorded beside the dict: a second record per dict, where ADR 0057's position dtypes already say what the list runtime holds and are compared by identity.
- Keeping the refusal: the grounds for it had expired, as ADR 0057 found for a list.