Skip to content

Commit dad937e

Browse files
committed
Merge branch 'master' of https://github.com/pandas-dev/pandas into positional_slice
2 parents a076e6a + 6d3cc14 commit dad937e

File tree

3 files changed

+29
-37
lines changed

3 files changed

+29
-37
lines changed

pandas/_testing.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from shutil import rmtree
99
import string
1010
import tempfile
11-
from typing import Any, List, Optional, Union, cast
11+
from typing import Any, Callable, List, Optional, Type, Union, cast
1212
import warnings
1313
import zipfile
1414

@@ -2757,3 +2757,24 @@ def convert_rows_list_to_csv_str(rows_list: List[str]):
27572757
sep = os.linesep
27582758
expected = sep.join(rows_list) + sep
27592759
return expected
2760+
2761+
2762+
def external_error_raised(
2763+
expected_exception: Type[Exception],
2764+
) -> Callable[[Type[Exception], None], None]:
2765+
"""
2766+
Helper function to mark pytest.raises that have an external error message.
2767+
2768+
Parameters
2769+
----------
2770+
expected_exception : Exception
2771+
Expected error to raise.
2772+
2773+
Returns
2774+
-------
2775+
Callable
2776+
Regular `pytest.raises` function with `match` equal to `None`.
2777+
"""
2778+
import pytest
2779+
2780+
return pytest.raises(expected_exception, match=None)

pandas/tests/io/test_common.py

+2-36
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,7 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext):
141141
pytest.importorskip(module)
142142

143143
path = os.path.join(HERE, "data", "does_not_exist." + fn_ext)
144-
msg1 = r"File (b')?.+does_not_exist\.{}'? does not exist".format(fn_ext)
145-
msg2 = fr"\[Errno 2\] No such file or directory: '.+does_not_exist\.{fn_ext}'"
146-
msg3 = "Expected object or value"
147-
msg4 = "path_or_buf needs to be a string file path or file-like"
148-
msg5 = (
149-
fr"\[Errno 2\] File .+does_not_exist\.{fn_ext} does not exist: "
150-
fr"'.+does_not_exist\.{fn_ext}'"
151-
)
152-
msg6 = fr"\[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.{fn_ext}'"
153-
msg7 = (
154-
fr"\[Errno 2\] File o directory non esistente: '.+does_not_exist\.{fn_ext}'"
155-
)
156-
msg8 = fr"Failed to open local file.+does_not_exist\.{fn_ext}.?, error: .*"
157-
158-
with pytest.raises(
159-
error_class,
160-
match=fr"({msg1}|{msg2}|{msg3}|{msg4}|{msg5}|{msg6}|{msg7}|{msg8})",
161-
):
144+
with tm.external_error_raised(error_class):
162145
reader(path)
163146

164147
@pytest.mark.parametrize(
@@ -184,24 +167,7 @@ def test_read_expands_user_home_dir(
184167
path = os.path.join("~", "does_not_exist." + fn_ext)
185168
monkeypatch.setattr(icom, "_expand_user", lambda x: os.path.join("foo", x))
186169

187-
msg1 = fr"File (b')?.+does_not_exist\.{fn_ext}'? does not exist"
188-
msg2 = fr"\[Errno 2\] No such file or directory: '.+does_not_exist\.{fn_ext}'"
189-
msg3 = "Unexpected character found when decoding 'false'"
190-
msg4 = "path_or_buf needs to be a string file path or file-like"
191-
msg5 = (
192-
fr"\[Errno 2\] File .+does_not_exist\.{fn_ext} does not exist: "
193-
fr"'.+does_not_exist\.{fn_ext}'"
194-
)
195-
msg6 = fr"\[Errno 2\] 没有那个文件或目录: '.+does_not_exist\.{fn_ext}'"
196-
msg7 = (
197-
fr"\[Errno 2\] File o directory non esistente: '.+does_not_exist\.{fn_ext}'"
198-
)
199-
msg8 = fr"Failed to open local file.+does_not_exist\.{fn_ext}.?, error: .*"
200-
201-
with pytest.raises(
202-
error_class,
203-
match=fr"({msg1}|{msg2}|{msg3}|{msg4}|{msg5}|{msg6}|{msg7}|{msg8})",
204-
):
170+
with tm.external_error_raised(error_class):
205171
reader(path)
206172

207173
@pytest.mark.parametrize(

pandas/tests/util/test_util.py

+5
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ def test_rng_context():
7676
with tm.RNGContext(1):
7777
assert np.random.randn() == expected1
7878
assert np.random.randn() == expected0
79+
80+
81+
def test_external_error_raised():
82+
with tm.external_error_raised(TypeError):
83+
raise TypeError("Should not check this error message, so it will pass")

0 commit comments

Comments
 (0)