14
14
import enum
15
15
import logging
16
16
import threading
17
- from typing import Dict , Generic , Iterable , Optional , Set , TypeVar
17
+ from typing import Any , Dict , Generic , Iterable , Optional , Set , TypeVar
18
18
19
19
import attr
20
20
31
31
DV = TypeVar ("DV" )
32
32
33
33
34
+ # This class can't be generic because it uses slots with attrs.
35
+ # See: https://github.com/python-attrs/attrs/issues/313
34
36
@attr .s (slots = True )
35
- class DictionaryEntry ( Generic [DKT , DV ]):
37
+ class DictionaryEntry : # should be: Generic[DKT, DV].
36
38
"""Returned when getting an entry from the cache
37
39
38
40
Attributes:
@@ -45,8 +47,8 @@ class DictionaryEntry(Generic[DKT, DV]):
45
47
"""
46
48
47
49
full = attr .ib (type = bool )
48
- known_absent = attr .ib (type = Set [DKT ])
49
- value = attr .ib (type = Dict [DKT , DV ])
50
+ known_absent = attr .ib (type = Set [Any ]) # should be: Set[DKT]
51
+ value = attr .ib (type = Dict [Any , Any ]) # should be: Dict[ DKT, DV]
50
52
51
53
def __len__ (self ) -> int :
52
54
return len (self .value )
@@ -64,7 +66,7 @@ class DictionaryCache(Generic[KT, DKT, DV]):
64
66
"""
65
67
66
68
def __init__ (self , name : str , max_entries : int = 1000 ):
67
- self .cache : LruCache [KT , DictionaryEntry [ DKT , DV ] ] = LruCache (
69
+ self .cache : LruCache [KT , DictionaryEntry ] = LruCache (
68
70
max_size = max_entries , cache_name = name , size_callback = len
69
71
)
70
72
@@ -84,7 +86,7 @@ def check_thread(self) -> None:
84
86
85
87
def get (
86
88
self , key : KT , dict_keys : Optional [Iterable [DKT ]] = None
87
- ) -> DictionaryEntry [ DKT , DV ] :
89
+ ) -> DictionaryEntry :
88
90
"""Fetch an entry out of the cache
89
91
90
92
Args:
@@ -158,9 +160,7 @@ def _update_or_insert(
158
160
# We pop and reinsert as we need to tell the cache the size may have
159
161
# changed
160
162
161
- entry : DictionaryEntry [DKT , DV ] = self .cache .pop (
162
- key , DictionaryEntry (False , set (), {})
163
- )
163
+ entry : DictionaryEntry = self .cache .pop (key , DictionaryEntry (False , set (), {}))
164
164
entry .value .update (value )
165
165
entry .known_absent .update (known_absent )
166
166
self .cache [key ] = entry
0 commit comments