From 99d1ed70540cbf51ad7652af50990d1eb3bcdc36 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Thu, 2 Sep 2021 23:06:52 +0200 Subject: [PATCH 1/2] add arg validation for caption --- doc/source/whatsnew/v1.4.0.rst | 2 ++ pandas/io/formats/style.py | 10 ++++++++++ pandas/tests/io/formats/style/test_style.py | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 2f8cb346935a9..ac308623709c6 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -83,6 +83,8 @@ Formerly Styler relied on ``display.html.use_mathjax``, which has now been repla There are also bug fixes and deprecations listed below. +Validation now for ``caption`` arg (:issue:`xxxxx`) + .. _whatsnew_140.enhancements.pyarrow_csv_engine: Multithreaded CSV reading with a new CSV Engine based on pyarrow diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index e6f9503a1e6f2..07c0062fc5ce4 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1772,6 +1772,16 @@ def set_caption(self, caption: str | tuple) -> Styler: ------- self : Styler """ + msg = "`caption` must be either a string or 2-tuple of strings." + if isinstance(caption, tuple): + if ( + len(caption) != 2 + or not isinstance(caption[0], str) + or not isinstance(caption[1], str) + ): + raise ValueError(msg) + elif not isinstance(caption, str): + raise ValueError(msg) self.caption = caption return self diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index e6ac2e4ae81e1..96008f79359d5 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -1439,3 +1439,10 @@ def test_hidden_column_names(mi_df): ctx = mi_styler._translate(True, True) assert len(ctx["head"]) == 1 # no index names and only one visible column headers assert ctx["head"][0][1]["display_value"] == " " + + +@pytest.mark.parametrize("caption", [1, ("a", "b", "c"), (1, "s")]) +def test_caption_raises(mi_styler, caption): + msg = "`caption` must be either a string or 2-tuple of strings." + with pytest.raises(ValueError, match=msg): + mi_styler.set_caption(caption) From 14627c4d01b8e9f637eff681d829247e4cae5663 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Thu, 2 Sep 2021 23:16:42 +0200 Subject: [PATCH 2/2] add arg validation for caption --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index ac308623709c6..163aec721962b 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -83,7 +83,7 @@ Formerly Styler relied on ``display.html.use_mathjax``, which has now been repla There are also bug fixes and deprecations listed below. -Validation now for ``caption`` arg (:issue:`xxxxx`) +Validation now for ``caption`` arg (:issue:`43368`) .. _whatsnew_140.enhancements.pyarrow_csv_engine: