From 52911529acabf3fa7d8da600a3ddce995c67675c Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 19 Jan 2020 03:51:28 +0200 Subject: [PATCH 1/6] Implemented "external_error_raised" @gfyoung wrote this function here https://github.com/pandas-dev/pandas/issues/30999#issuecomment-575426086 so we can ignore error messages without breaking fututre CI tests of bare pytest raises calls --- pandas/_testing.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 631d550c60534..009f2c2416d30 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -8,7 +8,7 @@ from shutil import rmtree import string import tempfile -from typing import Any, List, Optional, Union, cast +from typing import Any, Callable, List, Optional, Type, Union, cast import warnings import zipfile @@ -2757,3 +2757,31 @@ def convert_rows_list_to_csv_str(rows_list: List[str]): sep = os.linesep expected = sep.join(rows_list) + sep return expected + + +def external_error_raised( + expected_exception: Type[Exception], +) -> Callable[[Type[Exception], Optional[None]], None]: + """ + Helper function to mark pytest.raises that have an external error message. + + Parameters + ---------- + expected_exception : Exception + Exected error to raise. + + Returns + ------- + Callable + Regular `pytest.raises` function with `match` equal to `None`. + + Raises + ------ + AssertionError + When `expected_exception` is not a subclass of `Exception`. + """ + assert issubclass( + expected_exception, Exception + ), "The recived value is not an `Exception`." + + return pytest.raises(expected_exception, match=None) From 18f1d7bde8b65c6321a99bcdb64cdee78cc5fabc Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 19 Jan 2020 04:44:15 +0200 Subject: [PATCH 2/6] Added tests --- pandas/_testing.py | 9 +-------- pandas/tests/util/test_util.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 009f2c2416d30..2f0bc3a570484 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -2774,14 +2774,7 @@ def external_error_raised( ------- Callable Regular `pytest.raises` function with `match` equal to `None`. - - Raises - ------ - AssertionError - When `expected_exception` is not a subclass of `Exception`. """ - assert issubclass( - expected_exception, Exception - ), "The recived value is not an `Exception`." + import pytest return pytest.raises(expected_exception, match=None) diff --git a/pandas/tests/util/test_util.py b/pandas/tests/util/test_util.py index 6a19adef728e4..2ed64c415694e 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -76,3 +76,16 @@ def test_rng_context(): with tm.RNGContext(1): assert np.random.randn() == expected1 assert np.random.randn() == expected0 + + +def test_external_error_raised(): + # Raising an external error message + with tm.external_error_raised(TypeError): + pytest.approx() + + # Note: Should be the only place in the codebase with + # "pytest.raises(Exception, match=None)" + # (Except pandas._testing.test_external_error_raised) + with pytest.raises(TypeError, match=None): + with tm.external_error_raised(ValueError): + pytest.approx() From 9d53933f0838e9659295e129310662f2bbe8d4a2 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 19 Jan 2020 04:52:08 +0200 Subject: [PATCH 3/6] Fixed small mistakes --- pandas/_testing.py | 2 +- pandas/tests/util/test_util.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 2f0bc3a570484..83d431a5c7496 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -2761,7 +2761,7 @@ def convert_rows_list_to_csv_str(rows_list: List[str]): def external_error_raised( expected_exception: Type[Exception], -) -> Callable[[Type[Exception], Optional[None]], None]: +) -> Callable[[Type[Exception], None], None]: """ Helper function to mark pytest.raises that have an external error message. diff --git a/pandas/tests/util/test_util.py b/pandas/tests/util/test_util.py index 2ed64c415694e..7298f0edb6074 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -85,7 +85,7 @@ def test_external_error_raised(): # Note: Should be the only place in the codebase with # "pytest.raises(Exception, match=None)" - # (Except pandas._testing.test_external_error_raised) + # (Except pandas._testing.external_error_raised) with pytest.raises(TypeError, match=None): with tm.external_error_raised(ValueError): pytest.approx() From 739f76acf4202b6541ecbd135f539a57046874e1 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <50263213+MomIsBestFriend@users.noreply.github.com> Date: Sun, 19 Jan 2020 05:28:06 +0200 Subject: [PATCH 4/6] Update pandas/_testing.py Co-Authored-By: gfyoung --- pandas/_testing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index 83d431a5c7496..13af8703cef93 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -2768,7 +2768,7 @@ def external_error_raised( Parameters ---------- expected_exception : Exception - Exected error to raise. + Expected error to raise. Returns ------- From e1fa2dcb35fb322a1ff971fe380389d76f15cb4a Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 19 Jan 2020 05:40:35 +0200 Subject: [PATCH 5/6] Fixes for review number-1 by @gfyoung Ref: https://github.com/pandas-dev/pandas/pull/31130#discussion_r368262378 --- pandas/tests/util/test_util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/util/test_util.py b/pandas/tests/util/test_util.py index 7298f0edb6074..3b73afcd571bf 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -81,11 +81,11 @@ def test_rng_context(): def test_external_error_raised(): # Raising an external error message with tm.external_error_raised(TypeError): - pytest.approx() + raise TypeError("Should not check this error message, so it will pass") # Note: Should be the only place in the codebase with # "pytest.raises(Exception, match=None)" # (Except pandas._testing.external_error_raised) with pytest.raises(TypeError, match=None): with tm.external_error_raised(ValueError): - pytest.approx() + raise TypeError("Checking that we catch only the specified Exception") From 9e0972de7979d92cd1159fda6c7785a1e8bb8b14 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Sun, 19 Jan 2020 08:40:32 +0200 Subject: [PATCH 6/6] Droped confusing test Ref: https://github.com/pandas-dev/pandas/pull/31130#discussion_r368266676 --- pandas/tests/util/test_util.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pandas/tests/util/test_util.py b/pandas/tests/util/test_util.py index 3b73afcd571bf..8860e6fe272ce 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -79,13 +79,5 @@ def test_rng_context(): def test_external_error_raised(): - # Raising an external error message with tm.external_error_raised(TypeError): raise TypeError("Should not check this error message, so it will pass") - - # Note: Should be the only place in the codebase with - # "pytest.raises(Exception, match=None)" - # (Except pandas._testing.external_error_raised) - with pytest.raises(TypeError, match=None): - with tm.external_error_raised(ValueError): - raise TypeError("Checking that we catch only the specified Exception")