Skip to content

Commit

Permalink
fix: preserve complex objects in unwrap_py_scalar, only convert enums…
Browse files Browse the repository at this point in the history
… to strings

Co-Authored-By: Myles Scolnick <myles@marimo.io>
  • Loading branch information
devin-ai-integration[bot] and mscolnick committed Jan 16, 2025
1 parent acc117b commit 3197cf2
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions marimo/_utils/narwhals_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ def unwrap_narwhals_dataframe(df: Any) -> Any:
def unwrap_py_scalar(value: Any) -> Any:
"""
Convert a narwhals value to a python scalar if possible, otherwise return
the value as is. Datetime objects and primitive types are preserved.
Enums and other complex types are converted to strings.
the value as is. Datetime objects, primitive types, and complex objects like
dictionaries are preserved. Only enum/categorical types are converted to strings.
"""
from datetime import datetime, date
import polars as pl
Expand All @@ -160,14 +160,13 @@ def unwrap_py_scalar(value: Any) -> Any:
# Check if the result is already a datetime/date after conversion
if isinstance(scalar, (datetime, date)):
return scalar
# For other primitive types
# For primitive types
if isinstance(scalar, (str, int, float, bool, type(None))):
return scalar
# For non-primitive types (including enums), convert to string
return str(scalar)
# For enums, convert to string, otherwise preserve the object
if hasattr(scalar, '__members__'): # Check if it's an enum
return str(scalar)
return scalar
except ValueError:
# If conversion fails, preserve primitives and datetimes
if isinstance(value, (str, int, float, bool, type(None), datetime, date)):
return value
# Convert other types to string
return str(value)
# If conversion fails, preserve the value as-is
return value

0 comments on commit 3197cf2

Please sign in to comment.