Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

6.4 KB
Raw Blame
"""Tests for marimo._runtime.primitives module."""

import functools
from typing import Any

import pytest

from marimo._dependencies.dependencies import DependencyManager
from marimo._runtime.primitives import is_data_primitive, is_pure_function

HAS_NUMPY = DependencyManager.numpy.has()


class TestDataPrimitiveClassification:
    @pytest.mark.skipif(not HAS_NUMPY, reason="numpy is required")
    def test_rejects_class_like_array_protocol(self) -> None:
        """Issue #9563: class-like __array__ refs are not data primitives."""
        import numpy as np

        class _ClassLikeArray:
            @classmethod
            def __array__(cls, dtype: Any = None) -> Any:
                return np.array([object()], dtype=object)

        assert is_data_primitive(_ClassLikeArray) is False

    @pytest.mark.skipif(
        not DependencyManager.polars.has(), reason="polars required"
    )
    @pytest.mark.parametrize("as_frame", [False, True])
    def test_polars_numeric_classification(self, as_frame: bool) -> None:
        import polars as pl

        values = [
            pl.Series("integer", [1, 2]),
            pl.Series("float", [1.0, 2.0]),
            pl.Series("string", ["a", "b"]),
            pl.Series("list", [[1], [2]]),
            pl.Series("object", [object()], dtype=pl.Object),
        ]
        assert {
            value.name: is_data_primitive(
                value.to_frame() if as_frame else value
            )
            for value in values
        } == {
            "integer": True,
            "float": True,
            "string": False,
            "list": False,
            "object": False,
        }

    @pytest.mark.skipif(
        not DependencyManager.pyarrow.has(), reason="pyarrow required"
    )
    def test_pyarrow_numeric_classification(self) -> None:
        import pyarrow as pa

        values = {
            "integer_array": pa.array([1, None]),
            "float_chunks": pa.chunked_array([[1.0], [2.0]]),
            "numeric_table": pa.table({"a": [1], "b": [1.5]}),
            "numeric_batch": pa.RecordBatch.from_pydict({"a": [1]}),
            "string_array": pa.array(["a"]),
            "string_table": pa.table({"a": [1], "b": ["x"]}),
            "dictionary_table": pa.table(
                {"a": pa.array(["x"]).dictionary_encode()}
            ),
            "boolean_array": pa.array([True, None]),
        }
        assert {
            name: is_data_primitive(value) for name, value in values.items()
        } == {
            "integer_array": True,
            "float_chunks": True,
            "numeric_table": True,
            "numeric_batch": True,
            "string_array": False,
            "string_table": False,
            "dictionary_table": False,
            "boolean_array": False,
        }


class TestWrappedFunctionHandling:
    """Test handling of wrapped functions (decorators) in is_pure_function."""

    def test_wrapped_function_follows_wrapped_object(self):
        """Test that is_pure_function follows __wrapped__ attribute to check the underlying function."""

        def external_function():
            """A function from external module."""
            return 42

        external_function.__module__ = "external_module"

        # Create a decorator that wraps the function
        def decorator(func):
            @functools.wraps(func)
            def wrapper():
                return func()

            return wrapper

        decorated_function = decorator(external_function)

        # Mock globals dict
        defs = {"decorated_function": decorated_function}
        cache = {}

        # Should follow the wrapped function and determine purity based on that
        result = is_pure_function(
            "decorated_function", decorated_function, defs, cache
        )

        # Should be True since the wrapped function is external
        assert result is True

    def test_nested_wrapped_functions(self):
        """Test handling of functions with multiple layers of wrapping."""

        def original_function():
            return "original"

        original_function.__module__ = "external_module"

        def decorator1(func: Any) -> Any:
            @functools.wraps(func)
            def wrapper1(*args: Any, **kwargs: Any):
                return func(*args, **kwargs)

            return wrapper1

        def decorator2(func: Any) -> Any:
            @functools.wraps(func)
            def wrapper2(*args: Any, **kwargs: Any):
                return func(*args, **kwargs)

            return wrapper2

        # Apply multiple decorators
        @decorator2
        @decorator1
        def nested_decorated():
            return original_function()

        defs = {"nested_decorated": nested_decorated}
        cache = {}

        # Should handle nested wrapping correctly
        result = is_pure_function(
            "nested_decorated", nested_decorated, defs, cache
        )
        assert isinstance(result, bool)

    def test_wrapped_attribute_is_none(self):
        """Test handling when __wrapped__ exists but is None."""

        def function_with_none_wrapped():
            return 42

        function_with_none_wrapped.__module__ = "external_module"

        # Set __wrapped__ to None
        function_with_none_wrapped.__wrapped__ = None

        defs = {"function_with_none_wrapped": function_with_none_wrapped}
        cache = {}

        # Should handle None __wrapped__ gracefully
        result = is_pure_function(
            "function_with_none_wrapped",
            function_with_none_wrapped,
            defs,
            cache,
        )
        assert result is True  # External function should be pure

    def test_main_module_wrapped_function(self):
        """Test wrapped function from __main__ module."""

        def internal_function():
            return 42

        internal_function.__module__ = "__main__"

        def decorator(func):
            @functools.wraps(func)
            def wrapper():
                return func()

            return wrapper

        decorated_function = decorator(internal_function)

        defs = {
            "decorated_function": decorated_function,
            "internal_function": internal_function,
        }
        cache = {}

        # Should follow wrapped function and check if it's pure
        result = is_pure_function(
            "decorated_function", decorated_function, defs, cache
        )

        # Should be True since the wrapped function is also pure (no external refs)
        assert result is True