From de03cf2ac173d13f5ad7470008b92f69f63a993d Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 23 Mar 2020 20:41:07 +0700 Subject: [PATCH 1/8] fix bare pytest raise in tests/scalar --- pandas/tests/scalar/interval/test_interval.py | 6 +- pandas/tests/scalar/period/test_asfreq.py | 3 +- pandas/tests/scalar/period/test_period.py | 104 ++++++++++++------ pandas/tests/scalar/test_na_scalar.py | 12 +- .../tests/scalar/timedelta/test_arithmetic.py | 91 +++++++++------ .../scalar/timedelta/test_constructors.py | 21 ++-- .../tests/scalar/timedelta/test_timedelta.py | 18 +-- .../tests/scalar/timestamp/test_arithmetic.py | 14 ++- .../scalar/timestamp/test_comparisons.py | 28 ++--- .../scalar/timestamp/test_constructors.py | 63 +++++++---- .../tests/scalar/timestamp/test_timezones.py | 43 +++++--- .../tests/scalar/timestamp/test_unary_ops.py | 9 +- 12 files changed, 265 insertions(+), 147 deletions(-) diff --git a/pandas/tests/scalar/interval/test_interval.py b/pandas/tests/scalar/interval/test_interval.py index b51429d0338e3..6762c7b718d99 100644 --- a/pandas/tests/scalar/interval/test_interval.py +++ b/pandas/tests/scalar/interval/test_interval.py @@ -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) diff --git a/pandas/tests/scalar/period/test_asfreq.py b/pandas/tests/scalar/period/test_asfreq.py index 436810042186a..520d2e748c50d 100644 --- a/pandas/tests/scalar/period/test_asfreq.py +++ b/pandas/tests/scalar/period/test_asfreq.py @@ -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): diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 1fee40c2a902b..5bb1e1e42cf64 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -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): @@ -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): @@ -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 = "ufunc 'add' 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"]: @@ -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 = "ufunc 'add' 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 @@ -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 = "ufunc 'add' 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) @@ -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 = "ufunc 'add' 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"]: @@ -1243,14 +1271,15 @@ def test_add_offset(self): assert p + offsets.Hour(3) == exp assert offsets.Hour(3) + p == exp + msg = "ufunc 'add' 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) @@ -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 = "ufunc 'add' 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) @@ -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"]: @@ -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 @@ -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"]: @@ -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"]) @@ -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 diff --git a/pandas/tests/scalar/test_na_scalar.py b/pandas/tests/scalar/test_na_scalar.py index 07656de2e9062..c7e0340eacaa4 100644 --- a/pandas/tests/scalar/test_na_scalar.py +++ b/pandas/tests/scalar/test_na_scalar.py @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index 3cb868dd88605..8207c2120e2c6 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -89,10 +89,11 @@ def test_td_add_datetimelike_scalar(self, op): assert result is NaT def test_td_add_timestamp_overflow(self): - with pytest.raises(OverflowError): + msg = "Python int too large to convert to C long" + with pytest.raises(OverflowError, match=msg): Timestamp("1700-01-01") + Timedelta(13 * 19999, unit="D") - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): Timestamp("1700-01-01") + timedelta(days=13 * 19999) @pytest.mark.parametrize("op", [operator.add, ops.radd]) @@ -180,14 +181,15 @@ def test_td_sub_offset(self): def test_td_add_sub_numeric_raises(self): td = Timedelta(10, unit="d") + msg = "unsupported operand type" for other in [2, 2.0, np.int64(2), np.float64(2)]: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): td + other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): other + td - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): td - other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): other - td def test_td_rsub_nat(self): @@ -228,7 +230,8 @@ def test_td_rsub_mixed_most_timedeltalike_object_dtype_array(self): # GH#21980 now = Timestamp.now() arr = np.array([now, Timedelta("1D"), np.timedelta64(2, "h")]) - with pytest.raises(TypeError): + msg = r"unsupported operand type\(s\) for \-: 'Timedelta' and 'Timestamp'" + with pytest.raises(TypeError, match=msg): Timedelta("1D") - arr @pytest.mark.parametrize("op", [operator.add, ops.radd]) @@ -322,7 +325,11 @@ class TestTimedeltaMultiplicationDivision: def test_td_mul_nat(self, op, td_nat): # GH#19819 td = Timedelta(10, unit="d") - with pytest.raises(TypeError): + msg = ( + "ufunc 'multiply' cannot use operands with types|" + "Cannot multiply Timedelta with NaT" + ) + with pytest.raises(TypeError, match=msg): op(td, td_nat) @pytest.mark.parametrize("nan", [np.nan, np.float64("NaN"), float("nan")]) @@ -349,11 +356,12 @@ def test_td_mul_scalar(self, op): assert op(-1, td).value == -1 * td.value assert op(-1.0, td).value == -1.0 * td.value - with pytest.raises(TypeError): + msg = "unsupported operand type" + with pytest.raises(TypeError, match=msg): # timedelta * datetime is gibberish op(td, Timestamp(2016, 1, 2)) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): # invalid multiply with another timedelta op(td, td) @@ -509,7 +517,8 @@ def test_td_floordiv_invalid_scalar(self): # GH#18846 td = Timedelta(hours=3, minutes=4) - with pytest.raises(TypeError): + msg = r"Invalid dtype datetime64\[D\] for __floordiv__" + with pytest.raises(TypeError, match=msg): td // np.datetime64("2016-01-01", dtype="datetime64[us]") def test_td_floordiv_numeric_scalar(self): @@ -580,7 +589,8 @@ def test_td_rfloordiv_invalid_scalar(self): td = Timedelta(hours=3, minutes=3) dt64 = np.datetime64("2016-01-01", "us") - with pytest.raises(TypeError): + msg = r"Invalid dtype datetime64\[us\] for __floordiv__" + with pytest.raises(TypeError, match=msg): td.__rfloordiv__(dt64) def test_td_rfloordiv_numeric_scalar(self): @@ -591,11 +601,12 @@ def test_td_rfloordiv_numeric_scalar(self): assert td.__rfloordiv__(3.5) is NotImplemented assert td.__rfloordiv__(2) is NotImplemented - with pytest.raises(TypeError): + msg = "Invalid dtype" + with pytest.raises(TypeError, match=msg): td.__rfloordiv__(np.float64(2.0)) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): td.__rfloordiv__(np.uint8(9)) - with pytest.raises(TypeError, match="Invalid dtype"): + with pytest.raises(TypeError, match=msg): # deprecated GH#19761, enforced GH#29797 td.__rfloordiv__(np.int32(2.0)) @@ -697,11 +708,11 @@ def test_mod_numeric(self): def test_mod_invalid(self): # GH#19365 td = Timedelta(hours=37) - - with pytest.raises(TypeError): + msg = "unsupported operand type" + with pytest.raises(TypeError, match=msg): td % Timestamp("2018-01-22") - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): td % [] def test_rmod_pytimedelta(self): @@ -723,16 +734,18 @@ def test_rmod_invalid(self): # GH#19365 td = Timedelta(minutes=3) - with pytest.raises(TypeError): + msg = "unsupported operand" + with pytest.raises(TypeError, match=msg): Timestamp("2018-01-22") % td - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): 15 % td - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): 16.0 % td - with pytest.raises(TypeError): + msg = "Invalid dtype int64 for __mod__" + with pytest.raises(TypeError, match=msg): np.array([22, 24]) % td # ---------------------------------------------------------------- @@ -783,7 +796,8 @@ def test_divmod_invalid(self): # GH#19365 td = Timedelta(days=2, hours=6) - with pytest.raises(TypeError): + msg = r"unsupported operand type\(s\) for //: 'Timedelta' and 'Timestamp'" + with pytest.raises(TypeError, match=msg): divmod(td, Timestamp("2018-01-22")) def test_rdivmod_pytimedelta(self): @@ -802,17 +816,19 @@ def test_rdivmod_offset(self): def test_rdivmod_invalid(self): # GH#19365 td = Timedelta(minutes=3) + msg = "unsupported operand type" - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): divmod(Timestamp("2018-01-22"), td) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): divmod(15, td) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): divmod(16.0, td) - with pytest.raises(TypeError): + msg = "Invalid dtype int64 for __mod__" + with pytest.raises(TypeError, match=msg): divmod(np.array([22, 24]), td) # ---------------------------------------------------------------- @@ -828,7 +844,8 @@ def test_rdivmod_invalid(self): ], ) def test_td_op_timedelta_timedeltalike_array(self, op, arr): - with pytest.raises(TypeError): + msg = "unsupported operand type|" "cannot use operands with types" + with pytest.raises(TypeError, match=msg): op(arr, Timedelta("1D")) @@ -918,13 +935,14 @@ def __gt__(self, other): def test_compare_unknown_type(self, val): # GH#20829 t = Timedelta("1s") - with pytest.raises(TypeError): + msg = "Cannot compare type Timedelta with type (int|str)" + with pytest.raises(TypeError, match=msg): t >= val - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): t > val - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): t <= val - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): t < val @@ -948,10 +966,15 @@ def test_ops_error_str(): for left, right in [(td, "a"), ("a", td)]: - with pytest.raises(TypeError): + msg = ( + "unsupported operand type|" + r"can only concatenate str \(not \"Timedelta\"\) to str" + ) + with pytest.raises(TypeError, match=msg): left + right - with pytest.raises(TypeError): + msg = "Cannot compare type" + with pytest.raises(TypeError, match=msg): left > right assert not left == right diff --git a/pandas/tests/scalar/timedelta/test_constructors.py b/pandas/tests/scalar/timedelta/test_constructors.py index d32d1994cac74..23a80e69a0e13 100644 --- a/pandas/tests/scalar/timedelta/test_constructors.py +++ b/pandas/tests/scalar/timedelta/test_constructors.py @@ -79,15 +79,17 @@ def test_construction(): # Currently invalid as it has a - on the hh:mm:dd part # (only allowed on the days) - with pytest.raises(ValueError): + msg = "only leading negative signs are allowed" + with pytest.raises(ValueError, match=msg): Timedelta("-10 days -1 h 1.5m 1s 3us") # only leading neg signs are allowed - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Timedelta("10 days -1 h 1.5m 1s 3us") # no units specified - with pytest.raises(ValueError): + msg = "no units specified" + with pytest.raises(ValueError, match=msg): Timedelta("3.1415") # invalid construction @@ -121,7 +123,7 @@ def test_construction(): assert result == expected assert to_timedelta(offsets.Hour(2)) == Timedelta("0 days, 02:00:00") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="unit abbreviation w/o a number"): Timedelta("foo bar") @@ -177,16 +179,18 @@ def test_td_from_repr_roundtrip(val): def test_overflow_on_construction(): + msg = "Python int too large to convert to C long" + # GH#3374 value = Timedelta("1day").value * 20169940 - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): Timedelta(value) # xref GH#17637 - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): Timedelta(7 * 19999, unit="D") - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): Timedelta(timedelta(days=13 * 19999)) @@ -272,7 +276,8 @@ def test_td_constructor_on_nanoseconds(constructed_td, conversion): def test_td_constructor_value_error(): - with pytest.raises(TypeError): + msg = "Invalid type . Must be int or float." + with pytest.raises(TypeError, match=msg): Timedelta(nanoseconds="abc") diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 0f2486be3a626..0f29030471ea8 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -408,9 +408,11 @@ def conv(v): assert Timedelta(" - 10000D ") == -conv(np.timedelta64(10000, "D")) # invalid - with pytest.raises(ValueError): + msg = "invalid unit abbreviation" + with pytest.raises(ValueError, match=msg): Timedelta("1foo") - with pytest.raises(ValueError): + msg = "unit abbreviation w/o a number" + with pytest.raises(ValueError, match=msg): Timedelta("foo") def test_full_format_converters(self): @@ -439,7 +441,8 @@ def conv(v): ) # invalid - with pytest.raises(ValueError): + msg = "have leftover units" + with pytest.raises(ValueError, match=msg): Timedelta("- 1days, 00") def test_pickle(self): @@ -476,20 +479,21 @@ def test_implementation_limits(self): # Beyond lower limit, a NAT before the Overflow assert (min_td - Timedelta(1, "ns")) is NaT - with pytest.raises(OverflowError): + msg = "Python int too large to convert to C long" + with pytest.raises(OverflowError, match=msg): min_td - Timedelta(2, "ns") - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): max_td + Timedelta(1, "ns") # Same tests using the internal nanosecond values td = Timedelta(min_td.value - 1, "ns") assert td is NaT - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): Timedelta(min_td.value - 2, "ns") - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): Timedelta(max_td.value + 1, "ns") def test_total_seconds_precision(self): diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index ccd7bf721430a..ca795cc54c164 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -195,7 +195,8 @@ def test_add_int_no_freq_raises(self, ts, other): with pytest.raises(TypeError, match=msg): ts - other - with pytest.raises(TypeError): + msg = "unsupported operand type" + with pytest.raises(TypeError, match=msg): other - ts @pytest.mark.parametrize( @@ -215,14 +216,15 @@ def test_add_int_no_freq_raises(self, ts, other): ], ) def test_add_int_with_freq(self, ts, other): - - with pytest.raises(TypeError): + msg = "Addition/subtraction of integers and integer-arrays" + with pytest.raises(TypeError, match=msg): ts + other - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): other + ts - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): ts - other - with pytest.raises(TypeError): + msg = "unsupported operand type" + with pytest.raises(TypeError, match=msg): other - ts diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py index fce4fa6eb1eaa..4581e736b2ea1 100644 --- a/pandas/tests/scalar/timestamp/test_comparisons.py +++ b/pandas/tests/scalar/timestamp/test_comparisons.py @@ -28,7 +28,8 @@ def test_comparison_object_array(self): # tzaware mismatch arr = np.array([naive], dtype=object) - with pytest.raises(TypeError): + msg = "Cannot compare tz-naive and tz-aware timestamps" + with pytest.raises(TypeError, match=msg): arr < ts def test_comparison(self): @@ -85,30 +86,31 @@ def test_cant_compare_tz_naive_w_aware(self, utc_fixture): a = Timestamp("3/12/2012") b = Timestamp("3/12/2012", tz=utc_fixture) - with pytest.raises(TypeError): + msg = "Cannot compare tz-naive and tz-aware timestamps" + with pytest.raises(TypeError, match=msg): a == b - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): a != b - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): a < b - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): a <= b - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): a > b - with pytest.raises(TypeError): + 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): b != a - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): b < a - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): b <= a - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): b > a - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): b >= a assert not a == b.to_pydatetime() diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 4c75d1ebcd377..ca945f5b2eef3 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -189,7 +189,8 @@ def test_constructor_strptime(self): # Test support for Timestamp.strptime fmt = "%Y%m%d-%H%M%S-%f%z" ts = "20190129-235348-000001+0000" - with pytest.raises(NotImplementedError): + msg = r"Timestamp.strptime\(\) is not implemented" + with pytest.raises(NotImplementedError, match=msg): Timestamp.strptime(ts, fmt) def test_constructor_tz_or_tzinfo(self): @@ -206,15 +207,20 @@ def test_constructor_tz_or_tzinfo(self): def test_constructor_positional(self): # see gh-10758 - with pytest.raises(TypeError): + msg = "an integer is required" + with pytest.raises(TypeError, match=msg): Timestamp(2000, 1) - with pytest.raises(ValueError): + + msg = "month must be in 1..12" + with pytest.raises(ValueError, match=msg): Timestamp(2000, 0, 1) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Timestamp(2000, 13, 1) - with pytest.raises(ValueError): + + msg = "day is out of range for month" + with pytest.raises(ValueError, match=msg): Timestamp(2000, 1, 0) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Timestamp(2000, 1, 32) # see gh-11630 @@ -225,15 +231,20 @@ def test_constructor_positional(self): def test_constructor_keyword(self): # GH 10758 - with pytest.raises(TypeError): + msg = "function missing required argument 'day'" + with pytest.raises(TypeError, match=msg): Timestamp(year=2000, month=1) - with pytest.raises(ValueError): + + msg = "month must be in 1..12" + with pytest.raises(ValueError, match=msg): Timestamp(year=2000, month=0, day=1) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Timestamp(year=2000, month=13, day=1) - with pytest.raises(ValueError): + + msg = "day is out of range for month" + with pytest.raises(ValueError, match=msg): Timestamp(year=2000, month=1, day=0) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Timestamp(year=2000, month=1, day=32) assert repr(Timestamp(year=2015, month=11, day=12)) == repr( @@ -313,7 +324,7 @@ def test_constructor_nanosecond(self, result): @pytest.mark.parametrize("z", ["Z0", "Z00"]) def test_constructor_invalid_Z0_isostring(self, z): # GH 8910 - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="could not convert string to Timestamp"): Timestamp(f"2014-11-02 01:00{z}") @pytest.mark.parametrize( @@ -331,14 +342,17 @@ def test_constructor_invalid_Z0_isostring(self, z): ) def test_invalid_date_kwarg_with_string_input(self, arg): kwarg = {arg: 1} - with pytest.raises(ValueError): + msg = "Cannot pass a date attribute keyword argument" + with pytest.raises(ValueError, match=msg): Timestamp("2010-10-10 12:59:59.999999999", **kwarg) def test_out_of_bounds_integer_value(self): # GH#26651 check that we raise OutOfBoundsDatetime, not OverflowError - with pytest.raises(OutOfBoundsDatetime): + msg = str(Timestamp.max.value * 2) + with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp(Timestamp.max.value * 2) - with pytest.raises(OutOfBoundsDatetime): + msg = str(Timestamp.min.value * 2) + with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp(Timestamp.min.value * 2) def test_out_of_bounds_value(self): @@ -353,25 +367,28 @@ def test_out_of_bounds_value(self): Timestamp(min_ts_us) Timestamp(max_ts_us) + msg = "Out of bounds" # One us less than the minimum is an error - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Timestamp(min_ts_us - one_us) # One us more than the maximum is an error - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Timestamp(max_ts_us + one_us) def test_out_of_bounds_string(self): - with pytest.raises(ValueError): + msg = "Out of bounds" + with pytest.raises(ValueError, match=msg): Timestamp("1676-01-01") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): Timestamp("2263-01-01") def test_barely_out_of_bounds(self): # GH#19529 # GH#19382 close enough to bounds that dropping nanos would result # in an in-bounds datetime - with pytest.raises(OutOfBoundsDatetime): + msg = "Out of bounds" + with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp("2262-04-11 23:47:16.854775808") def test_bounds_with_different_units(self): @@ -382,7 +399,8 @@ def test_bounds_with_different_units(self): for date_string in out_of_bounds_dates: for unit in time_units: dt64 = np.datetime64(date_string, unit) - with pytest.raises(ValueError): + msg = "Out of bounds" + with pytest.raises(ValueError, match=msg): Timestamp(dt64) in_bounds_dates = ("1677-09-23", "2262-04-11") @@ -449,7 +467,8 @@ def test_today(self): def test_disallow_setting_tz(self, tz): # GH 3746 ts = Timestamp("2010") - with pytest.raises(AttributeError): + msg = "Cannot directly set timezone" + with pytest.raises(AttributeError, match=msg): ts.tz = tz @pytest.mark.parametrize("offset", ["+0300", "+0200"]) diff --git a/pandas/tests/scalar/timestamp/test_timezones.py b/pandas/tests/scalar/timestamp/test_timezones.py index cfa7da810ada1..bc37367282806 100644 --- a/pandas/tests/scalar/timestamp/test_timezones.py +++ b/pandas/tests/scalar/timestamp/test_timezones.py @@ -26,14 +26,14 @@ def test_tz_localize_pushes_out_of_bounds(self): pac = Timestamp.min.tz_localize("US/Pacific") assert pac.value > Timestamp.min.value pac.tz_convert("Asia/Tokyo") # tz_convert doesn't change value - with pytest.raises(OutOfBoundsDatetime): + with pytest.raises(OutOfBoundsDatetime, match="^$"): Timestamp.min.tz_localize("Asia/Tokyo") # tz_localize that pushes away from the boundary is OK tokyo = Timestamp.max.tz_localize("Asia/Tokyo") assert tokyo.value < Timestamp.max.value tokyo.tz_convert("US/Pacific") # tz_convert doesn't change value - with pytest.raises(OutOfBoundsDatetime): + with pytest.raises(OutOfBoundsDatetime, match="^$"): Timestamp.max.tz_localize("US/Pacific") def test_tz_localize_ambiguous_bool(self): @@ -43,7 +43,8 @@ def test_tz_localize_ambiguous_bool(self): expected0 = Timestamp("2015-11-01 01:00:03-0500", tz="US/Central") expected1 = Timestamp("2015-11-01 01:00:03-0600", tz="US/Central") - with pytest.raises(pytz.AmbiguousTimeError): + msg = "Cannot infer dst time from 2015-11-01 01:00:03" + with pytest.raises(pytz.AmbiguousTimeError, match=msg): ts.tz_localize("US/Central") result = ts.tz_localize("US/Central", ambiguous=True) @@ -58,7 +59,8 @@ def test_tz_localize_ambiguous(self): ts_no_dst = ts.tz_localize("US/Eastern", ambiguous=False) assert (ts_no_dst.value - ts_dst.value) / 1e9 == 3600 - with pytest.raises(ValueError): + msg = "Cannot infer offset with only one time" + with pytest.raises(ValueError, match=msg): ts.tz_localize("US/Eastern", ambiguous="infer") # GH#8025 @@ -82,24 +84,29 @@ def test_tz_localize_ambiguous(self): def test_tz_localize_nonexistent(self, stamp, tz): # GH#13057 ts = Timestamp(stamp) - with pytest.raises(NonExistentTimeError): + with pytest.raises(NonExistentTimeError, match=stamp): ts.tz_localize(tz) # GH 22644 - with pytest.raises(NonExistentTimeError): + with pytest.raises(NonExistentTimeError, match=stamp): ts.tz_localize(tz, nonexistent="raise") assert ts.tz_localize(tz, nonexistent="NaT") is NaT def test_tz_localize_ambiguous_raise(self): # GH#13057 ts = Timestamp("2015-11-1 01:00") - with pytest.raises(AmbiguousTimeError): + msg = "Cannot infer dst time from 2015-11-01 01:00:00," + with pytest.raises(AmbiguousTimeError, match=msg): ts.tz_localize("US/Pacific", ambiguous="raise") def test_tz_localize_nonexistent_invalid_arg(self): # GH 22644 tz = "Europe/Warsaw" ts = Timestamp("2015-03-29 02:00:00") - with pytest.raises(ValueError): + msg = ( + "The nonexistent argument must be one of 'raise', 'NaT', " + "'shift_forward', 'shift_backward' or a timedelta object" + ) + with pytest.raises(ValueError, match=msg): ts.tz_localize(tz, nonexistent="foo") @pytest.mark.parametrize( @@ -117,7 +124,7 @@ def test_tz_localize_roundtrip(self, stamp, tz_aware_fixture): localized = ts.tz_localize(tz) assert localized == Timestamp(stamp, tz=tz) - with pytest.raises(TypeError): + with pytest.raises(TypeError, match="Cannot localize tz-aware Timestamp"): localized.tz_localize(tz) reset = localized.tz_localize(None) @@ -249,9 +256,14 @@ def test_timestamp_tz_localize_nonexistent_NaT(self, tz): def test_timestamp_tz_localize_nonexistent_raise(self, tz): # GH 8917 ts = Timestamp("2015-03-29 02:20:00") - with pytest.raises(pytz.NonExistentTimeError): + msg = "2015-03-29 02:20:00" + with pytest.raises(pytz.NonExistentTimeError, match=msg): ts.tz_localize(tz, nonexistent="raise") - with pytest.raises(ValueError): + msg = ( + "The nonexistent argument must be one of 'raise', 'NaT', " + "'shift_forward', 'shift_backward' or a timedelta object" + ) + with pytest.raises(ValueError, match=msg): ts.tz_localize(tz, nonexistent="foo") # ------------------------------------------------------------------ @@ -327,14 +339,16 @@ def test_timestamp_constructor_near_dst_boundary(self): expected = Timestamp("2015-10-25 01:00").tz_localize(tz) assert result == expected - with pytest.raises(pytz.AmbiguousTimeError): + msg = "Cannot infer dst time from 2015-10-25 02:00:00" + with pytest.raises(pytz.AmbiguousTimeError, match=msg): Timestamp("2015-10-25 02:00", tz=tz) result = Timestamp("2017-03-26 01:00", tz="Europe/Paris") expected = Timestamp("2017-03-26 01:00").tz_localize("Europe/Paris") assert result == expected - with pytest.raises(pytz.NonExistentTimeError): + msg = "2017-03-26 02:00" + with pytest.raises(pytz.NonExistentTimeError, match=msg): Timestamp("2017-03-26 02:00", tz="Europe/Paris") # GH#11708 @@ -352,7 +366,8 @@ def test_timestamp_constructor_near_dst_boundary(self): expected = Timestamp("2017-03-26 01:00:00+0100", tz="Europe/Paris") assert result == expected - with pytest.raises(pytz.NonExistentTimeError): + msg = "2017-03-26 02:00" + with pytest.raises(pytz.NonExistentTimeError, match=msg): Timestamp("2017-03-26 02:00", tz="Europe/Paris") result = Timestamp("2017-03-26 02:00:00+0100", tz="Europe/Paris") diff --git a/pandas/tests/scalar/timestamp/test_unary_ops.py b/pandas/tests/scalar/timestamp/test_unary_ops.py index 78e795e71cd07..e2264ae9cb8cc 100644 --- a/pandas/tests/scalar/timestamp/test_unary_ops.py +++ b/pandas/tests/scalar/timestamp/test_unary_ops.py @@ -166,7 +166,8 @@ def test_round_dst_border_ambiguous(self, method): result = getattr(ts, method)("H", ambiguous="NaT") assert result is NaT - with pytest.raises(pytz.AmbiguousTimeError): + msg = "Cannot infer dst time" + with pytest.raises(pytz.AmbiguousTimeError, match=msg): getattr(ts, method)("H", ambiguous="raise") @pytest.mark.parametrize( @@ -298,14 +299,16 @@ def test_replace_invalid_kwarg(self, tz_aware_fixture): tz = tz_aware_fixture # GH#14621, GH#7825 ts = Timestamp("2016-01-01 09:00:00.000000123", tz=tz) - with pytest.raises(TypeError): + msg = r"replace\(\) got an unexpected keyword argument" + with pytest.raises(TypeError, match=msg): ts.replace(foo=5) def test_replace_integer_args(self, tz_aware_fixture): tz = tz_aware_fixture # GH#14621, GH#7825 ts = Timestamp("2016-01-01 09:00:00.000000123", tz=tz) - with pytest.raises(ValueError): + msg = "value must be an integer, received for hour" + with pytest.raises(ValueError, match=msg): ts.replace(hour=0.1) def test_replace_tzinfo_equiv_tz_localize_none(self): From 218bf9b5fa9e7b552f5448b67b32ac62fe72764e Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 23 Mar 2020 21:24:39 +0700 Subject: [PATCH 2/8] add msg on orther system --- pandas/tests/scalar/period/test_period.py | 8 ++++---- pandas/tests/scalar/timedelta/test_arithmetic.py | 5 +++-- pandas/tests/scalar/timestamp/test_constructors.py | 5 ++++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 5bb1e1e42cf64..c9250e7ee5daa 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -1157,12 +1157,12 @@ def test_add_offset(self): np.timedelta64(365, "D"), timedelta(365), ]: - msg = "Input has different freq|" "Input cannot be converted to Period" + msg = "Input has different freq|Input cannot be converted to Period" with pytest.raises(IncompatibleFrequency, match=msg): p + o if isinstance(o, np.timedelta64): - msg = "ufunc 'add' cannot use operands with types" + msg = "cannot use operands with types" with pytest.raises(TypeError, match=msg): o + p else: @@ -1190,7 +1190,7 @@ def test_add_offset(self): np.timedelta64(365, "D"), timedelta(365), ]: - msg = "Input has different freq|" "Input cannot be converted to Period" + msg = "Input has different freq|Input cannot be converted to Period" with pytest.raises(IncompatibleFrequency, match=msg): p + o @@ -1220,7 +1220,7 @@ def test_add_offset(self): exp = Period("2011-04-03", freq=freq) assert p + np.timedelta64(2, "D") == exp - msg = "ufunc 'add' cannot use operands with types" + msg = "cannot use operands with types" with pytest.raises(TypeError, match=msg): np.timedelta64(2, "D") + p diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index 8207c2120e2c6..e9cc29acc1b5f 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -326,7 +326,7 @@ def test_td_mul_nat(self, op, td_nat): # GH#19819 td = Timedelta(10, unit="d") msg = ( - "ufunc 'multiply' cannot use operands with types|" + "cannot use operands with types|" "Cannot multiply Timedelta with NaT" ) with pytest.raises(TypeError, match=msg): @@ -968,7 +968,8 @@ def test_ops_error_str(): msg = ( "unsupported operand type|" - r"can only concatenate str \(not \"Timedelta\"\) to str" + r"can only concatenate str \(not \"Timedelta\"\) to str\|" + "must be str, not Timedelta" ) with pytest.raises(TypeError, match=msg): left + right diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index ca945f5b2eef3..23054b3a0a013 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -231,7 +231,10 @@ def test_constructor_positional(self): def test_constructor_keyword(self): # GH 10758 - msg = "function missing required argument 'day'" + msg = ( + "function missing required argument 'day'|" + "Required argument 'day'" + ) with pytest.raises(TypeError, match=msg): Timestamp(year=2000, month=1) From eb6b1dc4551e435fe41b035cd5c6213b5a884490 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 23 Mar 2020 21:25:46 +0700 Subject: [PATCH 3/8] style --- pandas/tests/scalar/timedelta/test_arithmetic.py | 5 +---- pandas/tests/scalar/timestamp/test_constructors.py | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index e9cc29acc1b5f..e5d793db9e5c6 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -325,10 +325,7 @@ class TestTimedeltaMultiplicationDivision: def test_td_mul_nat(self, op, td_nat): # GH#19819 td = Timedelta(10, unit="d") - msg = ( - "cannot use operands with types|" - "Cannot multiply Timedelta with NaT" - ) + msg = "cannot use operands with types|" "Cannot multiply Timedelta with NaT" with pytest.raises(TypeError, match=msg): op(td, td_nat) diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 23054b3a0a013..b0ed5b635fce3 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -231,10 +231,7 @@ def test_constructor_positional(self): def test_constructor_keyword(self): # GH 10758 - msg = ( - "function missing required argument 'day'|" - "Required argument 'day'" - ) + msg = "function missing required argument 'day'|" "Required argument 'day'" with pytest.raises(TypeError, match=msg): Timestamp(year=2000, month=1) From 7774d9121c081bde93521416c7d0ca72004e475d Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 23 Mar 2020 21:46:40 +0700 Subject: [PATCH 4/8] " " --- pandas/tests/scalar/period/test_period.py | 6 +++--- pandas/tests/scalar/timedelta/test_arithmetic.py | 13 ++++++++----- pandas/tests/scalar/timestamp/test_constructors.py | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index c9250e7ee5daa..60e29bc5c458d 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -1244,7 +1244,7 @@ def test_add_offset(self): np.timedelta64(4, "h"), timedelta(hours=23), ]: - msg = "Input has different freq|" "Input cannot be converted to Period" + msg = "Input has different freq|Input cannot be converted to Period" with pytest.raises(IncompatibleFrequency, match=msg): p + o @@ -1297,7 +1297,7 @@ def test_add_offset(self): np.timedelta64(3200, "s"), timedelta(hours=23, minutes=30), ]: - msg = "Input has different freq|" "Input cannot be converted to Period" + msg = "Input has different freq|Input cannot be converted to Period" with pytest.raises(IncompatibleFrequency, match=msg): p + o @@ -1315,7 +1315,7 @@ def test_add_offset(self): def test_sub_offset(self): # freq is DateOffset - msg = "Input has different freq|" "Input cannot be converted to Period" + 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) diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index e5d793db9e5c6..edb4b36d43779 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -325,7 +325,7 @@ class TestTimedeltaMultiplicationDivision: def test_td_mul_nat(self, op, td_nat): # GH#19819 td = Timedelta(10, unit="d") - msg = "cannot use operands with types|" "Cannot multiply Timedelta with NaT" + msg = "cannot use operands with types|Cannot multiply Timedelta with NaT" with pytest.raises(TypeError, match=msg): op(td, td_nat) @@ -514,7 +514,10 @@ def test_td_floordiv_invalid_scalar(self): # GH#18846 td = Timedelta(hours=3, minutes=4) - msg = r"Invalid dtype datetime64\[D\] for __floordiv__" + msg = ( + r"Invalid dtype datetime64\[D\] for __floordiv__|" + "'dtype' is an invalid keyword argument for this function" + ) with pytest.raises(TypeError, match=msg): td // np.datetime64("2016-01-01", dtype="datetime64[us]") @@ -841,7 +844,7 @@ def test_rdivmod_invalid(self): ], ) def test_td_op_timedelta_timedeltalike_array(self, op, arr): - msg = "unsupported operand type|" "cannot use operands with types" + msg = "unsupported operand type|cannot use operands with types" with pytest.raises(TypeError, match=msg): op(arr, Timedelta("1D")) @@ -965,8 +968,8 @@ def test_ops_error_str(): msg = ( "unsupported operand type|" - r"can only concatenate str \(not \"Timedelta\"\) to str\|" - "must be str, not Timedelta" + 'can only concatenate str \\(not "Timedelta"\\) to str|' + "must be str, not Timedelta|" ) with pytest.raises(TypeError, match=msg): left + right diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index b0ed5b635fce3..7229b574739c2 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -231,7 +231,7 @@ def test_constructor_positional(self): def test_constructor_keyword(self): # GH 10758 - msg = "function missing required argument 'day'|" "Required argument 'day'" + msg = "function missing required argument 'day'|Required argument 'day'" with pytest.raises(TypeError, match=msg): Timestamp(year=2000, month=1) From 3447afb9cd86bcd6306db4d8de015d577a6dec18 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 23 Mar 2020 22:12:40 +0700 Subject: [PATCH 5/8] --- pandas/tests/scalar/period/test_period.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 60e29bc5c458d..6624d59c37c57 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -1195,7 +1195,7 @@ def test_add_offset(self): p + o if isinstance(o, np.timedelta64): - msg = "ufunc 'add' cannot use operands with types" + msg = "cannot use operands with types" with pytest.raises(TypeError, match=msg): o + p else: @@ -1249,7 +1249,7 @@ def test_add_offset(self): p + o if isinstance(o, np.timedelta64,): - msg = "ufunc 'add' cannot use operands with types" + msg = "cannot use operands with types" with pytest.raises(TypeError, match=msg): o + p else: @@ -1271,7 +1271,7 @@ def test_add_offset(self): assert p + offsets.Hour(3) == exp assert offsets.Hour(3) + p == exp - msg = "ufunc 'add' cannot use operands with types" + 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, match=msg): @@ -1302,7 +1302,7 @@ def test_add_offset(self): p + o if isinstance(o, np.timedelta64): - msg = "ufunc 'add' cannot use operands with types" + msg = "cannot use operands with types" with pytest.raises(TypeError, match=msg): o + p else: From b3595d8bb02855108d62a3239cdcd00e6437b5f3 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 23 Mar 2020 22:42:15 +0700 Subject: [PATCH 6/8] 32bit --- pandas/tests/scalar/timedelta/test_arithmetic.py | 6 +++--- pandas/tests/scalar/timedelta/test_constructors.py | 2 +- pandas/tests/scalar/timedelta/test_timedelta.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index edb4b36d43779..b26754c990d09 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -89,7 +89,7 @@ def test_td_add_datetimelike_scalar(self, op): assert result is NaT def test_td_add_timestamp_overflow(self): - msg = "Python int too large to convert to C long" + msg = "int too (large|big) to convert" with pytest.raises(OverflowError, match=msg): Timestamp("1700-01-01") + Timedelta(13 * 19999, unit="D") @@ -744,7 +744,7 @@ def test_rmod_invalid(self): with pytest.raises(TypeError, match=msg): 16.0 % td - msg = "Invalid dtype int64 for __mod__" + msg = "Invalid dtype int" with pytest.raises(TypeError, match=msg): np.array([22, 24]) % td @@ -827,7 +827,7 @@ def test_rdivmod_invalid(self): with pytest.raises(TypeError, match=msg): divmod(16.0, td) - msg = "Invalid dtype int64 for __mod__" + msg = "Invalid dtype int" with pytest.raises(TypeError, match=msg): divmod(np.array([22, 24]), td) diff --git a/pandas/tests/scalar/timedelta/test_constructors.py b/pandas/tests/scalar/timedelta/test_constructors.py index 23a80e69a0e13..d014435db2861 100644 --- a/pandas/tests/scalar/timedelta/test_constructors.py +++ b/pandas/tests/scalar/timedelta/test_constructors.py @@ -179,7 +179,7 @@ def test_td_from_repr_roundtrip(val): def test_overflow_on_construction(): - msg = "Python int too large to convert to C long" + msg = "int too (large|big) to convert" # GH#3374 value = Timedelta("1day").value * 20169940 diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 0f29030471ea8..38e77321418d1 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -479,7 +479,7 @@ def test_implementation_limits(self): # Beyond lower limit, a NAT before the Overflow assert (min_td - Timedelta(1, "ns")) is NaT - msg = "Python int too large to convert to C long" + msg = "int too (large|big) to convert" with pytest.raises(OverflowError, match=msg): min_td - Timedelta(2, "ns") From 85f89ebf9d249decd379ec5774271442432e1747 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 23 Mar 2020 23:43:16 +0700 Subject: [PATCH 7/8] fix to suggestions --- pandas/tests/scalar/interval/test_interval.py | 11 +++-- pandas/tests/scalar/period/test_asfreq.py | 5 +- pandas/tests/scalar/period/test_period.py | 49 ++++++++++++------- pandas/tests/scalar/test_na_scalar.py | 12 +++-- pandas/tests/scalar/test_nat.py | 14 +++--- .../tests/scalar/timedelta/test_arithmetic.py | 36 +++++++++----- .../scalar/timedelta/test_constructors.py | 9 ++-- .../tests/scalar/timestamp/test_arithmetic.py | 3 +- .../scalar/timestamp/test_constructors.py | 27 ++++++---- .../tests/scalar/timestamp/test_timezones.py | 8 +-- .../tests/scalar/timestamp/test_unary_ops.py | 3 +- 11 files changed, 113 insertions(+), 64 deletions(-) diff --git a/pandas/tests/scalar/interval/test_interval.py b/pandas/tests/scalar/interval/test_interval.py index 6762c7b718d99..ddfa2a4ff6048 100644 --- a/pandas/tests/scalar/interval/test_interval.py +++ b/pandas/tests/scalar/interval/test_interval.py @@ -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) @@ -255,9 +256,11 @@ 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 - msg = ( - "left and right must have the same time zone|" - "Cannot compare tz-naive and tz-aware timestamps" + msg = "|".join( + [ + "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) diff --git a/pandas/tests/scalar/period/test_asfreq.py b/pandas/tests/scalar/period/test_asfreq.py index 520d2e748c50d..b9f637c178d53 100644 --- a/pandas/tests/scalar/period/test_asfreq.py +++ b/pandas/tests/scalar/period/test_asfreq.py @@ -669,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): diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 6624d59c37c57..c42f4c143691b 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -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): @@ -1038,7 +1039,7 @@ def test_sub_delta(self): result = left - right assert result == 4 * right.freq - msg = "Input has different freq=M from Period\\(freq=A-DEC\\)" + msg = r"Input has different freq=M from Period\(freq=A-DEC\)" with pytest.raises(IncompatibleFrequency, match=msg): left - Period("2007-01", freq="M") @@ -1081,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) @@ -1166,9 +1171,11 @@ def test_add_offset(self): with pytest.raises(TypeError, match=msg): o + p else: - msg = ( - "Input has different freq|" - "Input cannot be converted to Period" + msg = "|".join( + [ + "Input has different freq", + "Input cannot be converted to Period", + ] ) with pytest.raises(IncompatibleFrequency, match=msg): o + p @@ -1199,9 +1206,11 @@ def test_add_offset(self): with pytest.raises(TypeError, match=msg): o + p else: - msg = ( - "Input has different freq|" - "Input cannot be converted to Period" + msg = "|".join( + [ + "Input has different freq", + "Input cannot be converted to Period", + ] ) with pytest.raises(IncompatibleFrequency, match=msg): o + p @@ -1253,9 +1262,11 @@ def test_add_offset(self): with pytest.raises(TypeError, match=msg): o + p else: - msg = ( - "Input has different freq|" - "Input cannot be converted to Period" + msg = "|".join( + [ + "Input has different freq", + "Input cannot be converted to Period", + ] ) with pytest.raises(IncompatibleFrequency, match=msg): o + p @@ -1306,9 +1317,11 @@ def test_add_offset(self): with pytest.raises(TypeError, match=msg): o + p else: - msg = ( - "Input has different freq|" - "Input cannot be converted to Period" + msg = "|".join( + [ + "Input has different freq", + "Input cannot be converted to Period", + ] ) with pytest.raises(IncompatibleFrequency, match=msg): o + p diff --git a/pandas/tests/scalar/test_na_scalar.py b/pandas/tests/scalar/test_na_scalar.py index c7e0340eacaa4..a0e3f8984fbe4 100644 --- a/pandas/tests/scalar/test_na_scalar.py +++ b/pandas/tests/scalar/test_na_scalar.py @@ -147,7 +147,8 @@ def test_logical_and(): assert False & NA is False assert NA & NA is NA - with pytest.raises(TypeError, match="unsupported operand type"): + msg = "unsupported operand type" + with pytest.raises(TypeError, match=msg): NA & 5 @@ -159,7 +160,8 @@ def test_logical_or(): assert False | NA is NA assert NA | NA is NA - with pytest.raises(TypeError, match="unsupported operand type"): + msg = "unsupported operand type" + with pytest.raises(TypeError, match=msg): NA | 5 @@ -171,7 +173,8 @@ def test_logical_xor(): assert False ^ NA is NA assert NA ^ NA is NA - with pytest.raises(TypeError, match="unsupported operand type"): + msg = "unsupported operand type" + with pytest.raises(TypeError, match=msg): NA ^ 5 @@ -218,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) diff --git a/pandas/tests/scalar/test_nat.py b/pandas/tests/scalar/test_nat.py index f94b96b47fc05..0e5414a8b4d2d 100644 --- a/pandas/tests/scalar/test_nat.py +++ b/pandas/tests/scalar/test_nat.py @@ -393,12 +393,14 @@ def test_nat_arithmetic_scalar(op_name, value, val_type): elif val_type == "str": # un-specific check here because the message comes from str # and varies by method - msg = ( - "can only concatenate str|" - "unsupported operand type|" - "can't multiply sequence|" - "Can't convert 'NaTType'|" - "must be str, not NaTType" + msg = "|".join( + [ + "can only concatenate str", + "unsupported operand type", + "can't multiply sequence", + "Can't convert 'NaTType'", + "must be str, not NaTType", + ] ) else: msg = "unsupported operand type" diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index b26754c990d09..12572648fca9e 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -457,10 +457,12 @@ def test_td_rdiv_na_scalar(self): result = np.timedelta64("NaT") / td assert np.isnan(result) - with pytest.raises(TypeError, match="cannot use operands with types dtype"): + msg = "cannot use operands with types dtype" + with pytest.raises(TypeError, match=msg): np.datetime64("NaT") / td - with pytest.raises(TypeError, match="Cannot divide float by Timedelta"): + msg = "Cannot divide float by Timedelta" + with pytest.raises(TypeError, match=msg): np.nan / td def test_td_rdiv_ndarray(self): @@ -477,11 +479,13 @@ def test_td_rdiv_ndarray(self): tm.assert_numpy_array_equal(result, expected) arr = np.array([np.nan], dtype=object) - with pytest.raises(TypeError, match="Cannot divide float by Timedelta"): + msg = "Cannot divide float by Timedelta" + with pytest.raises(TypeError, match=msg): arr / td arr = np.array([np.nan], dtype=np.float64) - with pytest.raises(TypeError, match="cannot use operands with types dtype"): + msg = "cannot use operands with types dtype" + with pytest.raises(TypeError, match=msg): arr / td # --------------------------------------------------------------- @@ -514,9 +518,11 @@ def test_td_floordiv_invalid_scalar(self): # GH#18846 td = Timedelta(hours=3, minutes=4) - msg = ( - r"Invalid dtype datetime64\[D\] for __floordiv__|" - "'dtype' is an invalid keyword argument for this function" + msg = "|".join( + [ + r"Invalid dtype datetime64\[D\] for __floordiv__", + "'dtype' is an invalid keyword argument for this function", + ] ) with pytest.raises(TypeError, match=msg): td // np.datetime64("2016-01-01", dtype="datetime64[us]") @@ -631,7 +637,8 @@ def test_td_rfloordiv_intarray(self): # deprecated GH#19761, enforced GH#29797 ints = np.array([1349654400, 1349740800, 1349827200, 1349913600]) * 10 ** 9 - with pytest.raises(TypeError, match="Invalid dtype"): + msg = "Invalid dtype" + with pytest.raises(TypeError, match=msg): ints // Timedelta(1, unit="s") def test_td_rfloordiv_numeric_series(self): @@ -641,7 +648,8 @@ def test_td_rfloordiv_numeric_series(self): res = td.__rfloordiv__(ser) assert res is NotImplemented - with pytest.raises(TypeError, match="Invalid dtype"): + msg = "Invalid dtype" + with pytest.raises(TypeError, match=msg): # Deprecated GH#19761, enforced GH#29797 # TODO: GH-19761. Change to TypeError. ser // td @@ -966,10 +974,12 @@ def test_ops_error_str(): for left, right in [(td, "a"), ("a", td)]: - msg = ( - "unsupported operand type|" - 'can only concatenate str \\(not "Timedelta"\\) to str|' - "must be str, not Timedelta|" + msg = "|".join( + [ + "unsupported operand type", + r'can only concatenate str \(not "Timedelta"\) to str', + "must be str, not Timedelta", + ] ) with pytest.raises(TypeError, match=msg): left + right diff --git a/pandas/tests/scalar/timedelta/test_constructors.py b/pandas/tests/scalar/timedelta/test_constructors.py index d014435db2861..5523d72b1eec9 100644 --- a/pandas/tests/scalar/timedelta/test_constructors.py +++ b/pandas/tests/scalar/timedelta/test_constructors.py @@ -93,10 +93,12 @@ def test_construction(): Timedelta("3.1415") # invalid construction - with pytest.raises(ValueError, match="cannot construct a Timedelta"): + msg = "cannot construct a Timedelta" + with pytest.raises(ValueError, match=msg): Timedelta() - with pytest.raises(ValueError, match="unit abbreviation w/o a number"): + msg = "unit abbreviation w/o a number" + with pytest.raises(ValueError, match=msg): Timedelta("foo") msg = ( @@ -123,7 +125,8 @@ def test_construction(): assert result == expected assert to_timedelta(offsets.Hour(2)) == Timedelta("0 days, 02:00:00") - with pytest.raises(ValueError, match="unit abbreviation w/o a number"): + msg = "unit abbreviation w/o a number" + with pytest.raises(ValueError, match=msg): Timedelta("foo bar") diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index ca795cc54c164..ee70d1d0432fc 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -90,7 +90,8 @@ def test_rsub_dtscalars(self, tz_naive_fixture): if tz_naive_fixture is None: assert other.to_datetime64() - ts == td else: - with pytest.raises(TypeError, match="subtraction must have"): + msg = "subtraction must have" + with pytest.raises(TypeError, match=msg): other.to_datetime64() - ts def test_timestamp_sub_datetime(self): diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 7229b574739c2..6817a4d219718 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -165,20 +165,25 @@ def test_constructor_with_stringoffset(self): assert result == eval(repr(result)) def test_constructor_invalid(self): - with pytest.raises(TypeError, match="Cannot convert input"): + msg = "Cannot convert input" + with pytest.raises(TypeError, match=msg): Timestamp(slice(2)) - with pytest.raises(ValueError, match="Cannot convert Period"): + msg = "Cannot convert Period" + with pytest.raises(ValueError, match=msg): Timestamp(Period("1000-01-01")) def test_constructor_invalid_tz(self): # GH#17690 - with pytest.raises(TypeError, match="must be a datetime.tzinfo"): + msg = "must be a datetime.tzinfo" + with pytest.raises(TypeError, match=msg): Timestamp("2017-10-22", tzinfo="US/Eastern") - with pytest.raises(ValueError, match="at most one of"): + msg = "at most one of" + with pytest.raises(ValueError, match=msg): Timestamp("2017-10-22", tzinfo=pytz.utc, tz="UTC") - with pytest.raises(ValueError, match="Invalid frequency:"): + msg = "Invalid frequency:" + with pytest.raises(ValueError, match=msg): # GH#5168 # case where user tries to pass tz as an arg, not kwarg, gets # interpreted as a `freq` @@ -324,7 +329,8 @@ def test_constructor_nanosecond(self, result): @pytest.mark.parametrize("z", ["Z0", "Z00"]) def test_constructor_invalid_Z0_isostring(self, z): # GH 8910 - with pytest.raises(ValueError, match="could not convert string to Timestamp"): + msg = "could not convert string to Timestamp" + with pytest.raises(ValueError, match=msg): Timestamp(f"2014-11-02 01:00{z}") @pytest.mark.parametrize( @@ -495,16 +501,19 @@ def test_construct_timestamp_preserve_original_frequency(self): def test_constructor_invalid_frequency(self): # GH 22311 - with pytest.raises(ValueError, match="Invalid frequency:"): + msg = "Invalid frequency:" + with pytest.raises(ValueError, match=msg): Timestamp("2012-01-01", freq=[]) @pytest.mark.parametrize("box", [datetime, Timestamp]) def test_raise_tz_and_tzinfo_in_datetime_input(self, box): # GH 23579 kwargs = {"year": 2018, "month": 1, "day": 1, "tzinfo": pytz.utc} - with pytest.raises(ValueError, match="Cannot pass a datetime or Timestamp"): + msg = "Cannot pass a datetime or Timestamp" + with pytest.raises(ValueError, match=msg): Timestamp(box(**kwargs), tz="US/Pacific") - with pytest.raises(ValueError, match="Cannot pass a datetime or Timestamp"): + msg = "Cannot pass a datetime or Timestamp" + with pytest.raises(ValueError, match=msg): Timestamp(box(**kwargs), tzinfo=pytz.timezone("US/Pacific")) def test_dont_convert_dateutil_utc_to_pytz_utc(self): diff --git a/pandas/tests/scalar/timestamp/test_timezones.py b/pandas/tests/scalar/timestamp/test_timezones.py index bc37367282806..9611c827be6fe 100644 --- a/pandas/tests/scalar/timestamp/test_timezones.py +++ b/pandas/tests/scalar/timestamp/test_timezones.py @@ -21,19 +21,20 @@ class TestTimestampTZOperations: # Timestamp.tz_localize def test_tz_localize_pushes_out_of_bounds(self): + msg = "^$" # GH#12677 # tz_localize that pushes away from the boundary is OK pac = Timestamp.min.tz_localize("US/Pacific") assert pac.value > Timestamp.min.value pac.tz_convert("Asia/Tokyo") # tz_convert doesn't change value - with pytest.raises(OutOfBoundsDatetime, match="^$"): + with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp.min.tz_localize("Asia/Tokyo") # tz_localize that pushes away from the boundary is OK tokyo = Timestamp.max.tz_localize("Asia/Tokyo") assert tokyo.value < Timestamp.max.value tokyo.tz_convert("US/Pacific") # tz_convert doesn't change value - with pytest.raises(OutOfBoundsDatetime, match="^$"): + with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp.max.tz_localize("US/Pacific") def test_tz_localize_ambiguous_bool(self): @@ -124,7 +125,8 @@ def test_tz_localize_roundtrip(self, stamp, tz_aware_fixture): localized = ts.tz_localize(tz) assert localized == Timestamp(stamp, tz=tz) - with pytest.raises(TypeError, match="Cannot localize tz-aware Timestamp"): + msg = "Cannot localize tz-aware Timestamp" + with pytest.raises(TypeError, match=msg): localized.tz_localize(tz) reset = localized.tz_localize(None) diff --git a/pandas/tests/scalar/timestamp/test_unary_ops.py b/pandas/tests/scalar/timestamp/test_unary_ops.py index e2264ae9cb8cc..e657559b55d5a 100644 --- a/pandas/tests/scalar/timestamp/test_unary_ops.py +++ b/pandas/tests/scalar/timestamp/test_unary_ops.py @@ -188,7 +188,8 @@ def test_round_dst_border_nonexistent(self, method, ts_str, freq): result = getattr(ts, method)(freq, nonexistent="NaT") assert result is NaT - with pytest.raises(pytz.NonExistentTimeError, match="2018-03-11 02:00:00"): + msg = "2018-03-11 02:00:00" + with pytest.raises(pytz.NonExistentTimeError, match=msg): getattr(ts, method)(freq, nonexistent="raise") @pytest.mark.parametrize( From d8403a52e8e9f5639352f24df688b0a4738b518c Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Tue, 24 Mar 2020 00:33:20 +0700 Subject: [PATCH 8/8] fix to suggestion --- pandas/tests/scalar/interval/test_interval.py | 14 +++++++------- pandas/tests/scalar/period/test_period.py | 2 +- pandas/tests/scalar/timestamp/test_constructors.py | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pandas/tests/scalar/interval/test_interval.py b/pandas/tests/scalar/interval/test_interval.py index ddfa2a4ff6048..b21e98827ca92 100644 --- a/pandas/tests/scalar/interval/test_interval.py +++ b/pandas/tests/scalar/interval/test_interval.py @@ -255,12 +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 - msg = "|".join( - [ - "left and right must have the same time zone", - "Cannot compare tz-naive and tz-aware timestamps", - ] - ) + + 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) diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index c42f4c143691b..304033f82c7a2 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -1257,7 +1257,7 @@ def test_add_offset(self): with pytest.raises(IncompatibleFrequency, match=msg): p + o - if isinstance(o, np.timedelta64,): + if isinstance(o, np.timedelta64): msg = "cannot use operands with types" with pytest.raises(TypeError, match=msg): o + p diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 6817a4d219718..770753f42a4c8 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -393,7 +393,7 @@ def test_barely_out_of_bounds(self): # GH#19529 # GH#19382 close enough to bounds that dropping nanos would result # in an in-bounds datetime - msg = "Out of bounds" + msg = "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16" with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp("2262-04-11 23:47:16.854775808") @@ -406,7 +406,7 @@ def test_bounds_with_different_units(self): for unit in time_units: dt64 = np.datetime64(date_string, unit) msg = "Out of bounds" - with pytest.raises(ValueError, match=msg): + with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp(dt64) in_bounds_dates = ("1677-09-23", "2262-04-11")