1
1
"""Various helper functions."""
2
2
3
3
import sys
4
+ from collections .abc import Mapping
4
5
from functools import cached_property
5
6
from typing import Any , Callable , Generic , Optional , Protocol , TypeVar , Union , overload
6
7
13
14
Self = Any
14
15
15
16
_T = TypeVar ("_T" )
17
+ # We use Mapping to make it possible to use TypedDict, but this isn't
18
+ # technically type safe as we need to assign into the dict.
19
+ _Cache = TypeVar ("_Cache" , bound = Mapping [str , Any ])
16
20
17
21
18
- class _CacheImpl (Protocol ):
19
- _cache : dict [ str , Any ]
22
+ class _CacheImpl (Protocol [ _Cache ] ):
23
+ _cache : _Cache
20
24
21
25
22
26
class under_cached_property (Generic [_T ]):
@@ -29,7 +33,7 @@ class under_cached_property(Generic[_T]):
29
33
variable. It is, in Python parlance, a data descriptor.
30
34
"""
31
35
32
- def __init__ (self , wrapped : Callable [... , _T ]) -> None :
36
+ def __init__ (self , wrapped : Callable [[ Any ] , _T ]) -> None :
33
37
self .wrapped = wrapped
34
38
self .__doc__ = wrapped .__doc__
35
39
self .name = wrapped .__name__
@@ -38,10 +42,10 @@ def __init__(self, wrapped: Callable[..., _T]) -> None:
38
42
def __get__ (self , inst : None , owner : Optional [type [object ]] = None ) -> Self : ...
39
43
40
44
@overload
41
- def __get__ (self , inst : _CacheImpl , owner : Optional [type [object ]] = None ) -> _T : ...
45
+ def __get__ (self , inst : _CacheImpl [ Any ] , owner : Optional [type [object ]] = None ) -> _T : ... # type: ignore[misc]
42
46
43
47
def __get__ (
44
- self , inst : Optional [_CacheImpl ], owner : Optional [type [object ]] = None
48
+ self , inst : Optional [_CacheImpl [ Any ] ], owner : Optional [type [object ]] = None
45
49
) -> Union [_T , Self ]:
46
50
if inst is None :
47
51
return self
@@ -52,5 +56,5 @@ def __get__(
52
56
inst ._cache [self .name ] = val
53
57
return val
54
58
55
- def __set__ (self , inst : _CacheImpl , value : _T ) -> None :
59
+ def __set__ (self , inst : _CacheImpl [ Any ] , value : _T ) -> None :
56
60
raise AttributeError ("cached property is read-only" )
0 commit comments