From 3d6b7bcdfc5b8e0fee1f4206d08e75b5b72a91e2 Mon Sep 17 00:00:00 2001 From: Rodrigo-Palma Date: Fri, 25 Sep 2026 01:00:19 -0300 Subject: [PATCH] fix(stat_summary_bin): emit the fallback warning instead of building it `setup_params` constructs `PlotnineWarning(...)` and drops it on the floor, so falling back to `mean_se()` happens in complete silence. The commit that introduced this (827fd095, "Add default function to stat_summary_bin") replaced `raise PlotnineError("No summary function")` with the warning and even switched the import, so announcing the fallback was the intent; only the `warn()` call is missing. The sibling `stat_summary.setup_params` still raises on the same condition, and ggplot2 informs here too (`R/stat-summary-bin.R`: "No summary function supplied, defaulting to mean_se"). It matters because a misspelled argument, `fun=` instead of `fun_y=` for instance, silently yields a plausible and wrong plot. The stray comma in the message goes away with it, making the wording match ggplot2's. --- plotnine/stats/stat_summary_bin.py | 6 ++++-- tests/test_stat_summary_bin.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/plotnine/stats/stat_summary_bin.py b/plotnine/stats/stat_summary_bin.py index 5e92c269e8..47585eb944 100644 --- a/plotnine/stats/stat_summary_bin.py +++ b/plotnine/stats/stat_summary_bin.py @@ -1,4 +1,5 @@ from typing import TYPE_CHECKING, cast +from warnings import warn import numpy as np import pandas as pd @@ -107,8 +108,9 @@ class stat_summary_bin(stat): def setup_params(self, data): keys = ("fun_data", "fun_y", "fun_ymin", "fun_ymax") if not any(self.params[k] for k in keys): - PlotnineWarning( - "No summary function, supplied, defaulting to mean_se()" + warn( + "No summary function supplied, defaulting to mean_se()", + PlotnineWarning, ) self.params["fun_data"] = "mean_se" diff --git a/tests/test_stat_summary_bin.py b/tests/test_stat_summary_bin.py index a80e63be9e..5f0dfd45c9 100644 --- a/tests/test_stat_summary_bin.py +++ b/tests/test_stat_summary_bin.py @@ -1,7 +1,9 @@ import numpy as np import pandas as pd +import pytest from plotnine import aes, ggplot, stat_summary_bin +from plotnine.exceptions import PlotnineWarning data = pd.DataFrame( { @@ -31,3 +33,11 @@ def test_continuous_x(): def test_setting_binwidth(): p = ggplot(data, aes("xc", "y")) + stat_summary_bin(binwidth=3, geom="bar") assert p == "setting_binwidth" + + +def test_no_summary_function_warns(): + # A missing summary function falls back to mean_se(), and the + # fallback must be announced. + p = ggplot(data, aes("xc", "y")) + stat_summary_bin(bins=5) + with pytest.warns(PlotnineWarning, match="defaulting to mean_se"): + p._build()