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.

fix more tests (print_code, test_uv_tree snapshot) (#7781)

Shahmir Varqha444e708af64b
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py b/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
index 17e9497889..9e0ed11b8f 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
@@ -254,6 +254,12 @@ def python_print_pandas(
"sort=False",
)
pivot_code = f"{df_name}.pivot_table({args}).sort_index(axis=0)"
+ # Fill NaN with 0 for count/sum aggregations to match narwhals behavior
+ fill_code = (
+ f"{df_name} = {df_name}.fillna(0)"
+ if agg_func in ("count", "sum")
+ else ""
+ )
flatten_columns_code = (
f"{df_name}.columns = ["
f"f\"{{'_'.join(map(str, col)).strip()}}_{agg_func}\" "
@@ -261,7 +267,13 @@ def python_print_pandas(
f"for col in {df_name}.columns]"
)
reset_index_code = f"{df_name} = {df_name}.reset_index()"
- return f"{pivot_code}\n{flatten_columns_code}\n{reset_index_code}"
+ code_parts = [
+ pivot_code,
+ fill_code,
+ flatten_columns_code,
+ reset_index_code,
+ ]
+ return "\n".join(part for part in code_parts if part)
assert_never(transform.type)
@@ -352,16 +364,15 @@ def python_print_polars(
elif transform.type == TransformType.AGGREGATE:
column_ids, aggregations = transform.column_ids, transform.aggregations
- selected_df = f"{df_name}.select({_list_of_strings(column_ids)})"
- result_df = "pl.DataFrame()"
+ # Build aggregation expressions that match narwhals behavior
+ # Each column/aggregation combination produces a column named "column_agg"
+ agg_exprs = []
for agg_func in aggregations:
- agg_df = f"{selected_df}.{agg_func}()"
- rename_dict = {
- column: f"{column}_{agg_func}" for column in column_ids
- }
- agg_df = f"{agg_df}.rename({rename_dict})"
- result_df = f"{result_df}.hstack({agg_df})"
- return result_df
+ for column_id in column_ids:
+ agg_exprs.append(
+ f"pl.col({_as_literal(column_id)}).{agg_func}().alias({_as_literal(f'{column_id}_{agg_func}')})"
+ )
+ return f"{df_name}.select([{', '.join(agg_exprs)}])"
elif transform.type == TransformType.GROUP_BY:
column_ids, aggregation = transform.column_ids, transform.aggregation
@@ -457,12 +468,14 @@ def python_print_polars(
f"values={value_column_ids}",
f"aggregate_function={_as_literal(transform.aggregation) if transform.aggregation != 'count' else _as_literal('len')}",
)
+ pivot_code = f"{df_name}.pivot({args}).sort(by={index_column_ids})"
+ # Fill NaN with 0 for count/sum aggregations AFTER pivot to match narwhals behavior
+ # Only fill numeric columns (not index columns which retain original types)
fill_null_code = (
- ".select(pl.all().fill_null(0))"
+ f"{df_name} = {df_name}.with_columns(pl.selectors.numeric().fill_null(0))"
if transform.aggregation in ["count", "sum"]
else ""
- ) # noqa: E501
- pivot_code = f"{df_name}{fill_null_code}.pivot({args}).sort(by={index_column_ids})" # noqa: E501
+ )
lambda_code = (
f'lambda col,replacements=replacements: f"{transform.value_column_ids[0]}_{{col.translate(replacements)}}_{transform.aggregation}"' # noqa: E501
if len(transform.value_column_ids) == 1
@@ -472,7 +485,8 @@ def python_print_polars(
'replacements = str.maketrans({"{": "", "}": "", \'"\': "", ",": "_"})\n'
f"{df_name} = {df_name}.rename({lambda_code})"
)
- return f"{pivot_code}\n{rename_code}"
+ code_parts = [pivot_code, fill_null_code, rename_code]
+ return "\n".join(part for part in code_parts if part)
assert_never(transform.type)
diff --git a/tests/_plugins/ui/_impl/dataframes/test_print_code.py b/tests/_plugins/ui/_impl/dataframes/test_print_code.py
index 848b9b36f7..ea3af473f3 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_print_code.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_print_code.py
@@ -580,6 +580,13 @@ def test_print_code_result_matches_actual_transform_polars(
for column_id in transform.column_ids
)
)
+ # When aggregation_column_ids is empty, all remaining columns become
+ # aggregation columns. sum/mean fail on non-numeric columns (strings, etc.)
+ if not transform.aggregation_column_ids and transform.aggregation in {
+ "sum",
+ "mean",
+ }:
+ assume(False)
if transform.type == TransformType.PIVOT:
assume(
(
@@ -596,6 +603,7 @@ def test_print_code_result_matches_actual_transform_polars(
== set()
)
)
+ # Exclude lists, dicts, datetimes from pivot columns
assume(
not any(
column_id in {"lists", "dicts", "datetimes"}
@@ -614,6 +622,20 @@ def test_print_code_result_matches_actual_transform_polars(
for column_id in transform.value_column_ids
)
)
+ # Exclude booleans from pivot column_ids - polars uses lowercase (true/false)
+ # while narwhals uses Python's str() which gives True/False
+ assume(
+ not any(
+ column_id in {"booleans"} for column_id in transform.column_ids
+ )
+ )
+ # When value_column_ids is empty, all remaining columns become values.
+ # sum/mean aggregations fail on non-numeric columns (strings, booleans)
+ if not transform.value_column_ids and transform.aggregation in {
+ "sum",
+ "mean",
+ }:
+ assume(False)
# Polars
polars_code = python_print_transforms(
@@ -1009,3 +1031,80 @@ class TestCombinedTransforms:
self._test_transforms(pl_dataframe, transforms)
self._test_transforms(pd_dataframe, transforms)
+
+ def test_pivot_count_fills_null_with_zero(self):
+ """Test that pivot with count aggregation fills null values with 0.
+
+ When pivoting, some combinations may not exist in the data, resulting
+ in null values. For count and sum aggregations, these should be filled
+ with 0 to match narwhals behavior.
+ """
+ import pandas as pd
+ import polars as pl
+
+ # Create data where pivot will produce nulls
+ # Only 'x' has values for group 'a', only 'y' has values for group 'b'
+ pd_df = pd.DataFrame(
+ {
+ "group": ["a", "a", "b", "b"],
+ "category": ["x", "x", "y", "y"],
+ "value": [1, 2, 3, 4],
+ }
+ )
+ pl_df = pl.DataFrame(
+ {
+ "group": ["a", "a", "b", "b"],
+ "category": ["x", "x", "y", "y"],
+ "value": [1, 2, 3, 4],
+ }
+ )
+
+ transforms = Transformations(
+ [
+ PivotTransform(
+ type=TransformType.PIVOT,
+ column_ids=["category"],
+ index_column_ids=["group"],
+ aggregation="count",
+ value_column_ids=["value"],
+ ),
+ ]
+ )
+
+ self._test_transforms(pd_df, transforms)
+ self._test_transforms(pl_df, transforms)
+
+ def test_pivot_sum_fills_null_with_zero(self):
+ """Test that pivot with sum aggregation fills null values with 0."""
+ import pandas as pd
+ import polars as pl
+
+ pd_df = pd.DataFrame(
+ {
+ "group": ["a", "a", "b", "b"],
+ "category": ["x", "x", "y", "y"],
+ "value": [1, 2, 3, 4],
+ }
+ )
+ pl_df = pl.DataFrame(
+ {
+ "group": ["a", "a", "b", "b"],
+ "category": ["x", "x", "y", "y"],
+ "value": [1, 2, 3, 4],
+ }
+ )
+
+ transforms = Transformations(
+ [
+ PivotTransform(
+ type=TransformType.PIVOT,
+ column_ids=["category"],
+ index_column_ids=["group"],
+ aggregation="sum",
+ value_column_ids=["value"],
+ ),
+ ]
+ )
+
+ self._test_transforms(pd_df, transforms)
+ self._test_transforms(pl_df, transforms)
diff --git a/tests/_utils/snapshots/complex_project_tree.json b/tests/_utils/snapshots/complex_project_tree.json
index cd8ed78faf..d167faeb74 100644
--- a/tests/_utils/snapshots/complex_project_tree.json
+++ b/tests/_utils/snapshots/complex_project_tree.json
@@ -322,7 +322,7 @@
},
{
"name": "h11",
- "version": "0.0.0",
+ "version": "0.16.0",
"tags": [],
"dependencies": []
}
@@ -418,4 +418,4 @@
]
}
]
-}
\ No newline at end of file
+}
diff --git a/tests/_utils/test_uv_tree.py b/tests/_utils/test_uv_tree.py
index f5925d8831..58b5642206 100644
--- a/tests/_utils/test_uv_tree.py
+++ b/tests/_utils/test_uv_tree.py
@@ -4,6 +4,7 @@ import json
import os
import pathlib
import subprocess
+import sys
import pytest
@@ -12,6 +13,11 @@ from marimo._server.models.packages import DependencyTreeNode
from marimo._utils.uv_tree import parse_uv_tree
from tests.mocks import snapshotter
+skip_if_below_py312 = pytest.mark.skipif(
+ sys.version_info < (3, 12),
+ reason="uv resolution snapshots only run on Python 3.12+",
+)
+
UV_BIN = os.environ.get("UV")
SELF_DIR = pathlib.Path(__file__).parent
snapshot_test = snapshotter(__file__)
@@ -38,6 +44,7 @@ def uv(cmd: list[str], cwd: str | None = None) -> str:
@pytest.mark.skipif(UV_BIN is None, reason="requires uv executable.")
+@skip_if_below_py312
def test_complex_project_tree(tmp_path: pathlib.Path) -> None:
uv(["init", "blah"], cwd=str(tmp_path))
project_dir = tmp_path / "blah"
@@ -70,6 +77,7 @@ def test_simple_project_tree(tmp_path: pathlib.Path) -> None:
@pytest.mark.skipif(UV_BIN is None, reason="requires uv executable.")
+@skip_if_below_py312
def test_script_tree(tmp_path: pathlib.Path) -> None:
script_path = tmp_path / "blah.py"
uv(["init", "--script", str(script_path)])
@@ -88,7 +96,7 @@ def test_empty_script_tree_stable_output(tmp_path: pathlib.Path) -> None:
snapshot_test("empty_script_tree.json", serialize(tree))
[email protected](reason="TODO: fix this. fails in CI.")
+@skip_if_below_py312
def test_complex_project_tree_raw_snapshot() -> None:
raw = """blah v0.1.0
├── anywidget v0.9.18
@@ -194,4 +202,8 @@ anywidget v0.9.18
├── psygnal v0.13.0
└── typing-extensions v4.14.0"""
tree = parse_uv_tree(raw)
- snapshot_test("script_tree_from_raw.json", serialize(tree))
+ # Use keep_version=True to avoid jedi v0.19.2 being incorrectly
+ # sanitized when marimo version is 0.19.2
+ snapshot_test(
+ "script_tree_from_raw.json", serialize(tree), keep_version=True
+ )
diff --git a/tests/mocks.py b/tests/mocks.py
index f577dc5e43..ccebaa4031 100644
--- a/tests/mocks.py
+++ b/tests/mocks.py
@@ -4,7 +4,7 @@ import difflib
import re
from html.parser import HTMLParser
from pathlib import Path
-from typing import Callable
+from typing import Protocol
import pytest
@@ -13,6 +13,12 @@ from marimo._utils.paths import maybe_make_dirs
from marimo._utils.platform import is_windows
+class SnapshotFunc(Protocol):
+ def __call__(
+ self, filename: str, result: str, keep_version: bool = False
+ ) -> None: ...
+
+
class ToText(HTMLParser):
def __init__(self) -> None:
HTMLParser.__init__(self)
@@ -35,7 +41,7 @@ class ToText(HTMLParser):
return parser.text()
-def snapshotter(current_file: str) -> Callable[[str, str], None]:
+def snapshotter(current_file: str) -> SnapshotFunc:
"""
Utility function to create and compare snapshots.