pydantic/monty · error

dict_items view must reference a dict

Error message

dict_items view must reference a dict

What it means

Internal panic in the dict-items view `py_contains_impl` (the `in` operator on `dict.items()`). After extracting a candidate (key, value) pair, the code reads the view's backing dict; if the HeapId does not resolve to a Dict, an invariant is broken. Normal membership tests never trigger it.

Source

Thrown at crates/monty/src/types/dict_view.rs:440

    }
}

impl<'h> PyTrait<'h> for HeapObjectRead<'h, DictItemsView> {
    fn py_is_iterable(&self, _vm: &VM<'h>) -> bool {
        true
    }

    /// Membership takes a `(key, value)` probe: look the key up, then compare
    /// the stored value. A non-pair probe can never be a member.
    fn py_contains_impl(&self, item: &Value, vm: &mut VM<'h>) -> RunResult<Option<bool>> {
        let dict_id = self.get(vm.heap).dict_id();
        let Some((key, value)) = cloned_items_view_candidate(item, vm) else {
            return Ok(Some(false));
        };
        defer_drop!(key, vm);
        defer_drop!(value, vm);
        let HeapReadOutput::Dict(dict) = vm.heap.read(dict_id) else {
            panic!("dict_items view must reference a dict");
        };
        match dict.dict_get(key, vm) {
            Ok(Some(existing_value)) => {
                // Stored value on the left, as CPython's `dictitems_contains` does.
                let result = existing_value.py_eq(value, vm);
                existing_value.drop_with(vm);
                result.map(Some)
            }
            Ok(None) => Ok(Some(false)),
            Err(e) => Err(e),
        }
    }

    fn py_type(&self, _vm: &VM<'h>) -> Type {
        Type::DictItems
    }

    fn py_iter(&self, vm: &mut VM<'h>) -> RunResult<Value> {

View on GitHub (pinned to adc986b362)

Solutions

  1. Report the reproducing case to the Monty project as a bug.
  2. Trace how the view's dict_id was produced and whether the referenced entry was freed or replaced.
  3. Audit `py_dec_ref_ids` / drop paths for the view and its backing dict.
Defensive patterns

Strategy: try-catch

Try / catch

let HeapReadOutput::Dict(dict) = vm.heap.read(dict_id) else {
    return Err(internal_error("dict_items view must reference a dict"));
};

Prevention

When it happens

Trigger: Evaluating `pair in dict.items()` (or `set <= dict.items()` style comparisons) when the items view's dict_id resolves to a non-Dict heap entry — reachable only through an interpreter bug or heap corruption.

Common situations: Seen by Monty developers working on heap entry reuse, view lifetime, or cycle-collection changes; unreachable from correct sandboxed Python execution.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of pydantic/monty@adc986b362 (2026-09-13). Data as JSON: /api/errors/4164cd81792be54a. Report an issue: GitHub.