1
1
# SPDX-FileCopyrightText: 2015 Eric Larson
2
2
#
3
3
# SPDX-License-Identifier: Apache-2.0
4
+ from __future__ import annotations
4
5
5
6
import functools
6
7
import types
7
8
import zlib
8
- from typing import TYPE_CHECKING , Any , Collection , Mapping , Optional , Tuple , Type , Union
9
+ from typing import TYPE_CHECKING , Any , Collection , Mapping
9
10
10
11
from pip ._vendor .requests .adapters import HTTPAdapter
11
12
@@ -27,16 +28,16 @@ class CacheControlAdapter(HTTPAdapter):
27
28
28
29
def __init__ (
29
30
self ,
30
- cache : Optional [ " BaseCache" ] = None ,
31
+ cache : BaseCache | None = None ,
31
32
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 ,
33
+ controller_class : type [ CacheController ] | None = None ,
34
+ serializer : Serializer | None = None ,
35
+ heuristic : BaseHeuristic | None = None ,
36
+ cacheable_methods : Collection [str ] | None = None ,
36
37
* args : Any ,
37
38
** kw : Any ,
38
39
) -> None :
39
- super (CacheControlAdapter , self ).__init__ (* args , ** kw )
40
+ super ().__init__ (* args , ** kw )
40
41
self .cache = DictCache () if cache is None else cache
41
42
self .heuristic = heuristic
42
43
self .cacheable_methods = cacheable_methods or ("GET" ,)
@@ -48,16 +49,14 @@ def __init__(
48
49
49
50
def send (
50
51
self ,
51
- request : " PreparedRequest" ,
52
+ request : PreparedRequest ,
52
53
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" :
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 :
61
60
"""
62
61
Send a request. Use the request information to see if it
63
62
exists in the cache and cache the response if we need to and can.
@@ -74,19 +73,17 @@ def send(
74
73
# check for etags and add headers if appropriate
75
74
request .headers .update (self .controller .conditional_headers (request ))
76
75
77
- resp = super (CacheControlAdapter , self ).send (
78
- request , stream , timeout , verify , cert , proxies
79
- )
76
+ resp = super ().send (request , stream , timeout , verify , cert , proxies )
80
77
81
78
return resp
82
79
83
80
def build_response (
84
81
self ,
85
- request : " PreparedRequest" ,
86
- response : " HTTPResponse" ,
82
+ request : PreparedRequest ,
83
+ response : HTTPResponse ,
87
84
from_cache : bool = False ,
88
- cacheable_methods : Optional [ Collection [str ]] = None ,
89
- ) -> " Response" :
85
+ cacheable_methods : Collection [str ] | None = None ,
86
+ ) -> Response :
90
87
"""
91
88
Build a response by making a request or using the cache.
92
89
@@ -137,7 +134,7 @@ def build_response(
137
134
if response .chunked :
138
135
super_update_chunk_length = response ._update_chunk_length # type: ignore[attr-defined]
139
136
140
- def _update_chunk_length (self : " HTTPResponse" ) -> None :
137
+ def _update_chunk_length (self : HTTPResponse ) -> None :
141
138
super_update_chunk_length ()
142
139
if self .chunk_left == 0 :
143
140
self ._fp ._close () # type: ignore[attr-defined]
@@ -146,9 +143,7 @@ def _update_chunk_length(self: "HTTPResponse") -> None:
146
143
_update_chunk_length , response
147
144
)
148
145
149
- resp : "Response" = super ( # type: ignore[no-untyped-call]
150
- CacheControlAdapter , self
151
- ).build_response (request , response )
146
+ resp : Response = super ().build_response (request , response ) # type: ignore[no-untyped-call]
152
147
153
148
# See if we should invalidate the cache.
154
149
if request .method in self .invalidating_methods and resp .ok :
@@ -163,4 +158,4 @@ def _update_chunk_length(self: "HTTPResponse") -> None:
163
158
164
159
def close (self ) -> None :
165
160
self .cache .close ()
166
- super (CacheControlAdapter , self ).close () # type: ignore[no-untyped-call]
161
+ super ().close () # type: ignore[no-untyped-call]
0 commit comments