pydantic/monty · error

usize fits in u128

Error message

usize fits in u128

What it means

An internal invariant `expect` in `ObjectIdentity::encoded`, the implementation behind Python's `id()`. The identity payload is accumulated into a `u128` from components (heap index, zigzagged int, float bits, byte payloads), and the assertion states that a `usize` heap index always fits in the 128-bit accumulator — trivially true on every supported platform. It fires only if the invariant is violated, i.e. never in correct builds; there is no user-controllable input that can trigger it.

Source

Thrown at crates/monty/src/identity.rs:104

    }

    /// Builds the identity of an arena-allocated object.
    pub(crate) fn from_heap_id(id: HeapId) -> Self {
        Self::Heap(id.index())
    }

    /// Encodes this key as the nonnegative integer exposed by Python's `id()`.
    pub(crate) fn encoded(&self) -> u128 {
        let payload = match self {
            Self::Undefined | Self::Ellipsis | Self::NotImplemented | Self::None => 0,
            Self::Bool(value) => u128::from(*value),
            Self::Int(value) => u128::from(zigzag_i64(*value)),
            Self::Float(bits) => u128::from(compact_float_bits(*bits)),
            Self::InternString(index)
            | Self::InternBytes(index)
            | Self::InternLongInt(index)
            | Self::DefFunction(index)
            | Self::Heap(index) => u128::try_from(*index).expect("usize fits in u128"),
            Self::Builtin(value) => fixed_serde_payload(value),
            Self::ModuleFunction(value) => fixed_serde_payload(value),
            Self::Marker(value) => fixed_serde_payload(value),
            Self::Property(value) => fixed_serde_payload(value),
        };
        (payload << TAG_BITS) | u128::from(self.tag())
    }

    /// Encodes this key as a Python integer.
    ///
    /// Returns an immediate integer when possible, otherwise allocating a `LongInt`.
    pub(crate) fn into_value(self, heap: &Heap) -> Value {
        LongInt::value_from_u128(self.encoded(), heap)
    }

    /// Returns the stable low-bit category tag for this identity variant.
    fn tag(&self) -> u8 {
        match self {

View on GitHub (pinned to adc986b362)

Solutions

  1. This expect fires only if a usize exceeds u128, impossible on all supported targets since usize is at most 64 bits; no recovery needed and none is possible.
  2. If it ever fires, it indicates a compile-target assumption violation: audit the platform's usize width and the encoded() layout rather than handling at runtime.

When it happens

Trigger: Thrown at crates/monty/src/identity.rs:104 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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