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
44from typing import TypeVar
55
66from httpware ._internal import import_checker
1515T = 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-
2318class 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 )
0 commit comments