diff --git a/pandas/_testing.py b/pandas/_testing.py index 631d550c60534..13af8703cef93 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,24 @@ 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], None], None]: + """ + Helper function to mark pytest.raises that have an external error message. + + Parameters + ---------- + expected_exception : Exception + Expected error to raise. + + Returns + ------- + Callable + Regular `pytest.raises` function with `match` equal to `None`. + """ + 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..8860e6fe272ce 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -76,3 +76,8 @@ def test_rng_context(): with tm.RNGContext(1): assert np.random.randn() == expected1 assert np.random.randn() == expected0 + + +def test_external_error_raised(): + with tm.external_error_raised(TypeError): + raise TypeError("Should not check this error message, so it will pass")