From f4b0fc42dfd211ddafdf483a7d4374f6a1daf452 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 14 Jan 2020 13:37:29 +0200 Subject: [PATCH 01/11] TST: insert 'match' to bare pytest raises in pandas/tests/internals/test_internals.py --- pandas/tests/internals/test_internals.py | 50 +++++++++++++++++++----- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 15b1434f8629f..2b298db1eeaa7 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -297,7 +297,10 @@ def test_delete(self): assert (newb.values[1] == 1).all() newb = self.fblock.copy() - with pytest.raises(Exception): + + msg = "index 3 is out of bounds for axis 0 with size 3" + + with pytest.raises(Exception, match=msg): newb.delete(3) @@ -321,7 +324,12 @@ def test_can_hold_element(self): val = date(2010, 10, 10) assert not block._can_hold_element(val) - with pytest.raises(TypeError): + + msg = ( + "'value' should be a 'Timestamp', 'NaT', " + "or array of those. Got 'date' instead." + ) + with pytest.raises(TypeError, match=msg): arr[0] = val @@ -350,7 +358,10 @@ def test_duplicate_ref_loc_failure(self): blocks[1].mgr_locs = np.array([0]) # test trying to create block manager with overlapping ref locs - with pytest.raises(AssertionError): + + msg = "Gaps in blk ref_locs" + + with pytest.raises(AssertionError, match=msg): BlockManager(blocks, axes) blocks[0].mgr_locs = np.array([0]) @@ -807,8 +818,15 @@ def test_validate_bool_args(self): invalid_values = [1, "True", [1, 2, 3], 5.0] bm1 = create_mgr("a,b,c: i8-1; d,e,f: i8-2") + msg = ( + r'For argument "inplace" expected type bool, received type int.|' + r'For argument "inplace" expected type bool, received type str.|' + r'For argument "inplace" expected type bool, received type list.|' + r'For argument "inplace" expected type bool, received type float.' + ) + for value in invalid_values: - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): bm1.replace_list([1], [2], inplace=value) @@ -1027,9 +1045,11 @@ def test_slice_len(self): assert len(BlockPlacement(slice(1, 0, -1))) == 1 def test_zero_step_raises(self): - with pytest.raises(ValueError): + msg = "slice step cannot be zero" + + with pytest.raises(ValueError, match=msg): BlockPlacement(slice(1, 1, 0)) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): BlockPlacement(slice(1, 2, 0)) def test_unbounded_slice_raises(self): @@ -1132,9 +1152,11 @@ def assert_add_equals(val, inc, result): assert_add_equals(slice(1, 4), -1, [0, 1, 2]) assert_add_equals([1, 2, 4], -1, [0, 1, 3]) - with pytest.raises(ValueError): + msg = "iadd causes length change" + + with pytest.raises(ValueError, match=msg): BlockPlacement(slice(1, 4)).add(-10) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): BlockPlacement([1, 2, 4]).add(-10) @@ -1216,7 +1238,17 @@ def test_binop_other(self, op, value, dtype): } if (op, dtype) in invalid: - with pytest.raises(TypeError): + msg = ( + "cannot perform __pow__ with this index type: DatetimeArray|" + "cannot perform __mod__ with this index type: DatetimeArray|" + "cannot perform __truediv__ with this index type: DatetimeArray|" + "cannot perform __mul__ with this index type: DatetimeArray|" + "cannot perform __pow__ with this index type: TimedeltaArray|" + "ufunc 'multiply' cannot use operands with types dtype" + r"\(' Date: Tue, 14 Jan 2020 13:49:37 +0200 Subject: [PATCH 02/11] Changed 'Exception' to 'IndexError' --- pandas/tests/internals/test_internals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 2b298db1eeaa7..ab60083c8bfa5 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -300,7 +300,7 @@ def test_delete(self): msg = "index 3 is out of bounds for axis 0 with size 3" - with pytest.raises(Exception, match=msg): + with pytest.raises(IndexError, match=msg): newb.delete(3) From e5a61cdfac6758fa40574b991e3d34d839912062 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 14 Jan 2020 14:21:10 +0200 Subject: [PATCH 03/11] Fix failing tests --- pandas/tests/internals/test_internals.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index ab60083c8bfa5..3de79ca8ee361 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -1239,11 +1239,11 @@ def test_binop_other(self, op, value, dtype): if (op, dtype) in invalid: msg = ( - "cannot perform __pow__ with this index type: DatetimeArray|" - "cannot perform __mod__ with this index type: DatetimeArray|" - "cannot perform __truediv__ with this index type: DatetimeArray|" - "cannot perform __mul__ with this index type: DatetimeArray|" - "cannot perform __pow__ with this index type: TimedeltaArray|" + r"cannot perform __pow__ with this index type: DatetimeArray|" + r"cannot perform __mod__ with this index type: DatetimeArray|" + r"cannot perform __truediv__ with this index type: DatetimeArray|" + r"cannot perform __mul__ with this index type: DatetimeArray|" + r"cannot perform __pow__ with this index type: TimedeltaArray|" "ufunc 'multiply' cannot use operands with types dtype" r"\(' Date: Tue, 14 Jan 2020 14:41:23 +0200 Subject: [PATCH 04/11] Fix failing tests (attempt number 2) --- pandas/tests/internals/test_internals.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 3de79ca8ee361..3f23486cd113a 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -1239,11 +1239,11 @@ def test_binop_other(self, op, value, dtype): if (op, dtype) in invalid: msg = ( - r"cannot perform __pow__ with this index type: DatetimeArray|" - r"cannot perform __mod__ with this index type: DatetimeArray|" - r"cannot perform __truediv__ with this index type: DatetimeArray|" - r"cannot perform __mul__ with this index type: DatetimeArray|" - r"cannot perform __pow__ with this index type: TimedeltaArray|" + r"cannot perform __pow\_\_ with this index type: DatetimeArray|" + r"cannot perform __mod\_\_ with this index type: DatetimeArray|" + r"cannot perform __truediv\_\_ with this index type: DatetimeArray|" + r"cannot perform __mul\_\_ with this index type: DatetimeArray|" + r"cannot perform __pow\_\_ with this index type: TimedeltaArray|" "ufunc 'multiply' cannot use operands with types dtype" r"\(' Date: Wed, 15 Jan 2020 23:11:04 +0200 Subject: [PATCH 05/11] Unified one of the big strings --- pandas/tests/internals/test_internals.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 3f23486cd113a..d20e1b98fff78 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -819,10 +819,8 @@ def test_validate_bool_args(self): bm1 = create_mgr("a,b,c: i8-1; d,e,f: i8-2") msg = ( - r'For argument "inplace" expected type bool, received type int.|' - r'For argument "inplace" expected type bool, received type str.|' - r'For argument "inplace" expected type bool, received type list.|' - r'For argument "inplace" expected type bool, received type float.' + 'For argument "inplace" expected type bool, ' + "received type (int|str|list|float)." ) for value in invalid_values: From d60be3cd535fb5a4f563e54fa2eb6bd5dd0050d6 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Wed, 15 Jan 2020 23:22:54 +0200 Subject: [PATCH 06/11] Trying what simonjayhawkings suggested ref https://github.com/pandas-dev/pandas/pull/30998#issuecomment-574411899 --- pandas/tests/internals/test_internals.py | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index d20e1b98fff78..4f2a319e51763 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -1236,16 +1236,21 @@ def test_binop_other(self, op, value, dtype): } if (op, dtype) in invalid: - msg = ( - r"cannot perform __pow\_\_ with this index type: DatetimeArray|" - r"cannot perform __mod\_\_ with this index type: DatetimeArray|" - r"cannot perform __truediv\_\_ with this index type: DatetimeArray|" - r"cannot perform __mul\_\_ with this index type: DatetimeArray|" - r"cannot perform __pow\_\_ with this index type: TimedeltaArray|" - "ufunc 'multiply' cannot use operands with types dtype" - r"\(' Date: Thu, 16 Jan 2020 00:24:06 +0200 Subject: [PATCH 07/11] Fix failing tests (attempt number 3) --- pandas/tests/internals/test_internals.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 4f2a319e51763..7f5bb0b094302 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -1248,8 +1248,10 @@ def test_binop_other(self, op, value, dtype): ) else: msg = ( - f"cannot perform __{op.__name__}__ with this index type: " - "(DatetimeArray|TimedeltaArray)" + re.escape( + f"cannot perform __{op.__name__}_\_ with this index type: " + ) + + "(DatetimeArray|TimedeltaArray)" ) with pytest.raises(TypeError, match=msg): op(s, e.value) From aa46a8e580c930539a6d4a5e0231453f0933b698 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 16 Jan 2020 00:27:14 +0200 Subject: [PATCH 08/11] Fix invalid escape sequence --- pandas/tests/internals/test_internals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 7f5bb0b094302..6c62e7ae55bb9 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -1249,7 +1249,7 @@ def test_binop_other(self, op, value, dtype): else: msg = ( re.escape( - f"cannot perform __{op.__name__}_\_ with this index type: " + f"cannot perform __{op.__name__}__ with this index type: " ) + "(DatetimeArray|TimedeltaArray)" ) From ae8494d9cbdcdabb539f539d4000eb77ed9f4f1e Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Thu, 16 Jan 2020 19:38:28 +0200 Subject: [PATCH 09/11] Reverted failing test --- pandas/tests/internals/test_internals.py | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 6c62e7ae55bb9..20c7788d27985 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -1236,24 +1236,7 @@ def test_binop_other(self, op, value, dtype): } if (op, dtype) in invalid: - if op is operator.add: - msg = "cannot add DatetimeArray and Timestamp" - elif op is operator.mul: - msg = ( - re.escape( - "ufunc 'multiply' cannot use operands with " - "types dtype(' Date: Fri, 17 Jan 2020 15:32:16 +0200 Subject: [PATCH 10/11] Removed tests for external error messages --- pandas/tests/internals/test_internals.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 20c7788d27985..4f6bc7780e2a2 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -298,9 +298,7 @@ def test_delete(self): newb = self.fblock.copy() - msg = "index 3 is out of bounds for axis 0 with size 3" - - with pytest.raises(IndexError, match=msg): + with pytest.raises(IndexError, match=None): newb.delete(3) @@ -325,11 +323,7 @@ def test_can_hold_element(self): val = date(2010, 10, 10) assert not block._can_hold_element(val) - msg = ( - "'value' should be a 'Timestamp', 'NaT', " - "or array of those. Got 'date' instead." - ) - with pytest.raises(TypeError, match=msg): + with pytest.raises(TypeError, match=None): arr[0] = val @@ -818,13 +812,8 @@ def test_validate_bool_args(self): invalid_values = [1, "True", [1, 2, 3], 5.0] bm1 = create_mgr("a,b,c: i8-1; d,e,f: i8-2") - msg = ( - 'For argument "inplace" expected type bool, ' - "received type (int|str|list|float)." - ) - for value in invalid_values: - with pytest.raises(ValueError, match=msg): + with pytest.raises(ValueError, match=None): bm1.replace_list([1], [2], inplace=value) From a8be057452f13b80a8fe6aa2fbe5992a12a91e08 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Fri, 17 Jan 2020 17:59:37 +0200 Subject: [PATCH 11/11] Reverted some of the changes made in f22fba69f9cb69d1e95ef514f506c67f1b8f1066 --- pandas/tests/internals/test_internals.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 4f6bc7780e2a2..a068d8f346cb4 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -323,7 +323,11 @@ def test_can_hold_element(self): val = date(2010, 10, 10) assert not block._can_hold_element(val) - with pytest.raises(TypeError, match=None): + msg = ( + "'value' should be a 'Timestamp', 'NaT', " + "or array of those. Got 'date' instead." + ) + with pytest.raises(TypeError, match=msg): arr[0] = val @@ -813,7 +817,11 @@ def test_validate_bool_args(self): bm1 = create_mgr("a,b,c: i8-1; d,e,f: i8-2") for value in invalid_values: - with pytest.raises(ValueError, match=None): + msg = ( + 'For argument "inplace" expected type bool, ' + f"received type {type(value).__name__}." + ) + with pytest.raises(ValueError, match=msg): bm1.replace_list([1], [2], inplace=value)