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 6 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
6 changes: 5 additions & 1 deletion pandas/tests/scalar/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,9 @@ def test_constructor_errors_tz(self, tz_left, tz_right):
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):
msg = (
"left and right must have the same time zone|"
"Cannot compare tz-naive and tz-aware timestamps"
)
with pytest.raises(error, match=msg):
Interval(left, right)
3 changes: 2 additions & 1 deletion 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
104 changes: 71 additions & 33 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,26 +235,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 +1038,8 @@ def test_sub_delta(self):
result = left - right
assert result == 4 * right.freq

with pytest.raises(IncompatibleFrequency):
msg = "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 @@ -1148,14 +1157,20 @@ 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 = (
"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 +1190,20 @@ 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 = (
"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 +1220,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 +1244,20 @@ 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):
if isinstance(o, np.timedelta64,):
msg = "cannot use operands with types"
with pytest.raises(TypeError, match=msg):
o + p
else:
with pytest.raises(IncompatibleFrequency):
msg = (
"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 +1271,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 +1297,25 @@ 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 = (
"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 +1327,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 +1342,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 +1362,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 +1385,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 +1413,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
12 changes: 7 additions & 5 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,7 @@ def test_logical_and():
assert False & NA is False
assert NA & NA is NA

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


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

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


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

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


Expand Down
Loading