Skip to content

30999 fix bare pytest raises #33925

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 11 commits into from
May 1, 2020
4 changes: 2 additions & 2 deletions pandas/tests/series/methods/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ def test_errors(self):

# non-monotonic
assert not s.index.is_monotonic
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="requires a sorted index"):
s.asof(s.index[0])

# subset with Series
N = 10
rng = date_range("1/1/1990", periods=N, freq="53s")
s = Series(np.random.randn(N), index=rng)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="not valid for Series"):
s.asof(s.index[0], subset="foo")

def test_all_nans(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_droplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ def test_droplevel(self):
result = ser.droplevel("b", axis="index")
tm.assert_series_equal(result, expected)
# test that droplevel raises ValueError on axis != 0
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="No axis named columns"):
ser.droplevel(1, axis="columns")
6 changes: 3 additions & 3 deletions pandas/tests/series/methods/test_tz_localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_series_tz_localize_ambiguous_bool(self):
expected0 = Series([expected0])
expected1 = Series([expected1])

with pytest.raises(pytz.AmbiguousTimeError):
with tm.external_error_raised(pytz.AmbiguousTimeError):
ser.dt.tz_localize("US/Central")

result = ser.dt.tz_localize("US/Central", ambiguous=True)
Expand Down Expand Up @@ -66,10 +66,10 @@ def test_series_tz_localize_nonexistent(self, tz, method, exp):
dti = date_range(start="2015-03-29 02:00:00", periods=n, freq="min")
s = Series(1, dti)
if method == "raise":
with pytest.raises(pytz.NonExistentTimeError):
with tm.external_error_raised(pytz.NonExistentTimeError):
s.tz_localize(tz, nonexistent=method)
elif exp == "invalid":
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="argument must be one of"):
dti.tz_localize(tz, nonexistent=method)
else:
result = s.tz_localize(tz, nonexistent=method)
Expand Down
11 changes: 7 additions & 4 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,21 @@ def test_transform(self, string_series):

def test_transform_and_agg_error(self, string_series):
# we are trying to transform with an aggregator
with pytest.raises(ValueError):
msg = "transforms cannot produce aggregated results"
with pytest.raises(ValueError, match=msg):
string_series.transform(["min", "max"])

with pytest.raises(ValueError):
msg = "cannot combine transform and aggregation"
with pytest.raises(ValueError, match=msg):
with np.errstate(all="ignore"):
string_series.agg(["sqrt", "max"])

with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
with np.errstate(all="ignore"):
string_series.transform(["sqrt", "max"])

with pytest.raises(ValueError):
msg = "cannot perform both aggregation and transformation"
with pytest.raises(ValueError, match=msg):
with np.errstate(all="ignore"):
string_series.agg({"foo": np.sqrt, "bar": "sum"})

Expand Down
33 changes: 19 additions & 14 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,13 @@ class TestSeriesComparison:
def test_comparison_different_length(self):
a = Series(["a", "b", "c"])
b = Series(["b", "a"])
with pytest.raises(ValueError):
msg = "only compare identically-labeled Series"
with pytest.raises(ValueError, match=msg):
a < b

a = Series([1, 2])
b = Series([2, 3, 4])
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
a == b

@pytest.mark.parametrize("opname", ["eq", "ne", "gt", "lt", "ge", "le"])
Expand Down Expand Up @@ -474,37 +475,40 @@ def test_categorical_comparisons(self):
assert (~(f == a) == (f != a)).all()

# non-equality is not comparable
with pytest.raises(TypeError):
msg = "can only compare equality or not"
with pytest.raises(TypeError, match=msg):
a < b
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
b < a
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
a > b
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
b > a

def test_unequal_categorical_comparison_raises_type_error(self):
# unequal comparison should raise for unordered cats
cat = Series(Categorical(list("abc")))
with pytest.raises(TypeError):
msg = "can only compare equality or not"
with pytest.raises(TypeError, match=msg):
cat > "b"

cat = Series(Categorical(list("abc"), ordered=False))
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
cat > "b"

# https://github.com/pandas-dev/pandas/issues/9836#issuecomment-92123057
# and following comparisons with scalars not in categories should raise
# for unequal comps, but not for equal/not equal
cat = Series(Categorical(list("abc"), ordered=True))

with pytest.raises(TypeError):
msg = "Cannot compare a Categorical for op.+with a scalar"
with pytest.raises(TypeError, match=msg):
cat < "d"
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
cat > "d"
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
"d" < cat
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
"d" > cat

tm.assert_series_equal(cat == "d", Series([False, False, False]))
Expand Down Expand Up @@ -668,10 +672,11 @@ def test_series_add_aware_naive_raises(self):

ser_utc = ser.tz_localize("utc")

with pytest.raises(Exception):
msg = "Cannot join tz-naive with tz-aware DatetimeIndex"
with pytest.raises(Exception, match=msg):
ser + ser_utc

with pytest.raises(Exception):
with pytest.raises(Exception, match=msg):
ser_utc + ser

def test_datetime_understood(self):
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ def get_dir(s):
s.dt.hour = 5

# trying to set a copy
msg = "modifications to a property of a datetimelike.+not supported"
with pd.option_context("chained_assignment", "raise"):
with pytest.raises(com.SettingWithCopyError):
with pytest.raises(com.SettingWithCopyError, match=msg):
s.dt.hour[0] = 5

@pytest.mark.parametrize(
Expand Down Expand Up @@ -311,7 +312,7 @@ def test_dt_round_tz_ambiguous(self, method):
tm.assert_series_equal(result, expected)

# raise
with pytest.raises(pytz.AmbiguousTimeError):
with tm.external_error_raised(pytz.AmbiguousTimeError):
getattr(df1.date.dt, method)("H", ambiguous="raise")

@pytest.mark.parametrize(
Expand Down
27 changes: 16 additions & 11 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,25 @@ def test_logical_operators_int_dtype_with_float(self):
# GH#9016: support bitwise op for integer types
s_0123 = Series(range(4), dtype="int64")

with pytest.raises(TypeError):
msg = "Cannot perform.+with a dtyped.+array and scalar of type"
with pytest.raises(TypeError, match=msg):
s_0123 & np.NaN
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
s_0123 & 3.14
with pytest.raises(TypeError):
msg = "unsupported operand type.+for &:"
with pytest.raises(TypeError, match=msg):
s_0123 & [0.1, 4, 3.14, 2]
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
s_0123 & np.array([0.1, 4, 3.14, 2])
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
s_0123 & Series([0.1, 4, -3.14, 2])

def test_logical_operators_int_dtype_with_str(self):
s_1111 = Series([1] * 4, dtype="int8")

with pytest.raises(TypeError):
msg = "Cannot perform 'and_' with a dtyped.+array and scalar of type"
with pytest.raises(TypeError, match=msg):
s_1111 & "a"
with pytest.raises(TypeError):
with pytest.raises(TypeError, match="unsupported operand.+for &"):
s_1111 & ["a", "b", "c", "d"]

def test_logical_operators_int_dtype_with_bool(self):
Expand Down Expand Up @@ -255,7 +257,8 @@ def test_logical_operators_int_dtype_with_bool_dtype_and_reindex(self):
def test_scalar_na_logical_ops_corners(self):
s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10])

with pytest.raises(TypeError):
msg = "Cannot perform.+with a dtyped.+array and scalar of type"
with pytest.raises(TypeError, match=msg):
s & datetime(2005, 1, 1)

s = Series([2, 3, 4, 5, 6, 7, 8, 9, datetime(2005, 1, 1)])
Expand Down Expand Up @@ -451,8 +454,9 @@ def test_logical_ops_label_based(self):
expected = Series([True, True, True], index=index)
tm.assert_series_equal(result, expected)

msg = "Cannot perform.+with a dtyped.+array and scalar of type"
for v in [np.nan, "foo"]:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
t | v

for v in [False, 0]:
Expand All @@ -469,8 +473,9 @@ def test_logical_ops_label_based(self):
result = Series([True, False, True], index=index) & v
expected = Series([False, False, False], index=index)
tm.assert_series_equal(result, expected)
msg = "Cannot perform.+with a dtyped.+array and scalar of type"
for v in [np.nan]:
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
t & v

def test_logical_ops_df_compat(self):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/tools/test_to_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def test_parsers_time(self):
res = to_time(arg, format="%I:%M%p", errors="ignore")
tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

with pytest.raises(ValueError):
msg = "Cannot convert.+to a time with given format"
with pytest.raises(ValueError, match=msg):
to_time(arg, format="%I:%M%p", errors="raise")

tm.assert_series_equal(
Expand Down