Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cachew/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def orjson_dumps(*args: Any, **kwargs: Any) -> bytes: # type: ignore[misc]
# sqlite needs a blob
return json.dumps(*args, **kwargs).encode('utf8')

orjson_loads = json.loads # type: ignore[assignment] # ty: ignore[invalid-assignment]
orjson_loads = json.loads # type: ignore[assignment]

import platformdirs

Expand Down
5 changes: 3 additions & 2 deletions src/cachew/marshall/cachew.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,16 @@ def build_schema(Type) -> Schema:
if is_dictish:
(ft, tt) = args
fts = build_schema(ft)
if not isinstance(fts, SPrimitive):
raise TypeNotSupported(type_=Type, reason='dictionary key type must be primitive')
tts = build_schema(tt)
assert isinstance(fts, SPrimitive)
return SDict(
type=origin,
ft=fts,
tt=tts,
)

raise RuntimeError(f"unsupported: {Type=} {origin=} {args=}")
raise TypeNotSupported(type_=Type, reason=f'generic type with origin {origin} is unsupported')


######### tests
Expand Down
32 changes: 32 additions & 0 deletions src/cachew/tests/test_cachew.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,38 @@ def fun2() -> Iterable[UGood | UBad]:
yield UGood(x=2)


def test_unsupported_annotation_follows_error_policy(
*,
tmp_path: Path,
restore_settings,
) -> None:
"""
Unsupported annotations must not bypass Cachew's strict or defensive setup policy.
Strict mode must reject decoration without running the source, while defensive mode must return the original function and leave it uncached.
"""

source_calls = 0

def source() -> Iterator[Callable[[int], str]]:
nonlocal source_calls
source_calls += 1
yield str

cache_path = tmp_path / 'cache'
settings.THROW_ON_ERROR = True
with pytest.raises(CachewException, match='failed to infer cache type'):
cachew(cache_path=cache_path)(source)
assert source_calls == 0

settings.THROW_ON_ERROR = False
decorated = cachew(cache_path=cache_path)(source)
assert decorated is source
assert list(decorated()) == [str]
assert list(decorated()) == [str]
assert source_calls == 2
assert cache_path.exists() is False


class BB(NamedTuple):
xx: int
yy: int
Expand Down