Skip to content

Commit 70f059a

Browse files
authored
Update cache (#413)
* add type annotations to cache.py refs: #355 * cache: remove unused 'import py' * cache: replace string interpolation with f-string
1 parent eaec9a2 commit 70f059a

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

cache.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@
55
import datetime
66
import functools
77
import json
8+
from typing import Any, Dict, Union, Tuple
89

9-
import py
1010
from dateutil.parser import isoparse
11+
import _pytest
12+
import _pytest.cacheprovider
1113

1214

13-
def json_iso_datetimes(obj):
15+
def json_iso_datetimes(obj: Any) -> str:
1416
"""JSON serializer for objects not serializable by default json
1517
module."""
1618
if isinstance(obj, datetime.datetime):
1719
return obj.isoformat()
1820

19-
raise TypeError("Unserializable type %s" % type(obj))
21+
raise TypeError(f"Unserializable type {type(obj)}")
2022

2123

22-
def json_iso_datetime_string_to_datetime(obj):
24+
def json_iso_datetime_string_to_datetime(obj: Dict[Any, Any]) -> Dict[Any, Any]:
2325
"""JSON object hook that converts object vals from ISO datetime
2426
strings to python datetime.datetime`s if possible."""
2527

@@ -35,7 +37,11 @@ def json_iso_datetime_string_to_datetime(obj):
3537
return obj
3638

3739

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:
3945
"""save value for the given key.
4046
4147
:param key: must be a ``/`` separated value. Usually the first
@@ -60,7 +66,9 @@ def datetime_encode_set(self, key, value):
6066
self._ensure_supporting_files()
6167

6268

63-
def datetime_encode_get(self, key, default):
69+
def datetime_encode_get(
70+
self: _pytest.cacheprovider.Cache, key: str, default: Any
71+
) -> Any:
6472
"""return cached value for the given key. If no value
6573
was yet cached or the value cannot be read, the specified
6674
default is returned.
@@ -78,6 +86,8 @@ def datetime_encode_get(self, key, default):
7886
return default
7987

8088

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

Comments
 (0)