Skip to content

Commit c97896d

Browse files
committed
Fix merge conflicts and typing in cache_selfie.
1 parent 5393b66 commit c97896d

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

python/selfie-lib/selfie_lib/CacheSelfie.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .Literals import LiteralValue, LiteralString, TodoStub
55
from .SnapshotSystem import DiskStorage
66
from .RoundTrip import Roundtrip
7+
import base64
78

89
T = TypeVar("T")
910

@@ -90,7 +91,9 @@ def to_match_disk_TODO(self, sub: str = "") -> T:
9091
return self._to_match_disk_impl(sub, True)
9192

9293
def _to_match_disk_impl(self, sub: str, is_todo: bool) -> T:
93-
system = get_system()
94+
from .Selfie import _selfieSystem
95+
96+
system = _selfieSystem()
9497
call = recordCall(False)
9598

9699
if system.mode.can_write(is_todo, call, system):
@@ -127,7 +130,9 @@ def to_be_file(self, subpath: str) -> T:
127130
return self._to_be_file_impl(subpath, False)
128131

129132
def _to_be_file_impl(self, subpath: str, is_todo: bool) -> T:
130-
system = get_system()
133+
from .Selfie import _selfieSystem
134+
135+
system = _selfieSystem()
131136
call = recordCall(False)
132137
writable = system.mode.can_write(is_todo, call, system)
133138

@@ -156,7 +161,9 @@ def to_be_base64(self, snapshot: str) -> T:
156161
return self._to_be_base64_impl(snapshot)
157162

158163
def _to_be_base64_impl(self, snapshot: Optional[str]) -> T:
159-
system = get_system()
164+
from .Selfie import _selfieSystem
165+
166+
system = _selfieSystem()
160167
call = recordCall(False)
161168
writable = system.mode.can_write(snapshot is None, call, system)
162169

python/selfie-lib/selfie_lib/Selfie.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypeVar, Any, Protocol, Union, overload
1+
from typing import Any, TypeVar, Optional, Protocol, Union, overload
22
from .SelfieImplementations import ReprSelfie, StringSelfie
33
from .SnapshotSystem import _selfieSystem
44
from .Snapshot import Snapshot
@@ -39,15 +39,25 @@ def expect_selfie(
3939
return ReprSelfie(actual)
4040

4141

42-
def cache_selfie_string(to_cache: Cacheable[str]) -> CacheSelfie[str]:
43-
"""Create a CacheSelfie instance for caching strings with identity transformation."""
44-
identity_roundtrip = Roundtrip.identity()
45-
return cache_selfie_generic(identity_roundtrip, to_cache)
42+
@overload
43+
def cache_selfie(to_cache: Cacheable[str]) -> CacheSelfie[str]: ...
4644

4745

48-
def cache_selfie_generic(
49-
roundtrip: Roundtrip[T, str], to_cache: Cacheable[T]
50-
) -> CacheSelfie[T]:
51-
"""Create a CacheSelfie instance for caching generic objects with specified roundtrip."""
52-
deferred_disk_storage = _selfieSystem().disk_thread_local()
53-
return CacheSelfie(deferred_disk_storage, roundtrip, to_cache)
46+
@overload
47+
def cache_selfie(
48+
to_cache: Cacheable[T], roundtrip: Roundtrip[T, str]
49+
) -> CacheSelfie[T]: ...
50+
51+
52+
def cache_selfie(
53+
to_cache: Union[Cacheable[str], Cacheable[T]],
54+
roundtrip: Optional[Roundtrip[T, str]] = None,
55+
) -> Union[CacheSelfie[str], CacheSelfie[T]]:
56+
if roundtrip is None:
57+
# the cacheable better be a string!
58+
return cache_selfie(to_cache, Roundtrip.identity()) # type: ignore
59+
elif isinstance(roundtrip, Roundtrip) and to_cache is not None:
60+
deferred_disk_storage = _selfieSystem().disk_thread_local()
61+
return CacheSelfie(deferred_disk_storage, roundtrip, to_cache)
62+
else:
63+
raise TypeError("Invalid arguments provided to cache_selfie")

0 commit comments

Comments
 (0)