From 65e49eae030da2768fd03cffec672d65069b9415 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 14 Jan 2020 13:05:55 +0200 Subject: [PATCH 1/8] TST: Insert 'match' to bare pytest raises --- pandas/tests/test_common.py | 13 ++++++++----- pandas/tests/test_downstream.py | 7 ++++++- pandas/tests/test_errors.py | 8 ++++---- pandas/tests/test_lib.py | 12 +++++++----- pandas/tests/test_take.py | 17 +++++++++++++---- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index a8a0fcea7182c..186c735a0bff9 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -60,10 +60,11 @@ def test_random_state(): assert com.random_state() is np.random # Error for floats or strings - with pytest.raises(ValueError): + msg = "random_state must be an integer, a numpy RandomState, or None" + with pytest.raises(ValueError, match=msg): com.random_state("test") - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): com.random_state(5.5) @@ -93,15 +94,17 @@ def test_dict_compat(): def test_standardize_mapping(): # No uninitialized defaultdicts - with pytest.raises(TypeError): + msg = r"to_dict\(\) only accepts initialized defaultdicts" + with pytest.raises(TypeError, match=msg): com.standardize_mapping(collections.defaultdict) # No non-mapping subtypes, instance - with pytest.raises(TypeError): + msg = "unsupported type: " + with pytest.raises(TypeError, match=msg): com.standardize_mapping([]) # No non-mapping subtypes, class - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): com.standardize_mapping(list) fill = {"bad": "data"} diff --git a/pandas/tests/test_downstream.py b/pandas/tests/test_downstream.py index 8edd9f20ec63c..ae9480efc047c 100644 --- a/pandas/tests/test_downstream.py +++ b/pandas/tests/test_downstream.py @@ -136,7 +136,12 @@ def test_missing_required_dependency(): # https://github.com/MacPython/pandas-wheels/pull/50 call = ["python", "-sSE", "-c", "import pandas"] - with pytest.raises(subprocess.CalledProcessError) as exc: + msg = ( + r"Command '\['python', '-sSE', '-c', 'import pandas'\]' " + r"returned non-zero exit status 1." + ) + + with pytest.raises(subprocess.CalledProcessError, match=msg) as exc: subprocess.check_output(call, stderr=subprocess.STDOUT) output = exc.value.stdout.decode() diff --git a/pandas/tests/test_errors.py b/pandas/tests/test_errors.py index fa2142444ed92..8da6afc2551b7 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -22,12 +22,12 @@ def test_exception_importable(exc): from pandas import errors - e = getattr(errors, exc) - assert e is not None + err = getattr(errors, exc) + assert err is not None # check that we can raise on them - with pytest.raises(e): - raise e() + with pytest.raises(err, match=""): + raise err() def test_catch_oob(): diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index f839aa198d03f..7ceebf9a4dfe8 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -22,7 +22,8 @@ def test_max_len_string_array(self): assert libwriters.max_len_string_array(arr) == 3 # raises - with pytest.raises(TypeError): + msg = "No matching signature found" + with pytest.raises(TypeError, match=msg): libwriters.max_len_string_array(arr.astype("U")) def test_fast_unique_multiple_list_gen_sort(self): @@ -100,9 +101,10 @@ def test_maybe_indices_to_slice_right_edge(self): assert not isinstance(maybe_slice, slice) tm.assert_numpy_array_equal(maybe_slice, indices) - with pytest.raises(IndexError): + msg = "index 100 is out of bounds for axis 0 with size 100" + with pytest.raises(IndexError, match=msg): target[indices] - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): target[maybe_slice] indices = np.array([100, 99, 98, 97], dtype=np.int64) @@ -111,9 +113,9 @@ def test_maybe_indices_to_slice_right_edge(self): assert not isinstance(maybe_slice, slice) tm.assert_numpy_array_equal(maybe_slice, indices) - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): target[indices] - with pytest.raises(IndexError): + with pytest.raises(IndexError, match=msg): target[maybe_slice] for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]: diff --git a/pandas/tests/test_take.py b/pandas/tests/test_take.py index 465296a6f9e51..38eae3b6e2f87 100644 --- a/pandas/tests/test_take.py +++ b/pandas/tests/test_take.py @@ -423,16 +423,21 @@ class TestExtensionTake: def test_bounds_check_large(self): arr = np.array([1, 2]) - with pytest.raises(IndexError): + + msg = "indices are out-of-bounds" + with pytest.raises(IndexError, match=msg): algos.take(arr, [2, 3], allow_fill=True) - with pytest.raises(IndexError): + msg = "index 2 is out of bounds for size 2" + with pytest.raises(IndexError, match=msg): algos.take(arr, [2, 3], allow_fill=False) def test_bounds_check_small(self): arr = np.array([1, 2, 3], dtype=np.int64) indexer = [0, -1, -2] - with pytest.raises(ValueError): + + msg = r"'indices' contains values less than allowed \(-2 < -1\)" + with pytest.raises(ValueError, match=msg): algos.take(arr, indexer, allow_fill=True) result = algos.take(arr, indexer) @@ -446,7 +451,11 @@ def test_take_empty(self, allow_fill): result = algos.take(arr, [], allow_fill=allow_fill) tm.assert_numpy_array_equal(arr, result) - with pytest.raises(IndexError): + msg = ( + r"cannot do a non-empty take from an empty axes.|" + r"indices are out-of-bounds" + ) + with pytest.raises(IndexError, match=msg): algos.take(arr, [0], allow_fill=allow_fill) def test_take_na_empty(self): From e81a0cb6f4593b48908c4dd8b69242a9787f69c7 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 14 Jan 2020 13:17:25 +0200 Subject: [PATCH 2/8] Replaced empty string with '^$' --- pandas/tests/test_errors.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/tests/test_errors.py b/pandas/tests/test_errors.py index 8da6afc2551b7..939ea8a64d94d 100644 --- a/pandas/tests/test_errors.py +++ b/pandas/tests/test_errors.py @@ -26,7 +26,10 @@ def test_exception_importable(exc): assert err is not None # check that we can raise on them - with pytest.raises(err, match=""): + + msg = "^$" + + with pytest.raises(err, match=msg): raise err() From 8155b3b88fe323b3b956fc6958c86f9de70e56b2 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <50263213+MomIsBestFriend@users.noreply.github.com> Date: Tue, 14 Jan 2020 13:18:14 +0200 Subject: [PATCH 3/8] Update pandas/tests/test_take.py Co-Authored-By: Simon Hawkins --- pandas/tests/test_take.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_take.py b/pandas/tests/test_take.py index 38eae3b6e2f87..1cd5f11057464 100644 --- a/pandas/tests/test_take.py +++ b/pandas/tests/test_take.py @@ -453,7 +453,7 @@ def test_take_empty(self, allow_fill): msg = ( r"cannot do a non-empty take from an empty axes.|" - r"indices are out-of-bounds" + "indices are out-of-bounds" ) with pytest.raises(IndexError, match=msg): algos.take(arr, [0], allow_fill=allow_fill) From 0fc7273ed95f9a114457c6c9dc93d8726353fd2b Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 14 Jan 2020 13:47:06 +0200 Subject: [PATCH 4/8] Fixed failing test --- pandas/tests/test_lib.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index 7ceebf9a4dfe8..77866eec27c3c 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -101,7 +101,11 @@ def test_maybe_indices_to_slice_right_edge(self): assert not isinstance(maybe_slice, slice) tm.assert_numpy_array_equal(maybe_slice, indices) - msg = "index 100 is out of bounds for axis 0 with size 100" + msg = ( + "index 100 is out of bounds for axis 0 with size 100|" + "index 100 is out of bounds for axis 1 with size 100" + ) + with pytest.raises(IndexError, match=msg): target[indices] with pytest.raises(IndexError, match=msg): From c94bdccbd7f7ab6df4d4c9732b5530cf18af4089 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <50263213+MomIsBestFriend@users.noreply.github.com> Date: Tue, 14 Jan 2020 20:28:11 +0200 Subject: [PATCH 5/8] Update pandas/tests/test_lib.py Co-Authored-By: William Ayd --- pandas/tests/test_lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index 77866eec27c3c..17d5bdeb7c7cd 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -102,7 +102,7 @@ def test_maybe_indices_to_slice_right_edge(self): tm.assert_numpy_array_equal(maybe_slice, indices) msg = ( - "index 100 is out of bounds for axis 0 with size 100|" + "index 100 is out of bounds for axis (0|1) with size 100|" "index 100 is out of bounds for axis 1 with size 100" ) From 15fce189d2123068df968922de6fa63fa5f0fad8 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <50263213+MomIsBestFriend@users.noreply.github.com> Date: Tue, 14 Jan 2020 20:28:30 +0200 Subject: [PATCH 6/8] Update pandas/tests/test_downstream.py Co-Authored-By: William Ayd --- pandas/tests/test_downstream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_downstream.py b/pandas/tests/test_downstream.py index ae9480efc047c..02898988ca8aa 100644 --- a/pandas/tests/test_downstream.py +++ b/pandas/tests/test_downstream.py @@ -138,7 +138,7 @@ def test_missing_required_dependency(): msg = ( r"Command '\['python', '-sSE', '-c', 'import pandas'\]' " - r"returned non-zero exit status 1." + "returned non-zero exit status 1." ) with pytest.raises(subprocess.CalledProcessError, match=msg) as exc: From 492e400ff2d557ae13e1bfac8b74675bef2daec9 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 14 Jan 2020 20:33:17 +0200 Subject: [PATCH 7/8] Fix regex --- pandas/tests/test_lib.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index 17d5bdeb7c7cd..bcf77b16e7b46 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -101,10 +101,7 @@ def test_maybe_indices_to_slice_right_edge(self): assert not isinstance(maybe_slice, slice) tm.assert_numpy_array_equal(maybe_slice, indices) - msg = ( - "index 100 is out of bounds for axis (0|1) with size 100|" - "index 100 is out of bounds for axis 1 with size 100" - ) + msg = r"index 100 is out of bounds for axis \(0|1\) with size 100" with pytest.raises(IndexError, match=msg): target[indices] From 8735b2b1b4af1f52aaad3f26fb00699892eac942 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 14 Jan 2020 20:56:30 +0200 Subject: [PATCH 8/8] Reverted 'Fixed regex' --- pandas/tests/test_lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_lib.py b/pandas/tests/test_lib.py index bcf77b16e7b46..d914cf873de24 100644 --- a/pandas/tests/test_lib.py +++ b/pandas/tests/test_lib.py @@ -101,7 +101,7 @@ def test_maybe_indices_to_slice_right_edge(self): assert not isinstance(maybe_slice, slice) tm.assert_numpy_array_equal(maybe_slice, indices) - msg = r"index 100 is out of bounds for axis \(0|1\) with size 100" + msg = "index 100 is out of bounds for axis (0|1) with size 100" with pytest.raises(IndexError, match=msg): target[indices]