pydantic/monty · error

dict_values view must always reference a dict

Error message

dict_values view must always reference a dict

What it means

Internal panic in the `DictValuesView::dict()` helper: a dict-values view must always reference a Dict heap entry via its `dict_id`. The panic fires if the read returns any other heap object variant, meaning the view invariant is broken. This is not a user-facing error.

Source

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

    }

    /// Returns the underlying dictionary heap id.
    #[must_use]
    pub fn dict_id(self) -> HeapId {
        self.dict_id
    }
}

impl DictView for DictValuesView {
    fn dict_id(&self) -> HeapId {
        self.dict_id
    }
}

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);

View on GitHub (pinned to adc986b362)

Solutions

  1. Report the failing script and panic message to Monty maintainers.
  2. Check that DictValuesView is only constructed with a live Dict HeapId.
  3. Run the memory-model-checks feature over the relevant tests to hunt refcount bugs.
Defensive patterns

Strategy: try-catch

Try / catch

match vm.heap.read(dict_id) {
    HeapReadOutput::Dict(d) => d,
    _ => return Err(internal_error("dict_values view lost its backing dict")),
}

Prevention

When it happens

Trigger: Any values-view operation that resolves the backing dict through the private `dict()` accessor (iteration, `len`, `contains`) when the stored dict_id resolves to a non-Dict entry — only via interpreter bugs or heap corruption.

Common situations: Hit by Monty contributors while changing heap allocation, view construction, or refcount handling; not reachable from sandboxed Python under correct operation.

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/576aecb50d4a8cd4. Report an issue: GitHub.