Add type hints to test.test_exceptions
MODIFY
noxfile.py
noxfile.py
@@ -169,6 +169,7 @@ def mypy(session: nox.Session) -> None:
"test/__init__.py",
"test/conftest.py",
"test/port_helpers.py",
+ "test/test_exceptions.py",
"test/test_fields.py",
"test/test_retry.py",
"test/test_wait.py",

MODIFY
src/urllib3/exceptions.py
src/urllib3/exceptions.py
import socket
import warnings
+from email.errors import MessageDefect
from http.client import IncompleteRead as httplib_IncompleteRead
from typing import TYPE_CHECKING, Callable, List, Optional, Tuple, Union
if TYPE_CHECKING:
- from email.errors import MessageDefect
-
from urllib3.connection import HTTPConnection
from urllib3.connectionpool import ConnectionPool
from urllib3.response import HTTPResponse
@@ -356,7 +355,7 @@ class HeaderParsingError(HTTPError):
"""Raised by assert_header_parsing, but we convert it to a log.warning statement."""
def __init__(
- self, defects: List["MessageDefect"], unparsed_data: Optional[Union[bytes, str]]
+ self, defects: List[MessageDefect], unparsed_data: Optional[Union[bytes, str]]
) -> None:
message = f"{defects or 'Unknown'}, unparsed data: {unparsed_data!r}"
super().__init__(message)

MODIFY
test/__init__.py
test/__init__.py
@@ -31,6 +31,7 @@ except ImportError:
import functools
from urllib3 import util
+from urllib3.connectionpool import ConnectionPool
from urllib3.exceptions import HTTPWarning
from urllib3.util import ssl_
@@ -77,6 +78,8 @@ LONG_TIMEOUT = 0.01
if os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS") == "true":
LONG_TIMEOUT = 0.5
+DUMMY_POOL = ConnectionPool("dummy")
+
def _can_resolve(host: str) -> bool:
""" Returns True if the system can resolve host to an address. """

MODIFY
test/test_exceptions.py
test/test_exceptions.py
import pickle
+from email.errors import MessageDefect
+from test import DUMMY_POOL
import pytest
@@ -23,34 +25,34 @@ class TestPickle:
"exception",
[
HTTPError(None),
- MaxRetryError(None, None, None),
- LocationParseError(None),
+ MaxRetryError(DUMMY_POOL, "", None),
+ LocationParseError(""),
ConnectTimeoutError(None),
HTTPError("foo"),
HTTPError("foo", IOError("foo")),
MaxRetryError(HTTPConnectionPool("localhost"), "/", None),
LocationParseError("fake location"),
- ClosedPoolError(HTTPConnectionPool("localhost"), None),
- EmptyPoolError(HTTPConnectionPool("localhost"), None),
- HostChangedError(HTTPConnectionPool("localhost"), "/", None),
- ReadTimeoutError(HTTPConnectionPool("localhost"), "/", None),
+ ClosedPoolError(HTTPConnectionPool("localhost"), ""),
+ EmptyPoolError(HTTPConnectionPool("localhost"), ""),
+ HostChangedError(HTTPConnectionPool("localhost"), "/", 0),
+ ReadTimeoutError(HTTPConnectionPool("localhost"), "/", ""),
],
)
- def test_exceptions(self, exception):
+ def test_exceptions(self, exception: Exception) -> None:
result = pickle.loads(pickle.dumps(exception))
assert isinstance(result, type(exception))
class TestFormat:
- def test_header_parsing_errors(self):
- hpe = HeaderParsingError("defects", "unparsed_data")
+ def test_header_parsing_errors(self) -> None:
+ hpe = HeaderParsingError([MessageDefect("defects")], "unparsed_data")
assert "defects" in str(hpe)
assert "unparsed_data" in str(hpe)
class TestNewConnectionError:
- def test_pool_property_deprecation_warning(self):
+ def test_pool_property_deprecation_warning(self) -> None:
err = NewConnectionError(HTTPConnection("localhost"), "test")
with pytest.warns(DeprecationWarning) as records:
err.pool
@@ -60,4 +62,6 @@ class TestNewConnectionError:
"The 'pool' property is deprecated and will be removed "
"in a later urllib3 v2.x release. use 'conn' instead."
)
- assert any(record.message.args[0] == msg for record in records)
+ record = records[0]
+ assert isinstance(record.message, Warning)
+ assert record.message.args[0] == msg

MODIFY
test/test_retry.py
test/test_retry.py
+from test import DUMMY_POOL
from typing import Optional
from unittest import mock
import pytest
-from urllib3.connectionpool import ConnectionPool
from urllib3.exceptions import (
ConnectTimeoutError,
InvalidHeader,
@@ -15,8 +15,6 @@ from urllib3.exceptions import (
from urllib3.response import HTTPResponse
from urllib3.util.retry import RequestHistory, Retry
-DUMMY_POOL = ConnectionPool("dummy")
-
class TestRetry:
def test_string(self) -> None:

Add type hints to test.test_exceptions

Co-authored-by: Seth Michael Larson <sethmichaellarson@gmail.com>