Skip to content

TST: bare pytest raises #32817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ def test_reader_dtype(self, read_ext):
expected["c"] = ["001", "002", "003", "004"]
tm.assert_frame_equal(actual, expected)

with pytest.raises(ValueError):
msg = "Unable to convert column d to type int64"
with pytest.raises(ValueError, match=msg):
pd.read_excel(basename + read_ext, dtype={"d": "int64"})

@pytest.mark.parametrize(
Expand Down Expand Up @@ -822,13 +823,15 @@ def test_excel_old_index_format(self, read_ext):

def test_read_excel_bool_header_arg(self, read_ext):
# GH 6114
msg = "Passing a bool to header is invalid"
for arg in [True, False]:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
pd.read_excel("test1" + read_ext, header=arg)

def test_read_excel_chunksize(self, read_ext):
# GH 8011
with pytest.raises(NotImplementedError):
msg = "chunksize keyword of read_excel is not implemented"
with pytest.raises(NotImplementedError, match=msg):
pd.read_excel("test1" + read_ext, chunksize=100)

def test_read_excel_skiprows_list(self, read_ext):
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ def test_excel_sheet_by_name_raise(self, path):

tm.assert_frame_equal(gt, df)

with pytest.raises(xlrd.XLRDError):
msg = "No sheet named <'0'>"
with pytest.raises(xlrd.XLRDError, match=msg):
pd.read_excel(xl, "0")

def test_excel_writer_context_manager(self, frame, path):
Expand Down Expand Up @@ -973,7 +974,11 @@ def roundtrip(data, header=True, parser_hdr=0, index=True):
# This if will be removed once multi-column Excel writing
# is implemented. For now fixing gh-9794.
if c_idx_nlevels > 1:
with pytest.raises(NotImplementedError):
msg = (
"Writing to Excel with MultiIndex columns and no index "
"\\('index'=False\\) is not yet implemented."
)
with pytest.raises(NotImplementedError, match=msg):
roundtrip(df, use_headers, index=False)
else:
res = roundtrip(df, use_headers)
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/io/excel/test_xlwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ def test_excel_raise_error_on_multiindex_columns_and_no_index(ext):
[("site", ""), ("2014", "height"), ("2014", "weight")]
)
df = DataFrame(np.random.randn(10, 3), columns=cols)
with pytest.raises(NotImplementedError):

msg = (
"Writing to Excel with MultiIndex columns and no index "
"\\('index'=False\\) is not yet implemented."
)
with pytest.raises(NotImplementedError, match=msg):
with tm.ensure_clean(ext) as path:
df.to_excel(path, index=False)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,8 @@ def test_to_string_specified_header(self):

assert df_s == expected

with pytest.raises(ValueError):
msg = "Writing 2 cols but got 1 aliases"
with pytest.raises(ValueError, match=msg):
df.to_string(header=["X"])

def test_to_string_no_index(self):
Expand Down
39 changes: 25 additions & 14 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def h(x, foo="bar"):
]

def test_init_non_pandas(self):
with pytest.raises(TypeError):
msg = "``data`` must be a Series or DataFrame"
with pytest.raises(TypeError, match=msg):
Styler([1, 2, 3])

def test_init_series(self):
Expand Down Expand Up @@ -1013,7 +1014,8 @@ def test_bar_align_zero_nans(self):

def test_bar_bad_align_raises(self):
df = pd.DataFrame({"A": [-100, -60, -30, -20]})
with pytest.raises(ValueError):
msg = "`align` must be one of {'left', 'zero',' mid'}"
with pytest.raises(ValueError, match=msg):
df.style.bar(align="poorly", color=["#d65f5f", "#5fba7d"])

def test_format_with_na_rep(self):
Expand Down Expand Up @@ -1082,7 +1084,8 @@ def test_format_non_numeric_na(self):
def test_format_with_bad_na_rep(self):
# GH 21527 28358
df = pd.DataFrame([[None, None], [1.1, 1.2]], columns=["A", "B"])
with pytest.raises(TypeError):
msg = "Expected a string, got -1 instead"
with pytest.raises(TypeError, match=msg):
df.style.format(None, na_rep=-1)

def test_highlight_null(self, null_color="red"):
Expand Down Expand Up @@ -1110,10 +1113,11 @@ def test_highlight_null_subset(self):

def test_nonunique_raises(self):
df = pd.DataFrame([[1, 2]], columns=["A", "A"])
with pytest.raises(ValueError):
msg = "style is not supported for non-unique indices."
with pytest.raises(ValueError, match=msg):
df.style

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
Styler(df)

def test_caption(self):
Expand Down Expand Up @@ -1260,9 +1264,12 @@ def test_display_format(self):

def test_display_format_raises(self):
df = pd.DataFrame(np.random.randn(2, 2))
with pytest.raises(TypeError):
msg = "Expected a template string or callable, got 5 instead"
with pytest.raises(TypeError, match=msg):
df.style.format(5)
with pytest.raises(TypeError):

msg = "Expected a template string or callable, got True instead"
with pytest.raises(TypeError, match=msg):
df.style.format(True)

def test_display_set_precision(self):
Expand Down Expand Up @@ -1335,35 +1342,39 @@ def test_display_dict(self):

def test_bad_apply_shape(self):
df = pd.DataFrame([[1, 2], [3, 4]])
with pytest.raises(ValueError):
msg = "returned the wrong shape"
with pytest.raises(ValueError, match=msg):
df.style._apply(lambda x: "x", subset=pd.IndexSlice[[0, 1], :])

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
df.style._apply(lambda x: [""], subset=pd.IndexSlice[[0, 1], :])

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
df.style._apply(lambda x: ["", "", "", ""])

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
df.style._apply(lambda x: ["", "", ""], subset=1)

with pytest.raises(ValueError):
msg = "Length mismatch: Expected axis has 3 elements"
with pytest.raises(ValueError, match=msg):
df.style._apply(lambda x: ["", "", ""], axis=1)

def test_apply_bad_return(self):
def f(x):
return ""

df = pd.DataFrame([[1, 2], [3, 4]])
with pytest.raises(TypeError):
msg = "must return a DataFrame when passed to `Styler.apply` with axis=None"
with pytest.raises(TypeError, match=msg):
df.style._apply(f, axis=None)

def test_apply_bad_labels(self):
def f(x):
return pd.DataFrame(index=[1, 2], columns=["a", "b"])

df = pd.DataFrame([[1, 2], [3, 4]])
with pytest.raises(ValueError):
msg = "must have identical index and columns as the input"
with pytest.raises(ValueError, match=msg):
df.style._apply(f, axis=None)

def test_get_level_lengths(self):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,8 @@ def test_to_latex_specified_header(self):

assert withoutescape_result == withoutescape_expected

with pytest.raises(ValueError):
msg = "Writing 2 cols but got 1 aliases"
with pytest.raises(ValueError, match=msg):
df.to_latex(header=["A"])

def test_to_latex_decimal(self, float_frame):
Expand Down