pydantic/monty · error

expected list

Error message

expected list

What it means

A test-helper panic in Monty's list tests: after reading a HeapId the code asserts it is a List before calling `py_setitem`. It fires only if the heap entry under test resolves to something other than a List, meaning the test fixture or heap setup is wrong. It is test code, not production behavior.

Source

Thrown at crates/monty/src/types/list.rs:1150

            create_heap_with_list_and_longint(vec![Value::Int(10), Value::Int(20), Value::Int(30)], BigInt::from(1));
        let mut interns = create_test_interns();
        let code = Code::empty();

        let key = Value::Ref(index_id);
        let new_value = Value::Int(99);
        heap.inc_ref(index_id);

        let result = HeapReader::with(&mut heap, &mut (&code, &mut interns), |reader, (code, interns)| {
            let mut vm = VM::new(
                Vec::new(),
                code,
                reader,
                interns,
                PrintWriter::Disabled,
                VmEnv::default(),
            );
            let HeapReadOutput::List(mut list) = vm.heap.read(list_id) else {
                panic!("expected list");
            };
            list.py_setitem(key, new_value, &mut vm)
        });

        assert!(result.is_ok());

        // Verify the list was updated by checking it matches expected Int value
        let HeapData::List(list) = heap.get(list_id) else {
            panic!("expected list");
        };
        assert!(matches!(list.as_slice()[1], Value::Int(99)));

        // Clean up
        Value::Ref(list_id).drop_with(&mut heap);
    }

    /// Tests py_setitem with a negative LongInt index that fits in i64.
    #[test]

View on GitHub (pinned to adc986b362)

Solutions

  1. Check that the test fixture allocates `list_id` with `HeapData::List`, not another variant.
  2. Re-read the surrounding test to confirm list_id refers to the list, not the key or value.
  3. Rebuild the crate (`cargo build -p monty`) to rule out stale test artifacts.

Example fix

// before
let list_id = vm.heap.allocate(HeapData::Tuple(items), &interns)?; // wrong variant
// after
let list_id = vm.heap.allocate(HeapData::List(Vec::new()), &interns)?;
Defensive patterns

Strategy: validation

Validate before calling

debug_assert!(matches!(vm.heap.read(list_id), HeapReadOutput::List(_)), "fixture must allocate a List for list_id");

Type guard

fn as_list(read: &HeapReadOutput<'_>) -> Option<&HeapRead<'_, List>> {
    match read { HeapReadOutput::List(l) => Some(l), _ => None }
}

Prevention

When it happens

Trigger: Running the list `py_setitem` unit test (`py_setitem_longint_fits_in_i64`) when `list_id` was allocated as a non-List value or the heap read returns a different variant after test refactoring.

Common situations: Seen by contributors editing list tests, changing heap entry allocation in the fixture, or renaming heap variants so a fixture allocates the wrong type.

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