Skip to content

Commit 5c81ac4

Browse files
authored
CLN: add arg validation for caption (#43368)
1 parent 6f4c382 commit 5c81ac4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ Formerly Styler relied on ``display.html.use_mathjax``, which has now been repla
8383

8484
There are also bug fixes and deprecations listed below.
8585

86+
Validation now for ``caption`` arg (:issue:`43368`)
87+
8688
.. _whatsnew_140.enhancements.pyarrow_csv_engine:
8789

8890
Multithreaded CSV reading with a new CSV Engine based on pyarrow

pandas/io/formats/style.py

+10
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,16 @@ def set_caption(self, caption: str | tuple) -> Styler:
17731773
-------
17741774
self : Styler
17751775
"""
1776+
msg = "`caption` must be either a string or 2-tuple of strings."
1777+
if isinstance(caption, tuple):
1778+
if (
1779+
len(caption) != 2
1780+
or not isinstance(caption[0], str)
1781+
or not isinstance(caption[1], str)
1782+
):
1783+
raise ValueError(msg)
1784+
elif not isinstance(caption, str):
1785+
raise ValueError(msg)
17761786
self.caption = caption
17771787
return self
17781788

pandas/tests/io/formats/style/test_style.py

+7
Original file line numberDiff line numberDiff line change
@@ -1440,3 +1440,10 @@ def test_hidden_column_names(mi_df):
14401440
ctx = mi_styler._translate(True, True)
14411441
assert len(ctx["head"]) == 1 # no index names and only one visible column headers
14421442
assert ctx["head"][0][1]["display_value"] == " "
1443+
1444+
1445+
@pytest.mark.parametrize("caption", [1, ("a", "b", "c"), (1, "s")])
1446+
def test_caption_raises(mi_styler, caption):
1447+
msg = "`caption` must be either a string or 2-tuple of strings."
1448+
with pytest.raises(ValueError, match=msg):
1449+
mi_styler.set_caption(caption)

0 commit comments

Comments
 (0)