1313# limitations under the License.
1414
1515from functools import partial
16+ from typing import List , Tuple
1617
1718from twisted .internet import defer
1819
2223
2324
2425class DeferredCacheTestCase (TestCase ):
25- def test_empty (self ):
26- cache = DeferredCache ("test" )
26+ def test_empty (self ) -> None :
27+ cache : DeferredCache [ str , int ] = DeferredCache ("test" )
2728 with self .assertRaises (KeyError ):
2829 cache .get ("foo" )
2930
30- def test_hit (self ):
31- cache = DeferredCache ("test" )
31+ def test_hit (self ) -> None :
32+ cache : DeferredCache [ str , int ] = DeferredCache ("test" )
3233 cache .prefill ("foo" , 123 )
3334
3435 self .assertEqual (self .successResultOf (cache .get ("foo" )), 123 )
3536
36- def test_hit_deferred (self ):
37- cache = DeferredCache ("test" )
38- origin_d = defer .Deferred ()
37+ def test_hit_deferred (self ) -> None :
38+ cache : DeferredCache [ str , int ] = DeferredCache ("test" )
39+ origin_d : "defer.Deferred[int]" = defer .Deferred ()
3940 set_d = cache .set ("k1" , origin_d )
4041
4142 # get should return an incomplete deferred
4243 get_d = cache .get ("k1" )
4344 self .assertFalse (get_d .called )
4445
4546 # add a callback that will make sure that the set_d gets called before the get_d
46- def check1 (r ) :
47+ def check1 (r : str ) -> str :
4748 self .assertTrue (set_d .called )
4849 return r
4950
@@ -55,16 +56,16 @@ def check1(r):
5556 self .assertEqual (self .successResultOf (set_d ), 99 )
5657 self .assertEqual (self .successResultOf (get_d ), 99 )
5758
58- def test_callbacks (self ):
59+ def test_callbacks (self ) -> None :
5960 """Invalidation callbacks are called at the right time"""
60- cache = DeferredCache ("test" )
61+ cache : DeferredCache [ str , int ] = DeferredCache ("test" )
6162 callbacks = set ()
6263
6364 # start with an entry, with a callback
6465 cache .prefill ("k1" , 10 , callback = lambda : callbacks .add ("prefill" ))
6566
6667 # now replace that entry with a pending result
67- origin_d = defer .Deferred ()
68+ origin_d : "defer.Deferred[int]" = defer .Deferred ()
6869 set_d = cache .set ("k1" , origin_d , callback = lambda : callbacks .add ("set" ))
6970
7071 # ... and also make a get request
@@ -89,15 +90,15 @@ def test_callbacks(self):
8990 cache .prefill ("k1" , 30 )
9091 self .assertEqual (callbacks , {"set" , "get" })
9192
92- def test_set_fail (self ):
93- cache = DeferredCache ("test" )
93+ def test_set_fail (self ) -> None :
94+ cache : DeferredCache [ str , int ] = DeferredCache ("test" )
9495 callbacks = set ()
9596
9697 # start with an entry, with a callback
9798 cache .prefill ("k1" , 10 , callback = lambda : callbacks .add ("prefill" ))
9899
99100 # now replace that entry with a pending result
100- origin_d = defer .Deferred ()
101+ origin_d : defer . Deferred = defer .Deferred ()
101102 set_d = cache .set ("k1" , origin_d , callback = lambda : callbacks .add ("set" ))
102103
103104 # ... and also make a get request
@@ -126,9 +127,9 @@ def test_set_fail(self):
126127 cache .prefill ("k1" , 30 )
127128 self .assertEqual (callbacks , {"prefill" , "get2" })
128129
129- def test_get_immediate (self ):
130- cache = DeferredCache ("test" )
131- d1 = defer .Deferred ()
130+ def test_get_immediate (self ) -> None :
131+ cache : DeferredCache [ str , int ] = DeferredCache ("test" )
132+ d1 : "defer.Deferred[int]" = defer .Deferred ()
132133 cache .set ("key1" , d1 )
133134
134135 # get_immediate should return default
@@ -142,27 +143,27 @@ def test_get_immediate(self):
142143 v = cache .get_immediate ("key1" , 1 )
143144 self .assertEqual (v , 2 )
144145
145- def test_invalidate (self ):
146- cache = DeferredCache ("test" )
146+ def test_invalidate (self ) -> None :
147+ cache : DeferredCache [ Tuple [ str ], int ] = DeferredCache ("test" )
147148 cache .prefill (("foo" ,), 123 )
148149 cache .invalidate (("foo" ,))
149150
150151 with self .assertRaises (KeyError ):
151152 cache .get (("foo" ,))
152153
153- def test_invalidate_all (self ):
154- cache = DeferredCache ("testcache" )
154+ def test_invalidate_all (self ) -> None :
155+ cache : DeferredCache [ str , str ] = DeferredCache ("testcache" )
155156
156157 callback_record = [False , False ]
157158
158- def record_callback (idx ) :
159+ def record_callback (idx : int ) -> None :
159160 callback_record [idx ] = True
160161
161162 # add a couple of pending entries
162- d1 = defer .Deferred ()
163+ d1 : "defer.Deferred[str]" = defer .Deferred ()
163164 cache .set ("key1" , d1 , partial (record_callback , 0 ))
164165
165- d2 = defer .Deferred ()
166+ d2 : "defer.Deferred[str]" = defer .Deferred ()
166167 cache .set ("key2" , d2 , partial (record_callback , 1 ))
167168
168169 # lookup should return pending deferreds
@@ -193,8 +194,8 @@ def record_callback(idx):
193194 with self .assertRaises (KeyError ):
194195 cache .get ("key1" , None )
195196
196- def test_eviction (self ):
197- cache = DeferredCache (
197+ def test_eviction (self ) -> None :
198+ cache : DeferredCache [ int , str ] = DeferredCache (
198199 "test" , max_entries = 2 , apply_cache_factor_from_config = False
199200 )
200201
@@ -208,8 +209,8 @@ def test_eviction(self):
208209 cache .get (2 )
209210 cache .get (3 )
210211
211- def test_eviction_lru (self ):
212- cache = DeferredCache (
212+ def test_eviction_lru (self ) -> None :
213+ cache : DeferredCache [ int , str ] = DeferredCache (
213214 "test" , max_entries = 2 , apply_cache_factor_from_config = False
214215 )
215216
@@ -227,8 +228,8 @@ def test_eviction_lru(self):
227228 cache .get (1 )
228229 cache .get (3 )
229230
230- def test_eviction_iterable (self ):
231- cache = DeferredCache (
231+ def test_eviction_iterable (self ) -> None :
232+ cache : DeferredCache [ int , List [ str ]] = DeferredCache (
232233 "test" ,
233234 max_entries = 3 ,
234235 apply_cache_factor_from_config = False ,
0 commit comments