Skip to content

TST: Allow tm.network to catch urllib.error.URLError #45723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions pandas/_testing/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
Series,
)

_RAISE_NETWORK_ERROR_DEFAULT = False

# skip tests on exceptions with these messages
_network_error_messages = (
# 'urlopen error timed out',
Expand Down Expand Up @@ -70,10 +68,12 @@


def _get_default_network_errors():
# Lazy import for http.client because it imports many things from the stdlib
# Lazy import for http.client & urllib.error
# because it imports many things from the stdlib
import http.client
import urllib.error

return (OSError, http.client.HTTPException, TimeoutError)
return (OSError, http.client.HTTPException, TimeoutError, urllib.error.URLError)


def optional_args(decorator):
Expand Down Expand Up @@ -108,7 +108,7 @@ def dec(f):
def network(
t,
url="https://www.google.com",
raise_on_error=_RAISE_NETWORK_ERROR_DEFAULT,
raise_on_error=False,
check_before_test=False,
error_classes=None,
skip_errnos=_network_errno_vals,
Expand Down Expand Up @@ -163,8 +163,8 @@ def network(
Tests decorated with @network will fail if it's possible to make a network
connection to another URL (defaults to google.com)::

>>> from pandas import _testing as ts
>>> @ts.network
>>> from pandas import _testing as tm
>>> @tm.network
... def test_network():
... with pd.io.common.urlopen("rabbit://bonanza.com"):
... pass
Expand All @@ -175,18 +175,18 @@ def network(

You can specify alternative URLs::

>>> @ts.network("https://www.yahoo.com")
>>> @tm.network("https://www.yahoo.com")
... def test_something_with_yahoo():
... raise OSError("Failure Message")
>>> test_something_with_yahoo()
>>> test_something_with_yahoo() # doctest: +SKIP
Traceback (most recent call last):
...
OSError: Failure Message

If you set check_before_test, it will check the url first and not run the
test on failure::

>>> @ts.network("failing://url.blaher", check_before_test=True)
>>> @tm.network("failing://url.blaher", check_before_test=True)
... def test_something():
... print("I ran!")
... raise ValueError("Failure")
Expand All @@ -196,7 +196,7 @@ def network(

Errors not related to networking will always be raised.
"""
from pytest import skip
import pytest

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

if errno in skip_errnos:
skip(f"Skipping test due to known errno and error {err}")
pytest.skip(f"Skipping test due to known errno and error {err}")

e_str = str(err)

if any(m.lower() in e_str.lower() for m in _skip_on_messages):
skip(
pytest.skip(
f"Skipping test because exception message is known and error {err}"
)

if not isinstance(err, error_classes):
raise

if raise_on_error or can_connect(url, error_classes):
if not isinstance(err, error_classes) or raise_on_error:
raise
else:
skip(f"Skipping test due to lack of connectivity and error {err}")
pytest.skip(
f"Skipping test due to lack of connectivity and error {err}"
)

return wrapper

Expand Down