pydantic/monty · error

dict_values view must reference a dict

Error message

dict_values view must reference a dict

What it means

Internal panic in the dict-values view `py_contains_impl`, which implements `value in dict.values()`. The view's dict_id must resolve to a Dict before the linear scan; any other heap variant means a core invariant was violated. Not reachable from correct Python code.

Source

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

impl<'h> HeapRead<'h, DictValuesView> {
    fn dict(&self, vm: &mut VM<'h>) -> HeapObjectRead<'h, Dict> {
        let HeapReadOutput::Dict(dict) = vm.heap.read(self.get(vm.heap).dict_id) else {
            panic!("dict_values view must always reference a dict");
        };
        dict
    }
}

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

    /// Values are not indexed, so this is a linear scan of the backing dict.
    fn py_contains_impl(&self, item: &Value, vm: &mut VM<'h>) -> RunResult<Option<bool>> {
        let dict_id = self.get(vm.heap).dict_id();
        let HeapReadOutput::Dict(dict) = vm.heap.read(dict_id) else {
            panic!("dict_values view must reference a dict");
        };
        let iter = dict.iter(vm)?;
        defer_drop_mut!(iter, vm);
        while let Some(value) = iter.next_value(vm)? {
            // Stored value on the left, matching CPython's iteration fallback.
            if value.py_eq(item, vm)? {
                return Ok(Some(true));
            }
        }
        Ok(Some(false))
    }

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

    fn py_iter(&self, vm: &mut VM<'h>) -> RunResult<Value> {
        let dict_id = self.get(vm.heap).dict_id();

View on GitHub (pinned to adc986b362)

Solutions

  1. File a bug report with the reproducing script and panic output.
  2. Audit how the view's dict_id was created and whether the dict entry could have been freed and reused.
  3. Verify `py_dec_ref_ids` ownership of the dict_id field on DictValuesView.
Defensive patterns

Strategy: try-catch

Try / catch

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

Prevention

When it happens

Trigger: Evaluating `x in dict.values()` when the values view's dict_id resolves to a non-Dict heap entry — only through an interpreter bug, stale HeapId, or heap corruption.

Common situations: Encountered by Monty developers modifying heap lifecycle or view code; sandboxed Python users should never trigger it.

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/2749b69486e0dd3f. Report an issue: GitHub.