Upgrade mypy to 0.910 and fix IncompleteRead exception types
MODIFY
noxfile.py
noxfile.py
@@ -105,11 +105,12 @@ def lint(session):
mypy(session)
-@nox.session()
+@nox.session(python="3.8")
def mypy(session):
"""Run mypy."""
- session.install("mypy==0.812")
+ session.install("mypy==0.910")
session.install("idna>=2.0.0")
+ session.install("cryptography>=1.3.4")
session.run("mypy", "--version")
session.run("mypy", "--strict", "src/urllib3")

MODIFY
src/urllib3/contrib/pyopenssl.py
src/urllib3/contrib/pyopenssl.py
@@ -52,7 +52,7 @@ from cryptography.hazmat.backends.openssl import backend as openssl_backend
from cryptography.hazmat.backends.openssl.x509 import _Certificate
try:
- from cryptography.x509 import UnsupportedExtension
+ from cryptography.x509 import UnsupportedExtension # type: ignore
except ImportError:
# UnsupportedExtension is gone in cryptography >= 2.1.0
class UnsupportedExtension(Exception): # type: ignore
@@ -79,7 +79,7 @@ HAS_SNI = True
# Use system TLS ciphers on OpenSSL 1.1.1+
USE_DEFAULT_SSLCONTEXT_CIPHERS = util.ssl_._is_ge_openssl_v1_1_1(
- openssl_backend.openssl_version_text(), openssl_backend.openssl_version_number()
+ openssl_backend.openssl_version_text(), openssl_backend.openssl_version_number() # type: ignore
)
# Map from urllib3 to PyOpenSSL compatible parameter-values.
@@ -150,7 +150,7 @@ def _validate_dependencies_met() -> None:
Throws `ImportError` if they are not met.
"""
# Method added in `cryptography==1.1`; not available in older versions
- from cryptography.x509.extensions import Extensions # type: ignore
+ from cryptography.x509.extensions import Extensions
if getattr(Extensions, "get_extension_for_class", None) is None:
raise ImportError(
@@ -221,7 +221,7 @@ def get_subj_alt_name(peer_cert: "CRL") -> List[Tuple[str, str]]:
else:
# This is technically using private APIs, but should work across all
# relevant versions before PyOpenSSL got a proper API for this.
- cert = _Certificate(openssl_backend, peer_cert._x509)
+ cert = _Certificate(openssl_backend, peer_cert._x509) # type: ignore
# We want to find the SAN extension. Ask Cryptography to locate it (it's
# faster than looping in Python)

MODIFY
src/urllib3/exceptions.py
src/urllib3/exceptions.py
@@ -305,7 +305,8 @@ class IncompleteRead(HTTPError, httplib_IncompleteRead):
expected: int
def __init__(self, partial: int, expected: int) -> None:
- super().__init__(partial, expected)
+ self.partial = partial
+ self.expected = expected
def __repr__(self) -> str:
return "IncompleteRead(%i bytes read, %i more expected)" % (
@@ -318,12 +319,13 @@ class InvalidChunkLength(HTTPError, httplib_IncompleteRead):
"""Invalid chunk length in a chunked response."""
partial: int
- expected: int
+ expected: Optional[int]
response: "HTTPResponse"
length: int
def __init__(self, response: "HTTPResponse", length: int) -> None:
- super().__init__(response.tell(), response.length_remaining)
+ self.partial = response.tell()
+ self.expected = response.length_remaining
self.response = response
self.length = length

Upgrade mypy to 0.910 and fix IncompleteRead exception types