Skip to content

Commit 4451392

Browse files
authored
TST: Allow tm.network to catch urllib.error.URLError (#45723)
1 parent e92839e commit 4451392

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

pandas/_testing/_io.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
Series,
3030
)
3131

32-
_RAISE_NETWORK_ERROR_DEFAULT = False
33-
3432
# skip tests on exceptions with these messages
3533
_network_error_messages = (
3634
# 'urlopen error timed out',
@@ -70,10 +68,12 @@
7068

7169

7270
def _get_default_network_errors():
73-
# Lazy import for http.client because it imports many things from the stdlib
71+
# Lazy import for http.client & urllib.error
72+
# because it imports many things from the stdlib
7473
import http.client
74+
import urllib.error
7575

76-
return (OSError, http.client.HTTPException, TimeoutError)
76+
return (OSError, http.client.HTTPException, TimeoutError, urllib.error.URLError)
7777

7878

7979
def optional_args(decorator):
@@ -108,7 +108,7 @@ def dec(f):
108108
def network(
109109
t,
110110
url="https://www.google.com",
111-
raise_on_error=_RAISE_NETWORK_ERROR_DEFAULT,
111+
raise_on_error=False,
112112
check_before_test=False,
113113
error_classes=None,
114114
skip_errnos=_network_errno_vals,
@@ -163,8 +163,8 @@ def network(
163163
Tests decorated with @network will fail if it's possible to make a network
164164
connection to another URL (defaults to google.com)::
165165
166-
>>> from pandas import _testing as ts
167-
>>> @ts.network
166+
>>> from pandas import _testing as tm
167+
>>> @tm.network
168168
... def test_network():
169169
... with pd.io.common.urlopen("rabbit://bonanza.com"):
170170
... pass
@@ -175,18 +175,18 @@ def network(
175175
176176
You can specify alternative URLs::
177177
178-
>>> @ts.network("https://www.yahoo.com")
178+
>>> @tm.network("https://www.yahoo.com")
179179
... def test_something_with_yahoo():
180180
... raise OSError("Failure Message")
181-
>>> test_something_with_yahoo()
181+
>>> test_something_with_yahoo() # doctest: +SKIP
182182
Traceback (most recent call last):
183183
...
184184
OSError: Failure Message
185185
186186
If you set check_before_test, it will check the url first and not run the
187187
test on failure::
188188
189-
>>> @ts.network("failing://url.blaher", check_before_test=True)
189+
>>> @tm.network("failing://url.blaher", check_before_test=True)
190190
... def test_something():
191191
... print("I ran!")
192192
... raise ValueError("Failure")
@@ -196,7 +196,7 @@ def network(
196196
197197
Errors not related to networking will always be raised.
198198
"""
199-
from pytest import skip
199+
import pytest
200200

201201
if error_classes is None:
202202
error_classes = _get_default_network_errors()
@@ -210,7 +210,9 @@ def wrapper(*args, **kwargs):
210210
and not raise_on_error
211211
and not can_connect(url, error_classes)
212212
):
213-
skip()
213+
pytest.skip(
214+
f"May not have network connectivity because cannot connect to {url}"
215+
)
214216
try:
215217
return t(*args, **kwargs)
216218
except Exception as err:
@@ -220,22 +222,21 @@ def wrapper(*args, **kwargs):
220222
errno = getattr(err.reason, "errno", None) # type: ignore[attr-defined]
221223

222224
if errno in skip_errnos:
223-
skip(f"Skipping test due to known errno and error {err}")
225+
pytest.skip(f"Skipping test due to known errno and error {err}")
224226

225227
e_str = str(err)
226228

227229
if any(m.lower() in e_str.lower() for m in _skip_on_messages):
228-
skip(
230+
pytest.skip(
229231
f"Skipping test because exception message is known and error {err}"
230232
)
231233

232-
if not isinstance(err, error_classes):
233-
raise
234-
235-
if raise_on_error or can_connect(url, error_classes):
234+
if not isinstance(err, error_classes) or raise_on_error:
236235
raise
237236
else:
238-
skip(f"Skipping test due to lack of connectivity and error {err}")
237+
pytest.skip(
238+
f"Skipping test due to lack of connectivity and error {err}"
239+
)
239240

240241
return wrapper
241242

0 commit comments

Comments
 (0)