From d03b087202bd6d35c7a76e7dbbd7c2a86d582380 Mon Sep 17 00:00:00 2001 From: amit Date: Tue, 20 May 2025 16:10:55 -0400 Subject: [PATCH 1/2] implement pull request #3731 and add tests to fix #3065 --- plotly/shapeannotation.py | 3 +++ .../test_autoshapes/test_annotated_shapes.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/plotly/shapeannotation.py b/plotly/shapeannotation.py index a2323ed02d4..085b4fd8810 100644 --- a/plotly/shapeannotation.py +++ b/plotly/shapeannotation.py @@ -4,6 +4,9 @@ def _mean(x): if len(x) == 0: raise ValueError("x must have positive length") + + if len(x) == 2 and x[0] == x[1]: + return x[0] return float(sum(x)) / len(x) diff --git a/tests/test_optional/test_autoshapes/test_annotated_shapes.py b/tests/test_optional/test_autoshapes/test_annotated_shapes.py index 8d7c3806991..e0e88686a06 100644 --- a/tests/test_optional/test_autoshapes/test_annotated_shapes.py +++ b/tests/test_optional/test_autoshapes/test_annotated_shapes.py @@ -51,6 +51,25 @@ def multi_plot_fixture(): fig.add_trace(go.Scatter(x=[], y=[]), row=r, col=c) return fig +from datetime import datetime, timedelta +def test_add_shape_with_dates(): + start_date = datetime(2025, 1, 1) + end_date = datetime(2025, 10, 10) + dates = [] + current_date = start_date + while current_date <= end_date: + dates.append(current_date.strftime("%Y-%m-%d")) + current_date += timedelta(weeks=4) + print(dates) + fig = go.Figure( + data=[go.Scatter(x=[x for x in range(1, 20, 2)], y=dates)], + layout=go.Layout( + title=go.layout.Title(text="A Figure Specified By A Graph Object") + ) + ) + fig.add_vline(x="2025-06-24", annotation_text="test") + assert len(fig.layout.annotations) == 1 + # Make sure adding a shape without specifying an annotation doesn't add any annotations def test_add_shape_no_annotation(multi_plot_fixture): From f2049c5c29aec9d6a33c8e73b944dfb55e5afbd6 Mon Sep 17 00:00:00 2001 From: amit Date: Tue, 20 May 2025 17:32:14 -0400 Subject: [PATCH 2/2] used black to reformat the file that had reformat errors --- tests/test_optional/test_autoshapes/test_annotated_shapes.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_optional/test_autoshapes/test_annotated_shapes.py b/tests/test_optional/test_autoshapes/test_annotated_shapes.py index e0e88686a06..dfef857288b 100644 --- a/tests/test_optional/test_autoshapes/test_annotated_shapes.py +++ b/tests/test_optional/test_autoshapes/test_annotated_shapes.py @@ -51,7 +51,10 @@ def multi_plot_fixture(): fig.add_trace(go.Scatter(x=[], y=[]), row=r, col=c) return fig + from datetime import datetime, timedelta + + def test_add_shape_with_dates(): start_date = datetime(2025, 1, 1) end_date = datetime(2025, 10, 10) @@ -65,7 +68,7 @@ def test_add_shape_with_dates(): data=[go.Scatter(x=[x for x in range(1, 20, 2)], y=dates)], layout=go.Layout( title=go.layout.Title(text="A Figure Specified By A Graph Object") - ) + ), ) fig.add_vline(x="2025-06-24", annotation_text="test") assert len(fig.layout.annotations) == 1