2
2
#
3
3
# SPDX-License-Identifier: Apache-2.0
4
4
5
- import types
6
5
import functools
6
+ import types
7
7
import zlib
8
+ from typing import TYPE_CHECKING , Any , Collection , Mapping , Optional , Tuple , Type , Union
8
9
9
10
from pip ._vendor .requests .adapters import HTTPAdapter
10
11
11
- from .controller import CacheController , PERMANENT_REDIRECT_STATUSES
12
- from .cache import DictCache
13
- from .filewrapper import CallbackFileWrapper
12
+ from pip ._vendor .cachecontrol .cache import DictCache
13
+ from pip ._vendor .cachecontrol .controller import PERMANENT_REDIRECT_STATUSES , CacheController
14
+ from pip ._vendor .cachecontrol .filewrapper import CallbackFileWrapper
15
+
16
+ if TYPE_CHECKING :
17
+ from pip ._vendor .requests import PreparedRequest , Response
18
+ from pip ._vendor .urllib3 import HTTPResponse
19
+
20
+ from pip ._vendor .cachecontrol .cache import BaseCache
21
+ from pip ._vendor .cachecontrol .heuristics import BaseHeuristic
22
+ from pip ._vendor .cachecontrol .serialize import Serializer
14
23
15
24
16
25
class CacheControlAdapter (HTTPAdapter ):
17
26
invalidating_methods = {"PUT" , "PATCH" , "DELETE" }
18
27
19
28
def __init__ (
20
29
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
+ cache : Optional [ "BaseCache" ] = None ,
31
+ cache_etags : bool = True ,
32
+ controller_class : Optional [ Type [ CacheController ]] = None ,
33
+ serializer : Optional [ "Serializer" ] = None ,
34
+ heuristic : Optional [ "BaseHeuristic" ] = None ,
35
+ cacheable_methods : Optional [ Collection [ str ]] = None ,
36
+ * args : Any ,
37
+ ** kw : Any ,
38
+ ) -> None :
30
39
super (CacheControlAdapter , self ).__init__ (* args , ** kw )
31
40
self .cache = DictCache () if cache is None else cache
32
41
self .heuristic = heuristic
@@ -37,7 +46,18 @@ def __init__(
37
46
self .cache , cache_etags = cache_etags , serializer = serializer
38
47
)
39
48
40
- def send (self , request , cacheable_methods = None , ** kw ):
49
+ def send (
50
+ self ,
51
+ request : "PreparedRequest" ,
52
+ stream : bool = False ,
53
+ timeout : Union [None , float , Tuple [float , float ], Tuple [float , None ]] = None ,
54
+ verify : Union [bool , str ] = True ,
55
+ cert : Union [
56
+ None , bytes , str , Tuple [Union [bytes , str ], Union [bytes , str ]]
57
+ ] = None ,
58
+ proxies : Optional [Mapping [str , str ]] = None ,
59
+ cacheable_methods : Optional [Collection [str ]] = None ,
60
+ ) -> "Response" :
41
61
"""
42
62
Send a request. Use the request information to see if it
43
63
exists in the cache and cache the response if we need to and can.
@@ -54,13 +74,19 @@ def send(self, request, cacheable_methods=None, **kw):
54
74
# check for etags and add headers if appropriate
55
75
request .headers .update (self .controller .conditional_headers (request ))
56
76
57
- resp = super (CacheControlAdapter , self ).send (request , ** kw )
77
+ resp = super (CacheControlAdapter , self ).send (
78
+ request , stream , timeout , verify , cert , proxies
79
+ )
58
80
59
81
return resp
60
82
61
83
def build_response (
62
- self , request , response , from_cache = False , cacheable_methods = None
63
- ):
84
+ self ,
85
+ request : "PreparedRequest" ,
86
+ response : "HTTPResponse" ,
87
+ from_cache : bool = False ,
88
+ cacheable_methods : Optional [Collection [str ]] = None ,
89
+ ) -> "Response" :
64
90
"""
65
91
Build a response by making a request or using the cache.
66
92
@@ -102,36 +128,39 @@ def build_response(
102
128
else :
103
129
# Wrap the response file with a wrapper that will cache the
104
130
# response when the stream has been consumed.
105
- response ._fp = CallbackFileWrapper (
106
- response ._fp ,
131
+ response ._fp = CallbackFileWrapper ( # type: ignore[attr-defined]
132
+ response ._fp , # type: ignore[attr-defined]
107
133
functools .partial (
108
134
self .controller .cache_response , request , response
109
135
),
110
136
)
111
137
if response .chunked :
112
- super_update_chunk_length = response ._update_chunk_length
138
+ super_update_chunk_length = response ._update_chunk_length # type: ignore[attr-defined]
113
139
114
- def _update_chunk_length (self ) :
140
+ def _update_chunk_length (self : "HTTPResponse" ) -> None :
115
141
super_update_chunk_length ()
116
142
if self .chunk_left == 0 :
117
- self ._fp ._close ()
143
+ self ._fp ._close () # type: ignore[attr-defined]
118
144
119
- response ._update_chunk_length = types .MethodType (
145
+ response ._update_chunk_length = types .MethodType ( # type: ignore[attr-defined]
120
146
_update_chunk_length , response
121
147
)
122
148
123
- resp = super (CacheControlAdapter , self ).build_response (request , response )
149
+ resp : "Response" = super ( # type: ignore[no-untyped-call]
150
+ CacheControlAdapter , self
151
+ ).build_response (request , response )
124
152
125
153
# See if we should invalidate the cache.
126
154
if request .method in self .invalidating_methods and resp .ok :
155
+ assert request .url is not None
127
156
cache_url = self .controller .cache_url (request .url )
128
157
self .cache .delete (cache_url )
129
158
130
159
# Give the request a from_cache attr to let people use it
131
- resp .from_cache = from_cache
160
+ resp .from_cache = from_cache # type: ignore[attr-defined]
132
161
133
162
return resp
134
163
135
- def close (self ):
164
+ def close (self ) -> None :
136
165
self .cache .close ()
137
- super (CacheControlAdapter , self ).close ()
166
+ super (CacheControlAdapter , self ).close () # type: ignore[no-untyped-call]
0 commit comments