55from typing import Annotated , NoReturn , Any
66import numpy as np
77
8- from pydantic import BeforeValidator , PlainSerializer
8+ from pydantic import PlainSerializer
99from starlette .datastructures import Headers , MutableHeaders
1010from starlette .types import ASGIApp , Message , Receive , Scope , Send
1111
1212
13- def to_python_primitive (v ):
13+ def to_python_primitive (v : Any ) -> Any :
14+ """
15+ Convert complex types to Python primitives.
16+
17+ Args:
18+ v: Any value, particularly those with a 'source' attribute
19+ containing numpy arrays or generic types.
20+
21+ Returns:
22+ The primitive representation of the input value.
23+ """
1424 if hasattr (v , "source" ):
1525 if isinstance (v .source , np .ndarray ):
1626 return v .source .tolist ()
@@ -19,13 +29,11 @@ def to_python_primitive(v):
1929 return v .source .item ()
2030
2131 return str (v .source )
22-
2332 return str (v )
2433
2534
2635AnyToPrimitive = Annotated [
2736 Any ,
28- BeforeValidator (lambda v : v ),
2937 PlainSerializer (to_python_primitive ),
3038]
3139
@@ -145,7 +153,6 @@ async def send_with_gzip(self, message: Message) -> None:
145153 self .gzip_buffer .truncate ()
146154
147155 await self .send (message )
148- return
149156
150157
151158async def unattached_send (message : Message ) -> NoReturn :
0 commit comments