Skip to content

TST: bare pytest raises in tests/scalar #32929

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 8 commits into from
Mar 24, 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
13 changes: 10 additions & 3 deletions pandas/tests/scalar/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def test_equal(self):
assert Interval(0, 1) != 0

def test_comparison(self):
with pytest.raises(TypeError, match="unorderable types"):
msg = "unorderable types"
with pytest.raises(TypeError, match=msg):
Interval(0, 1) < 2

assert Interval(0, 1) < Interval(1, 2)
Expand Down Expand Up @@ -254,6 +255,12 @@ def test_constructor_errors_tz(self, tz_left, tz_right):
# GH 18538
left = Timestamp("2017-01-01", tz=tz_left)
right = Timestamp("2017-01-02", tz=tz_right)
error = TypeError if com.any_none(tz_left, tz_right) else ValueError
with pytest.raises(error):

if com.any_none(tz_left, tz_right):
error = TypeError
msg = "Cannot compare tz-naive and tz-aware timestamps"
else:
error = ValueError
msg = "left and right must have the same time zone"
with pytest.raises(error, match=msg):
Interval(left, right)
8 changes: 5 additions & 3 deletions pandas/tests/scalar/period/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def test_asfreq_near_zero_weekly(self):
def test_to_timestamp_out_of_bounds(self):
# GH#19643, used to incorrectly give Timestamp in 1754
per = Period("0001-01-01", freq="B")
with pytest.raises(OutOfBoundsDatetime):
msg = "Out of bounds nanosecond timestamp"
with pytest.raises(OutOfBoundsDatetime, match=msg):
per.to_timestamp()

def test_asfreq_corner(self):
Expand Down Expand Up @@ -668,9 +669,10 @@ def test_conv_microsecond(self):
assert start.value == per.ordinal * 1000

per2 = Period("2300-01-01", "us")
with pytest.raises(OutOfBoundsDatetime, match="2300-01-01"):
msg = "2300-01-01"
with pytest.raises(OutOfBoundsDatetime, match=msg):
per2.start_time
with pytest.raises(OutOfBoundsDatetime, match="2300-01-01"):
with pytest.raises(OutOfBoundsDatetime, match=msg):
per2.end_time

def test_asfreq_mult(self):
Expand Down
125 changes: 88 additions & 37 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def test_construction(self):
with pytest.raises(ValueError, match=msg):
Period(ordinal=200701)

with pytest.raises(ValueError, match="Invalid frequency: X"):
msg = "Invalid frequency: X"
with pytest.raises(ValueError, match=msg):
Period("2007-1-1", freq="X")

def test_construction_bday(self):
Expand Down Expand Up @@ -235,26 +236,34 @@ def test_period_constructor_offsets(self):
assert i1 == expected

def test_invalid_arguments(self):
with pytest.raises(ValueError):
msg = "Must supply freq for datetime value"
with pytest.raises(ValueError, match=msg):
Period(datetime.now())
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=msg):
Period(datetime.now().date())

with pytest.raises(ValueError):
msg = "Value must be Period, string, integer, or datetime"
with pytest.raises(ValueError, match=msg):
Period(1.6, freq="D")
with pytest.raises(ValueError):
msg = "Ordinal must be an integer"
with pytest.raises(ValueError, match=msg):
Period(ordinal=1.6, freq="D")
with pytest.raises(ValueError):
msg = "Only value or ordinal but not both should be given but not both"
with pytest.raises(ValueError, match=msg):
Period(ordinal=2, value=1, freq="D")

with pytest.raises(ValueError):
msg = "If value is None, freq cannot be None"
with pytest.raises(ValueError, match=msg):
Period(month=1)

with pytest.raises(ValueError):
msg = "Given date string not likely a datetime"
with pytest.raises(ValueError, match=msg):
Period("-2000", "A")
with pytest.raises(DateParseError):
msg = "day is out of range for month"
with pytest.raises(DateParseError, match=msg):
Period("0", "A")
with pytest.raises(DateParseError):
msg = "Unknown datetime string format, unable to parse"
with pytest.raises(DateParseError, match=msg):
Period("1/1/-2000", "A")

def test_constructor_corner(self):
Expand Down Expand Up @@ -1030,7 +1039,8 @@ def test_sub_delta(self):
result = left - right
assert result == 4 * right.freq

with pytest.raises(IncompatibleFrequency):
msg = r"Input has different freq=M from Period\(freq=A-DEC\)"
with pytest.raises(IncompatibleFrequency, match=msg):
left - Period("2007-01", freq="M")

def test_add_integer(self):
Expand Down Expand Up @@ -1072,10 +1082,14 @@ def test_add_timestamp_raises(self, rbox, lbox):

# We may get a different message depending on which class raises
# the error.
msg = (
r"cannot add|unsupported operand|"
r"can only operate on a|incompatible type|"
r"ufunc add cannot use operands"
msg = "|".join(
[
"cannot add",
"unsupported operand",
"can only operate on a",
"incompatible type",
"ufunc add cannot use operands",
]
)
with pytest.raises(TypeError, match=msg):
lbox(ts) + rbox(per)
Expand Down Expand Up @@ -1148,14 +1162,22 @@ def test_add_offset(self):
np.timedelta64(365, "D"),
timedelta(365),
]:
with pytest.raises(IncompatibleFrequency):
msg = "Input has different freq|Input cannot be converted to Period"
with pytest.raises(IncompatibleFrequency, match=msg):
p + o

if isinstance(o, np.timedelta64):
with pytest.raises(TypeError):
msg = "cannot use operands with types"
with pytest.raises(TypeError, match=msg):
o + p
else:
with pytest.raises(IncompatibleFrequency):
msg = "|".join(
[
"Input has different freq",
"Input cannot be converted to Period",
]
)
with pytest.raises(IncompatibleFrequency, match=msg):
o + p

for freq in ["M", "2M", "3M"]:
Expand All @@ -1175,14 +1197,22 @@ def test_add_offset(self):
np.timedelta64(365, "D"),
timedelta(365),
]:
with pytest.raises(IncompatibleFrequency):
msg = "Input has different freq|Input cannot be converted to Period"
with pytest.raises(IncompatibleFrequency, match=msg):
p + o

if isinstance(o, np.timedelta64):
with pytest.raises(TypeError):
msg = "cannot use operands with types"
with pytest.raises(TypeError, match=msg):
o + p
else:
with pytest.raises(IncompatibleFrequency):
msg = "|".join(
[
"Input has different freq",
"Input cannot be converted to Period",
]
)
with pytest.raises(IncompatibleFrequency, match=msg):
o + p

# freq is Tick
Expand All @@ -1199,12 +1229,13 @@ def test_add_offset(self):

exp = Period("2011-04-03", freq=freq)
assert p + np.timedelta64(2, "D") == exp
with pytest.raises(TypeError):
msg = "cannot use operands with types"
with pytest.raises(TypeError, match=msg):
np.timedelta64(2, "D") + p

exp = Period("2011-04-02", freq=freq)
assert p + np.timedelta64(3600 * 24, "s") == exp
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
np.timedelta64(3600 * 24, "s") + p

exp = Period("2011-03-30", freq=freq)
Expand All @@ -1222,14 +1253,22 @@ def test_add_offset(self):
np.timedelta64(4, "h"),
timedelta(hours=23),
]:
with pytest.raises(IncompatibleFrequency):
msg = "Input has different freq|Input cannot be converted to Period"
with pytest.raises(IncompatibleFrequency, match=msg):
p + o

if isinstance(o, np.timedelta64):
with pytest.raises(TypeError):
msg = "cannot use operands with types"
with pytest.raises(TypeError, match=msg):
o + p
else:
with pytest.raises(IncompatibleFrequency):
msg = "|".join(
[
"Input has different freq",
"Input cannot be converted to Period",
]
)
with pytest.raises(IncompatibleFrequency, match=msg):
o + p

for freq in ["H", "2H", "3H"]:
Expand All @@ -1243,14 +1282,15 @@ def test_add_offset(self):
assert p + offsets.Hour(3) == exp
assert offsets.Hour(3) + p == exp

msg = "cannot use operands with types"
exp = Period("2011-04-01 12:00", freq=freq)
assert p + np.timedelta64(3, "h") == exp
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
np.timedelta64(3, "h") + p

exp = Period("2011-04-01 10:00", freq=freq)
assert p + np.timedelta64(3600, "s") == exp
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
np.timedelta64(3600, "s") + p

exp = Period("2011-04-01 11:00", freq=freq)
Expand All @@ -1268,18 +1308,27 @@ def test_add_offset(self):
np.timedelta64(3200, "s"),
timedelta(hours=23, minutes=30),
]:
with pytest.raises(IncompatibleFrequency):
msg = "Input has different freq|Input cannot be converted to Period"
with pytest.raises(IncompatibleFrequency, match=msg):
p + o

if isinstance(o, np.timedelta64):
with pytest.raises(TypeError):
msg = "cannot use operands with types"
with pytest.raises(TypeError, match=msg):
o + p
else:
with pytest.raises(IncompatibleFrequency):
msg = "|".join(
[
"Input has different freq",
"Input cannot be converted to Period",
]
)
with pytest.raises(IncompatibleFrequency, match=msg):
o + p

def test_sub_offset(self):
# freq is DateOffset
msg = "Input has different freq|Input cannot be converted to Period"
for freq in ["A", "2A", "3A"]:
p = Period("2011", freq=freq)
assert p - offsets.YearEnd(2) == Period("2009", freq=freq)
Expand All @@ -1291,7 +1340,7 @@ def test_sub_offset(self):
np.timedelta64(365, "D"),
timedelta(365),
]:
with pytest.raises(IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency, match=msg):
p - o

for freq in ["M", "2M", "3M"]:
Expand All @@ -1306,7 +1355,7 @@ def test_sub_offset(self):
np.timedelta64(365, "D"),
timedelta(365),
]:
with pytest.raises(IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency, match=msg):
p - o

# freq is Tick
Expand All @@ -1326,7 +1375,7 @@ def test_sub_offset(self):
np.timedelta64(4, "h"),
timedelta(hours=23),
]:
with pytest.raises(IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency, match=msg):
p - o

for freq in ["H", "2H", "3H"]:
Expand All @@ -1349,7 +1398,7 @@ def test_sub_offset(self):
np.timedelta64(3200, "s"),
timedelta(hours=23, minutes=30),
]:
with pytest.raises(IncompatibleFrequency):
with pytest.raises(IncompatibleFrequency, match=msg):
p - o

@pytest.mark.parametrize("freq", ["M", "2M", "3M"])
Expand Down Expand Up @@ -1377,12 +1426,14 @@ def test_period_ops_offset(self):

def test_period_immutable():
# see gh-17116
msg = "not writable"

per = Period("2014Q1")
with pytest.raises(AttributeError):
with pytest.raises(AttributeError, match=msg):
per.ordinal = 14

freq = per.freq
with pytest.raises(AttributeError):
with pytest.raises(AttributeError, match=msg):
per.freq = 2 * freq


Expand Down
18 changes: 12 additions & 6 deletions pandas/tests/scalar/test_na_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def test_repr():


def test_truthiness():
with pytest.raises(TypeError):
msg = "boolean value of NA is ambiguous"

with pytest.raises(TypeError, match=msg):
bool(NA)

with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
not NA


Expand Down Expand Up @@ -145,7 +147,8 @@ def test_logical_and():
assert False & NA is False
assert NA & NA is NA

with pytest.raises(TypeError):
msg = "unsupported operand type"
with pytest.raises(TypeError, match=msg):
NA & 5


Expand All @@ -157,7 +160,8 @@ def test_logical_or():
assert False | NA is NA
assert NA | NA is NA

with pytest.raises(TypeError):
msg = "unsupported operand type"
with pytest.raises(TypeError, match=msg):
NA | 5


Expand All @@ -169,7 +173,8 @@ def test_logical_xor():
assert False ^ NA is NA
assert NA ^ NA is NA

with pytest.raises(TypeError):
msg = "unsupported operand type"
with pytest.raises(TypeError, match=msg):
NA ^ 5


Expand Down Expand Up @@ -216,7 +221,8 @@ def test_ufunc():


def test_ufunc_raises():
with pytest.raises(ValueError, match="ufunc method 'at'"):
msg = "ufunc method 'at'"
with pytest.raises(ValueError, match=msg):
np.log.at(pd.NA, 0)


Expand Down
Loading