11# SPDX-FileCopyrightText: 2015 Eric Larson
22#
33# SPDX-License-Identifier: Apache-2.0
4+ from __future__ import annotations
45
5- import types
66import functools
7+ import types
78import zlib
9+ from typing import TYPE_CHECKING , Any , Collection , Mapping
810
911from requests .adapters import HTTPAdapter
1012
11- from .controller import CacheController , PERMANENT_REDIRECT_STATUSES
12- from .cache import DictCache
13- from .filewrapper import CallbackFileWrapper
13+ from cachecontrol .cache import DictCache
14+ from cachecontrol .controller import PERMANENT_REDIRECT_STATUSES , CacheController
15+ from cachecontrol .filewrapper import CallbackFileWrapper
16+
17+ if TYPE_CHECKING :
18+ from requests import PreparedRequest , Response
19+ from urllib3 import HTTPResponse
20+
21+ from cachecontrol .cache import BaseCache
22+ from cachecontrol .heuristics import BaseHeuristic
23+ from cachecontrol .serialize import Serializer
1424
1525
1626class CacheControlAdapter (HTTPAdapter ):
1727 invalidating_methods = {"PUT" , "PATCH" , "DELETE" }
1828
1929 def __init__ (
2030 self ,
21- cache = None ,
22- cache_etags = True ,
23- controller_class = None ,
24- serializer = None ,
25- heuristic = None ,
26- cacheable_methods = None ,
27- * args ,
28- ** kw
29- ):
30- super (CacheControlAdapter , self ).__init__ (* args , ** kw )
31+ cache : BaseCache | None = None ,
32+ cache_etags : bool = True ,
33+ controller_class : type [ CacheController ] | None = None ,
34+ serializer : Serializer | None = None ,
35+ heuristic : BaseHeuristic | None = None ,
36+ cacheable_methods : Collection [ str ] | None = None ,
37+ * args : Any ,
38+ ** kw : Any ,
39+ ) -> None :
40+ super ().__init__ (* args , ** kw )
3141 self .cache = DictCache () if cache is None else cache
3242 self .heuristic = heuristic
3343 self .cacheable_methods = cacheable_methods or ("GET" ,)
@@ -37,7 +47,16 @@ def __init__(
3747 self .cache , cache_etags = cache_etags , serializer = serializer
3848 )
3949
40- def send (self , request , cacheable_methods = None , ** kw ):
50+ def send (
51+ self ,
52+ request : PreparedRequest ,
53+ stream : bool = False ,
54+ timeout : None | float | tuple [float , float ] | tuple [float , None ] = None ,
55+ verify : bool | str = True ,
56+ cert : (None | bytes | str | tuple [bytes | str , bytes | str ]) = None ,
57+ proxies : Mapping [str , str ] | None = None ,
58+ cacheable_methods : Collection [str ] | None = None ,
59+ ) -> Response :
4160 """
4261 Send a request. Use the request information to see if it
4362 exists in the cache and cache the response if we need to and can.
@@ -54,13 +73,17 @@ def send(self, request, cacheable_methods=None, **kw):
5473 # check for etags and add headers if appropriate
5574 request .headers .update (self .controller .conditional_headers (request ))
5675
57- resp = super (CacheControlAdapter , self ).send (request , ** kw )
76+ resp = super ().send (request , stream , timeout , verify , cert , proxies )
5877
5978 return resp
6079
6180 def build_response (
62- self , request , response , from_cache = False , cacheable_methods = None
63- ):
81+ self ,
82+ request : PreparedRequest ,
83+ response : HTTPResponse ,
84+ from_cache : bool = False ,
85+ cacheable_methods : Collection [str ] | None = None ,
86+ ) -> Response :
6487 """
6588 Build a response by making a request or using the cache.
6689
@@ -102,36 +125,37 @@ def build_response(
102125 else :
103126 # Wrap the response file with a wrapper that will cache the
104127 # response when the stream has been consumed.
105- response ._fp = CallbackFileWrapper (
106- response ._fp ,
128+ response ._fp = CallbackFileWrapper ( # type: ignore[attr-defined]
129+ response ._fp , # type: ignore[attr-defined]
107130 functools .partial (
108131 self .controller .cache_response , request , response
109132 ),
110133 )
111134 if response .chunked :
112- super_update_chunk_length = response ._update_chunk_length
135+ super_update_chunk_length = response ._update_chunk_length # type: ignore[attr-defined]
113136
114- def _update_chunk_length (self ) :
137+ def _update_chunk_length (self : HTTPResponse ) -> None :
115138 super_update_chunk_length ()
116139 if self .chunk_left == 0 :
117- self ._fp ._close ()
140+ self ._fp ._close () # type: ignore[attr-defined]
118141
119- response ._update_chunk_length = types .MethodType (
142+ response ._update_chunk_length = types .MethodType ( # type: ignore[attr-defined]
120143 _update_chunk_length , response
121144 )
122145
123- resp = super (CacheControlAdapter , self ).build_response (request , response )
146+ resp : Response = super ().build_response (request , response ) # type: ignore[no-untyped-call]
124147
125148 # See if we should invalidate the cache.
126149 if request .method in self .invalidating_methods and resp .ok :
150+ assert request .url is not None
127151 cache_url = self .controller .cache_url (request .url )
128152 self .cache .delete (cache_url )
129153
130154 # Give the request a from_cache attr to let people use it
131- resp .from_cache = from_cache
155+ resp .from_cache = from_cache # type: ignore[attr-defined]
132156
133157 return resp
134158
135- def close (self ):
159+ def close (self ) -> None :
136160 self .cache .close ()
137- super (CacheControlAdapter , self ).close ()
161+ super ().close () # type: ignore[no-untyped-call]
0 commit comments