|
1 | | -from typing import TypeVar, Any, Protocol, Union, overload |
| 1 | +from typing import Any, TypeVar, Optional, Protocol, Union, overload |
2 | 2 | from .SelfieImplementations import ReprSelfie, StringSelfie |
3 | 3 | from .SnapshotSystem import _selfieSystem |
4 | 4 | from .Snapshot import Snapshot |
@@ -39,15 +39,25 @@ def expect_selfie( |
39 | 39 | return ReprSelfie(actual) |
40 | 40 |
|
41 | 41 |
|
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]: ... |
46 | 44 |
|
47 | 45 |
|
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