Skip to content

Commit b942d42

Browse files
committed
refactor(decoders): MsgspecDecoder uses per-instance decoder cache
1 parent af8a246 commit b942d42

2 files changed

Lines changed: 29 additions & 32 deletions

File tree

‎src/httpware/decoders/msgspec.py‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
"""MsgspecDecoder — opt-in ResponseDecoder backed by a cached msgspec.json.Decoder."""
1+
"""MsgspecDecoder — opt-in ResponseDecoder backed by a per-instance msgspec.json.Decoder cache."""
22

3-
import functools
3+
import typing
44
from typing import TypeVar
55

66
from httpware._internal import import_checker
@@ -15,32 +15,35 @@
1515
T = TypeVar("T")
1616

1717

18-
@functools.lru_cache(maxsize=1024)
19-
def _get_msgspec_decoder(model: type[T]) -> "msgspec.json.Decoder[T]":
20-
return msgspec.json.Decoder(model)
21-
22-
2318
class MsgspecDecoder:
24-
"""Decode raw response bytes via a cached `msgspec.json.Decoder(model)`.
19+
"""Decode raw response bytes via a per-instance cached `msgspec.json.Decoder(model)`.
2520
2621
Requires the `msgspec` extra: `pip install httpware[msgspec]`. Importing
2722
this module without the extra works (the `msgspec` import is guarded by a
2823
`find_spec` check), but instantiating the decoder raises `ImportError`.
2924
"""
3025

26+
_msgspec_decoders: dict[type, "msgspec.json.Decoder[typing.Any]"]
27+
3128
def __init__(self) -> None:
3229
if not import_checker.is_msgspec_installed:
3330
raise ImportError(MISSING_DEPENDENCY_MESSAGE)
31+
self._msgspec_decoders = {}
32+
33+
def _get_msgspec_decoder(self, model: type[T]) -> "msgspec.json.Decoder[T]":
34+
decoder = self._msgspec_decoders.get(model)
35+
if decoder is None:
36+
decoder = msgspec.json.Decoder(model)
37+
self._msgspec_decoders[model] = decoder
38+
return decoder
3439

3540
def can_decode(self, model: type) -> bool:
3641
"""Return True iff msgspec natively understands `model`.
3742
38-
Cached via `_get_msgspec_decoder`; subsequent calls reuse the same
39-
Decoder instance. Rejects `pydantic.BaseModel` subclasses — msgspec
40-
will *build* a Decoder for them (falling back to a generic
41-
`CustomType`) but cannot actually decode them without a `dec_hook`,
42-
so we use `msgspec.inspect.type_info` to detect the fallback and
43-
refuse to claim the model.
43+
msgspec builds a Decoder for almost any class via a generic CustomType
44+
fallback; the Decoder constructor itself does NOT raise on unsupported
45+
types (e.g. pydantic.BaseModel). We use msgspec.inspect.type_info
46+
to detect the fallback and reject CustomType results explicitly.
4447
"""
4548
try:
4649
info = msgspec.inspect.type_info(model)
@@ -49,15 +52,15 @@ def can_decode(self, model: type) -> bool:
4952
if isinstance(info, msgspec.inspect.CustomType):
5053
return False
5154
try:
52-
_get_msgspec_decoder(model)
55+
self._get_msgspec_decoder(model)
5356
except Exception: # noqa: BLE001 — can_decode is a probe; any failure means no
5457
return False
5558
return True
5659

5760
def decode(self, content: bytes, model: type[T]) -> T:
5861
"""Validate `content` as JSON against `model` in a single parse pass."""
5962
try:
60-
decoder = _get_msgspec_decoder(model)
63+
decoder = self._get_msgspec_decoder(model)
6164
except TypeError:
6265
decoder = msgspec.json.Decoder(model)
6366
return decoder.decode(content)

‎tests/test_decoders_msgspec.py‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from httpware import AsyncClient, DecodeError
1313
from httpware._internal import import_checker
1414
from httpware.decoders import ResponseDecoder
15-
from httpware.decoders.msgspec import MsgspecDecoder, _get_msgspec_decoder
15+
from httpware.decoders.msgspec import MsgspecDecoder
1616

1717

1818
class _Item(msgspec.Struct):
@@ -86,11 +86,6 @@ class _DC:
8686
name: str
8787

8888

89-
@pytest.fixture(autouse=True)
90-
def _clear_msgspec_cache() -> None:
91-
_get_msgspec_decoder.cache_clear()
92-
93-
9489
def test_msgspec_can_decode_struct() -> None:
9590
assert MsgspecDecoder().can_decode(_Item) is True
9691

@@ -116,13 +111,11 @@ def test_msgspec_rejects_pydantic_basemodel() -> None:
116111

117112

118113
def test_msgspec_can_decode_uses_cache() -> None:
119-
_get_msgspec_decoder.cache_clear()
120114
decoder = MsgspecDecoder()
121115
decoder.can_decode(_Item)
122116
decoder.can_decode(_Item)
123-
info = _get_msgspec_decoder.cache_info()
124-
assert info.hits >= 1
125-
assert info.misses == 1
117+
assert len(decoder._msgspec_decoders) == 1 # noqa: SLF001
118+
assert _Item in decoder._msgspec_decoders # noqa: SLF001
126119

127120

128121
def test_can_decode_returns_false_when_type_info_raises() -> None:
@@ -136,9 +129,9 @@ def test_can_decode_returns_false_when_type_info_raises() -> None:
136129

137130
def test_can_decode_returns_false_when_decoder_build_raises() -> None:
138131
"""A `_get_msgspec_decoder` failure after type_info-classification is a soft 'no'."""
139-
_get_msgspec_decoder.cache_clear()
140-
with patch(
141-
"httpware.decoders.msgspec._get_msgspec_decoder",
132+
with patch.object(
133+
MsgspecDecoder,
134+
"_get_msgspec_decoder",
142135
side_effect=TypeError("cannot build decoder"),
143136
):
144137
assert MsgspecDecoder().can_decode(_Item) is False
@@ -150,10 +143,11 @@ def test_unhashable_model_falls_back_to_uncached_decoder() -> None:
150143
Mirrors `PydanticDecoder`'s unhashable-fallback test: when `_get_msgspec_decoder`
151144
raises `TypeError` (e.g., an unhashable parameterized type), `decode` bypasses
152145
the cache so the user-visible error is `msgspec`'s own decode error, not a
153-
`functools`-internal `TypeError`.
146+
`TypeError` from the cache lookup.
154147
"""
155-
with patch(
156-
"httpware.decoders.msgspec._get_msgspec_decoder",
148+
with patch.object(
149+
MsgspecDecoder,
150+
"_get_msgspec_decoder",
157151
side_effect=TypeError("unhashable type"),
158152
):
159153
result = MsgspecDecoder().decode(b"42", int)

0 commit comments

Comments
 (0)