Skip to content

fix(cache): avoid treating class-like __array__ refs as data primitives#9569

Merged
mscolnick merged 3 commits into
marimo-team:mainfrom
jeremytanjianle:fix/cache-array-protocol-classification
May 18, 2026
Merged

fix(cache): avoid treating class-like __array__ refs as data primitives#9569
mscolnick merged 3 commits into
marimo-team:mainfrom
jeremytanjianle:fix/cache-array-protocol-classification

Conversation

@jeremytanjianle

Copy link
Copy Markdown

📝 Summary

Closes #9563
This PR fixes a cache-classification edge case in is_data_primitive:

  • Root cause: class-like references exposing __array__ were treated as data primitives.
  • Effect: those refs were routed into data_to_buffer(...).view("uint8"), which can fail for reference/object arrays with:
    • TypeError: Cannot change data-type for array of references.
  • Fix: add an early class guard (inspect.isclass(value) -> False) in is_data_primitive.
  • Test: add focused coverage in tests/_runtime/test_primitives.py to assert class-like __array__ refs are not classified as data primitives (issue persistent_cache: class-like __array__ refs can be misclassified as data primitives and crash hash serialization #9563 case).
    Validation run:
  • uv run --group test pytest tests/_runtime/test_primitives.py -k class_like_array_protocol → passed
  • uv run --group test pytest tests -k primitives6 passed, 14 skipped, 8812 deselected
  • uv run --group test pytest tests -k cache202 passed, 40 skipped, 8590 deselected
    Skips are expected in this environment due to optional dependencies and version/platform-gated tests.

📋 Pre-Review Checklist


✅ Merge Checklist

  • I have read the contributor guidelines.
  • Documentation has been updated where applicable, including docstrings for API changes.
    • Not applicable: no public API change.
  • Tests have been added for the changes made.

@vercel

vercel Bot commented May 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment May 17, 2026 5:09am

Request Review

@github-actions

github-actions Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Architecture diagram
sequenceDiagram
    participant Cache as Cache System
    participant Primitive as is_data_primitive()
    participant Inspect as inspect module
    participant Array as __array__ Protocol

    Note over Cache,Array: Data Primitive Classification Flow

    Cache->>Primitive: Check if value is data primitive
    Primitive->>Primitive: Check if value is basic primitive (int, str, etc.)
    alt Value is basic primitive
        Primitive-->>Cache: True (early return)
    else Value is not basic primitive
        Primitive->>Inspect: Check if value is a class
        alt Value is a class
            Primitive-->>Cache: False (reject class-like types)
        else Value is not a class
            Primitive->>Array: Check for __array__ protocol
            alt Has __array__ and not class
                Primitive->>Primitive: Route to data_to_buffer().view("uint8")
                alt Array contains references/objects
                    Primitive-->>Cache: TypeError (cannot change dtype)
                else Array contains primitive types
                    Primitive-->>Cache: Success
                end
            else No __array__ protocol
                Primitive-->>Cache: False
            end
        end
    end

    Note over Cache,Inspect: Key Guard: inspect.isclass(value) prevents<br/>class-like __array__ references from entering buffer path
Loading

Re-trigger cubic

@jeremytanjianle

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@dmadisetti dmadisetti left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Nice catch! The test also looks great

@dmadisetti dmadisetti added the bug Something isn't working label May 18, 2026
@mscolnick
mscolnick merged commit 87ac7a5 into marimo-team:main May 18, 2026
42 of 44 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.23.7-dev35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

persistent_cache: class-like __array__ refs can be misclassified as data primitives and crash hash serialization

3 participants