5
5
import datetime
6
6
import functools
7
7
import json
8
+ from typing import Any , Dict , Union , Tuple
8
9
9
- import py
10
10
from dateutil .parser import isoparse
11
+ import _pytest
12
+ import _pytest .cacheprovider
11
13
12
14
13
- def json_iso_datetimes (obj ) :
15
+ def json_iso_datetimes (obj : Any ) -> str :
14
16
"""JSON serializer for objects not serializable by default json
15
17
module."""
16
18
if isinstance (obj , datetime .datetime ):
17
19
return obj .isoformat ()
18
20
19
- raise TypeError ("Unserializable type %s" % type (obj ))
21
+ raise TypeError (f "Unserializable type { type (obj )} " )
20
22
21
23
22
- def json_iso_datetime_string_to_datetime (obj ) :
24
+ def json_iso_datetime_string_to_datetime (obj : Dict [ Any , Any ]) -> Dict [ Any , Any ] :
23
25
"""JSON object hook that converts object vals from ISO datetime
24
26
strings to python datetime.datetime`s if possible."""
25
27
@@ -35,7 +37,11 @@ def json_iso_datetime_string_to_datetime(obj):
35
37
return obj
36
38
37
39
38
- def datetime_encode_set (self , key , value ):
40
+ def datetime_encode_set (
41
+ self : _pytest .cacheprovider .Cache ,
42
+ key : str ,
43
+ value : Union [str , int , float , Dict [Any , Any ], Tuple [Any ]],
44
+ ) -> None :
39
45
"""save value for the given key.
40
46
41
47
:param key: must be a ``/`` separated value. Usually the first
@@ -60,7 +66,9 @@ def datetime_encode_set(self, key, value):
60
66
self ._ensure_supporting_files ()
61
67
62
68
63
- def datetime_encode_get (self , key , default ):
69
+ def datetime_encode_get (
70
+ self : _pytest .cacheprovider .Cache , key : str , default : Any
71
+ ) -> Any :
64
72
"""return cached value for the given key. If no value
65
73
was yet cached or the value cannot be read, the specified
66
74
default is returned.
@@ -78,6 +86,8 @@ def datetime_encode_get(self, key, default):
78
86
return default
79
87
80
88
81
- def patch_cache_set (config ):
82
- config .cache .set = functools .partial (datetime_encode_set , config .cache )
83
- config .cache .get = functools .partial (datetime_encode_get , config .cache )
89
+ def patch_cache_set (config : _pytest .config .Config ) -> None :
90
+ assert config .cache , "pytest does not have a cache configured to patch"
91
+ # types ignored due to https://github.com/python/mypy/issues/2427
92
+ config .cache .set = functools .partial (datetime_encode_set , config .cache ) # type: ignore
93
+ config .cache .get = functools .partial (datetime_encode_get , config .cache ) # type: ignore
0 commit comments