|
| 1 | +"""Tests for marimo._runtime.primitives module.""" |
| 2 | + |
| 3 | +import functools |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from marimo._runtime.primitives import is_pure_function |
| 7 | + |
| 8 | + |
| 9 | +class TestWrappedFunctionHandling: |
| 10 | + """Test handling of wrapped functions (decorators) in is_pure_function.""" |
| 11 | + |
| 12 | + def test_wrapped_function_follows_wrapped_object(self): |
| 13 | + """Test that is_pure_function follows __wrapped__ attribute to check the underlying function.""" |
| 14 | + |
| 15 | + def external_function(): |
| 16 | + """A function from external module.""" |
| 17 | + return 42 |
| 18 | + |
| 19 | + external_function.__module__ = "external_module" |
| 20 | + |
| 21 | + # Create a decorator that wraps the function |
| 22 | + def decorator(func): |
| 23 | + @functools.wraps(func) |
| 24 | + def wrapper(): |
| 25 | + return func() |
| 26 | + |
| 27 | + return wrapper |
| 28 | + |
| 29 | + decorated_function = decorator(external_function) |
| 30 | + |
| 31 | + # Mock globals dict |
| 32 | + defs = {"decorated_function": decorated_function} |
| 33 | + cache = {} |
| 34 | + |
| 35 | + # Should follow the wrapped function and determine purity based on that |
| 36 | + result = is_pure_function( |
| 37 | + "decorated_function", decorated_function, defs, cache |
| 38 | + ) |
| 39 | + |
| 40 | + # Should be True since the wrapped function is external |
| 41 | + assert result is True |
| 42 | + |
| 43 | + def test_nested_wrapped_functions(self): |
| 44 | + """Test handling of functions with multiple layers of wrapping.""" |
| 45 | + |
| 46 | + def original_function(): |
| 47 | + return "original" |
| 48 | + |
| 49 | + original_function.__module__ = "external_module" |
| 50 | + |
| 51 | + def decorator1(func: Any) -> Any: |
| 52 | + @functools.wraps(func) |
| 53 | + def wrapper1(*args: Any, **kwargs: Any): |
| 54 | + return func(*args, **kwargs) |
| 55 | + |
| 56 | + return wrapper1 |
| 57 | + |
| 58 | + def decorator2(func: Any) -> Any: |
| 59 | + @functools.wraps(func) |
| 60 | + def wrapper2(*args: Any, **kwargs: Any): |
| 61 | + return func(*args, **kwargs) |
| 62 | + |
| 63 | + return wrapper2 |
| 64 | + |
| 65 | + # Apply multiple decorators |
| 66 | + @decorator2 |
| 67 | + @decorator1 |
| 68 | + def nested_decorated(): |
| 69 | + return original_function() |
| 70 | + |
| 71 | + defs = {"nested_decorated": nested_decorated} |
| 72 | + cache = {} |
| 73 | + |
| 74 | + # Should handle nested wrapping correctly |
| 75 | + result = is_pure_function( |
| 76 | + "nested_decorated", nested_decorated, defs, cache |
| 77 | + ) |
| 78 | + assert isinstance(result, bool) |
| 79 | + |
| 80 | + def test_wrapped_attribute_is_none(self): |
| 81 | + """Test handling when __wrapped__ exists but is None.""" |
| 82 | + |
| 83 | + def function_with_none_wrapped(): |
| 84 | + return 42 |
| 85 | + |
| 86 | + function_with_none_wrapped.__module__ = "external_module" |
| 87 | + |
| 88 | + # Set __wrapped__ to None |
| 89 | + function_with_none_wrapped.__wrapped__ = None |
| 90 | + |
| 91 | + defs = {"function_with_none_wrapped": function_with_none_wrapped} |
| 92 | + cache = {} |
| 93 | + |
| 94 | + # Should handle None __wrapped__ gracefully |
| 95 | + result = is_pure_function( |
| 96 | + "function_with_none_wrapped", |
| 97 | + function_with_none_wrapped, |
| 98 | + defs, |
| 99 | + cache, |
| 100 | + ) |
| 101 | + assert result is True # External function should be pure |
| 102 | + |
| 103 | + def test_main_module_wrapped_function(self): |
| 104 | + """Test wrapped function from __main__ module.""" |
| 105 | + |
| 106 | + def internal_function(): |
| 107 | + return 42 |
| 108 | + |
| 109 | + internal_function.__module__ = "__main__" |
| 110 | + |
| 111 | + def decorator(func): |
| 112 | + @functools.wraps(func) |
| 113 | + def wrapper(): |
| 114 | + return func() |
| 115 | + |
| 116 | + return wrapper |
| 117 | + |
| 118 | + decorated_function = decorator(internal_function) |
| 119 | + |
| 120 | + defs = { |
| 121 | + "decorated_function": decorated_function, |
| 122 | + "internal_function": internal_function, |
| 123 | + } |
| 124 | + cache = {} |
| 125 | + |
| 126 | + # Should follow wrapped function and check if it's pure |
| 127 | + result = is_pure_function( |
| 128 | + "decorated_function", decorated_function, defs, cache |
| 129 | + ) |
| 130 | + |
| 131 | + # Should be True since the wrapped function is also pure (no external refs) |
| 132 | + assert result is True |
0 commit comments