Skip to content

Commit f3a4f9a

Browse files
committed
🐛 relax hard assert for response type in the synchronous code
partially resolve #320
1 parent e05c705 commit f3a4f9a

File tree

22 files changed

+60
-47
lines changed

22 files changed

+60
-47
lines changed

mypy-requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mypy==1.13.0; python_version >= '3.8'
1+
mypy==1.19.1; python_version >= '3.8'
22
mypy==1.4.1; python_version < '3.8'
33
idna>=2.0.0
44
cryptography<47
@@ -15,3 +15,4 @@ wsproto>=1.2,<2
1515
aiofile>=3.9.0,<4
1616
zstandard>=0.18.0
1717
wsproto
18+
types-PySocks

src/urllib3/_async/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ async def connect(self) -> None:
813813
self.assert_hostname,
814814
self.cert_reqs,
815815
)
816-
is NotImplemented
816+
is False
817817
):
818818
server_hostname: str = self.host
819819
tls_in_tls = False

src/urllib3/_collections.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ def extend(self, *args: ValidHTTPHeaderSource, **kwargs: str) -> None:
357357
for key, val in other.items():
358358
self.add(key, val)
359359
elif isinstance(other, typing.Iterable):
360-
other = typing.cast(typing.Iterable[typing.Tuple[str, str]], other)
361360
for key, value in other:
362361
self.add(key, value)
363362
elif hasattr(other, "keys") and hasattr(other, "__getitem__"):

src/urllib3/backend/_async/hface.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515

1616
try: # We shouldn't do this, it is private. Only for chain extraction check. We should find another way.
17-
from _ssl import Certificate # type: ignore[import-not-found]
17+
from _ssl import Certificate
1818
except (ImportError, AttributeError):
19-
Certificate = None
19+
Certificate = None # type: ignore[misc,assignment]
2020

2121
from ..._collections import HTTPHeaderDict
2222
from ..._constant import (
@@ -311,8 +311,8 @@ def _custom_tls(
311311
cert_reqs: int | str | None = None,
312312
) -> bool:
313313
"""Meant to support TLS over QUIC meanwhile cpython does not ship with its native implementation."""
314-
if self._svn != HttpVersion.h3:
315-
return NotImplemented
314+
if self._svn is not HttpVersion.h3:
315+
return False
316316

317317
cert_use_common_name = False
318318

@@ -532,7 +532,7 @@ async def _post_conn(self) -> None: # type: ignore[override]
532532
self.conn_info.issuer_certificate_der = (
533533
ssl.PEM_cert_to_DER_cert(chain[1].public_bytes())
534534
)
535-
self.conn_info.issuer_certificate_dict = chain[1].get_info()
535+
self.conn_info.issuer_certificate_dict = chain[1].get_info() # type: ignore[assignment]
536536

537537
if cipher_tuple:
538538
self.conn_info.cipher = cipher_tuple[0]

src/urllib3/backend/hface.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020

2121
try: # We shouldn't do this, it is private. Only for chain extraction check. We should find another way.
22-
from _ssl import Certificate # type: ignore[import-not-found]
22+
from _ssl import Certificate
2323
except (ImportError, AttributeError):
24-
Certificate = None
24+
Certificate = None # type: ignore[misc,assignment]
2525

2626
from .._collections import HTTPHeaderDict
2727
from .._constant import (
@@ -336,8 +336,8 @@ def _custom_tls(
336336
cert_reqs: int | str | None = None,
337337
) -> bool:
338338
"""Meant to support TLS over QUIC meanwhile cpython does not ship with its native implementation."""
339-
if self._svn != HttpVersion.h3:
340-
return NotImplemented
339+
if self._svn is not HttpVersion.h3:
340+
return False
341341

342342
cert_use_common_name = False
343343

@@ -574,7 +574,7 @@ def _post_conn(self) -> None:
574574
self.conn_info.issuer_certificate_der = (
575575
ssl.PEM_cert_to_DER_cert(chain[1].public_bytes())
576576
)
577-
self.conn_info.issuer_certificate_dict = chain[1].get_info()
577+
self.conn_info.issuer_certificate_dict = chain[1].get_info() # type: ignore[assignment]
578578

579579
elif hasattr(self.sock, "getpeercert"):
580580
self.conn_info.certificate_der = self.sock.getpeercert(binary_form=True)
@@ -608,7 +608,7 @@ def _post_conn(self) -> None:
608608
self.conn_info.issuer_certificate_der = (
609609
ssl.PEM_cert_to_DER_cert(chain[1].public_bytes())
610610
)
611-
self.conn_info.issuer_certificate_dict = chain[1].get_info()
611+
self.conn_info.issuer_certificate_dict = chain[1].get_info() # type: ignore[assignment]
612612

613613
if cipher_tuple:
614614
self.conn_info.cipher = cipher_tuple[0]

src/urllib3/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def connect(self) -> None:
791791
self.assert_hostname,
792792
self.cert_reqs,
793793
)
794-
is NotImplemented
794+
is False
795795
):
796796
server_hostname: str = self.host
797797
tls_in_tls = False

src/urllib3/connectionpool.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,12 @@ def urlopen(
18941894
assert isinstance(response, ResponsePromise)
18951895
return response # actually a response promise!
18961896

1897-
assert isinstance(response, HTTPResponse)
1897+
# we are relaxing that constraint to behave properly
1898+
# with 3rd party mocking tool such as vcrpy.
1899+
# see https://github.com/jawah/urllib3.future/issues/320
1900+
# for more details.
1901+
# assert isinstance(response, HTTPResponse)
1902+
response = typing.cast(HTTPResponse, response)
18981903

18991904
if redirect and response.get_redirect_location():
19001905
# Handle redirect?

src/urllib3/contrib/_socks_legacy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from __future__ import annotations
4242

4343
try:
44-
import socks # type: ignore[import-not-found]
44+
import socks
4545
except ImportError:
4646
import warnings
4747

@@ -113,13 +113,13 @@ def _new_conn(self) -> socks.socksocket:
113113
try:
114114
conn = socks.create_connection(
115115
(self.host, self.port),
116-
proxy_type=self._socks_options["socks_version"],
116+
proxy_type=self._socks_options["socks_version"], # type: ignore[arg-type]
117117
proxy_addr=self._socks_options["proxy_host"],
118-
proxy_port=self._socks_options["proxy_port"],
118+
proxy_port=self._socks_options["proxy_port"], # type: ignore[arg-type]
119119
proxy_username=self._socks_options["username"],
120120
proxy_password=self._socks_options["password"],
121121
proxy_rdns=self._socks_options["rdns"],
122-
timeout=self.timeout,
122+
timeout=self.timeout, # type: ignore[arg-type]
123123
**extra_kw,
124124
)
125125

src/urllib3/contrib/hface/protocols/_protocols.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def multiplexed(self) -> bool:
329329
def should_wait_remote_flow_control(
330330
self, stream_id: int, amt: int | None = None
331331
) -> bool | None:
332-
return NotImplemented
332+
return NotImplemented # type: ignore[no-any-return]
333333

334334

335335
class HTTP2Protocol(HTTPOverTCPProtocol, metaclass=ABCMeta):

src/urllib3/contrib/imcc/_ctypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def pull_error(self) -> typing.NoReturn:
220220
#
221221
class PySSLContextStruct(ctypes.Structure):
222222
_fields_ = (
223-
_head_extra_fields
223+
_head_extra_fields # type: ignore[assignment]
224224
+ [
225225
("ob_refcnt", ctypes.c_ssize_t), # Py_ssize_t ob_refcnt;
226226
("ob_type", ctypes.c_void_p), # PyTypeObject *ob_type;

0 commit comments

Comments
 (0)