pydantic/monty · error

StaticStrings serialization cannot fail

Error message

StaticStrings serialization cannot fail

What it means

An internal `expect` in the test-only `static_strings_fingerprint()`, asserting that postcard serialization of each `StaticStrings` variant into a byte vector cannot fail. Postcard serialization into `to_allocvec` is infallible for these simple enum/static-str payloads, so the assertion documents that no error path needs handling. It fires only if postcard itself returned an error, which would indicate a broken serializer or an unexpectedly complex payload, never user input.

Source

Thrown at crates/monty/src/intern.rs:1245

        {
            *hash ^= u64::from(byte);
            *hash = hash.wrapping_mul(PRIME);
        }
        for byte in bytes {
            *hash ^= u64::from(*byte);
            *hash = hash.wrapping_mul(PRIME);
        }
    }

    let mut hash = OFFSET_BASIS;
    for value in StaticStrings::iter() {
        update(&mut hash, &(value as u16).to_le_bytes());
        update(&mut hash, format!("{value:?}").as_bytes());
        let string: &'static str = value.into();
        update(&mut hash, string.as_bytes());
        update(
            &mut hash,
            &postcard::to_allocvec(&value).expect("StaticStrings serialization cannot fail"),
        );
    }
    hash
}

impl StaticStrings {
    /// Attempts to convert a `StringId` back to a `StaticStrings` variant.
    ///
    /// Returns `None` if the `StringId` doesn't correspond to a static string
    /// (e.g., it's an ASCII char or a dynamically interned string).
    pub fn from_string_id(id: StringId) -> Option<Self> {
        u16::try_from(id.0).ok().and_then(Self::from_repr)
    }
}

/// Converts this static string variant to its corresponding `StringId`.
impl From<StaticStrings> for StringId {
    fn from(value: StaticStrings) -> Self {

View on GitHub (pinned to adc986b362)

Solutions

  1. postcard serialization into an allocator-backed Vec of static enum data cannot fail (no io, no size cap); on failure the only cause is a postcard incompatibility, so re-examine the Serialize impls of StaticStrings variants rather than handling the error.
  2. No runtime fallback exists; the fingerprint function is test-only, so a failure should surface as a panic in tests and be fixed at the source.

When it happens

Trigger: Thrown at crates/monty/src/intern.rs:1245 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/3d6fcc8861a7194b. Report an issue: GitHub.