2121import logging
2222import random
2323import re
24- from typing import Any , Collection , Dict , List , Optional , Sequence , Tuple , Union
24+ from typing import Any , Collection , Dict , List , Optional , Sequence , Tuple , Union , cast
2525from urllib .parse import urlparse
2626from urllib .request import ( # type: ignore[attr-defined]
2727 getproxies_environment ,
4040 IProtocol ,
4141 IProtocolFactory ,
4242 IReactorCore ,
43+ IReactorTime ,
4344 IStreamClientEndpoint ,
4445)
4546from twisted .python .failure import Failure
@@ -129,7 +130,9 @@ def __init__(
129130 ):
130131 contextFactory = contextFactory or BrowserLikePolicyForHTTPS ()
131132
132- _AgentBase .__init__ (self , reactor , pool )
133+ # `_AgentBase` expects an `IReactorTime` provider. `IReactorCore`
134+ # extends `IReactorTime`, so this cast is safe.
135+ _AgentBase .__init__ (self , cast (IReactorTime , reactor ), pool )
133136
134137 if proxy_reactor is None :
135138 self .proxy_reactor = reactor
@@ -168,7 +171,7 @@ def __init__(
168171 self .no_proxy = no_proxy
169172
170173 self ._policy_for_https = contextFactory
171- self ._reactor = reactor
174+ self ._reactor = cast ( IReactorTime , reactor )
172175
173176 self ._federation_proxy_endpoint : Optional [IStreamClientEndpoint ] = None
174177 self ._federation_proxy_credentials : Optional [ProxyCredentials ] = None
@@ -257,7 +260,11 @@ def request(
257260 raise ValueError (f"Invalid URI { uri !r} " )
258261
259262 parsed_uri = URI .fromBytes (uri )
260- pool_key = f"{ parsed_uri .scheme !r} { parsed_uri .host !r} { parsed_uri .port } "
263+ pool_key : tuple [bytes , bytes , int ] = (
264+ parsed_uri .scheme ,
265+ parsed_uri .host ,
266+ parsed_uri .port ,
267+ )
261268 request_path = parsed_uri .originForm
262269
263270 should_skip_proxy = False
@@ -283,7 +290,7 @@ def request(
283290 )
284291 # Cache *all* connections under the same key, since we are only
285292 # connecting to a single destination, the proxy:
286- pool_key = "http-proxy"
293+ pool_key = ( b "http-proxy", b"" , 0 )
287294 endpoint = self .http_proxy_endpoint
288295 request_path = uri
289296 elif (
0 commit comments