99import enum
1010import json
1111from abc import ABC , abstractmethod
12- from typing import Any , Final , Literal , Optional , Protocol , Union , overload
12+ from typing import Any , Final , Literal , Protocol , overload
1313
1414from sqlspec .typing import MSGSPEC_INSTALLED , ORJSON_INSTALLED , PYDANTIC_INSTALLED , BaseModel
1515
@@ -43,7 +43,7 @@ class JSONSerializer(Protocol):
4343 Users can implement this protocol to create custom serializers.
4444 """
4545
46- def encode (self , data : Any , * , as_bytes : bool = False ) -> Union [ str , bytes ] :
46+ def encode (self , data : Any , * , as_bytes : bool = False ) -> str | bytes :
4747 """Encode data to JSON.
4848
4949 Args:
@@ -55,7 +55,7 @@ def encode(self, data: Any, *, as_bytes: bool = False) -> Union[str, bytes]:
5555 """
5656 ...
5757
58- def decode (self , data : Union [ str , bytes ] , * , decode_bytes : bool = True ) -> Any :
58+ def decode (self , data : str | bytes , * , decode_bytes : bool = True ) -> Any :
5959 """Decode from JSON.
6060
6161 Args:
@@ -74,12 +74,12 @@ class BaseJSONSerializer(ABC):
7474 __slots__ = ()
7575
7676 @abstractmethod
77- def encode (self , data : Any , * , as_bytes : bool = False ) -> Union [ str , bytes ] :
77+ def encode (self , data : Any , * , as_bytes : bool = False ) -> str | bytes :
7878 """Encode data to JSON."""
7979 ...
8080
8181 @abstractmethod
82- def decode (self , data : Union [ str , bytes ] , * , decode_bytes : bool = True ) -> Any :
82+ def decode (self , data : str | bytes , * , decode_bytes : bool = True ) -> Any :
8383 """Decode from JSON."""
8484 ...
8585
@@ -96,7 +96,7 @@ def __init__(self) -> None:
9696 self ._encoder : Final [Encoder ] = Encoder (enc_hook = _type_to_string )
9797 self ._decoder : Final [Decoder ] = Decoder ()
9898
99- def encode (self , data : Any , * , as_bytes : bool = False ) -> Union [ str , bytes ] :
99+ def encode (self , data : Any , * , as_bytes : bool = False ) -> str | bytes :
100100 """Encode data using msgspec."""
101101 try :
102102 if as_bytes :
@@ -107,7 +107,7 @@ def encode(self, data: Any, *, as_bytes: bool = False) -> Union[str, bytes]:
107107 return OrjsonSerializer ().encode (data , as_bytes = as_bytes )
108108 return StandardLibSerializer ().encode (data , as_bytes = as_bytes )
109109
110- def decode (self , data : Union [ str , bytes ] , * , decode_bytes : bool = True ) -> Any :
110+ def decode (self , data : str | bytes , * , decode_bytes : bool = True ) -> Any :
111111 """Decode data using msgspec."""
112112 if isinstance (data , bytes ):
113113 if decode_bytes :
@@ -132,7 +132,7 @@ class OrjsonSerializer(BaseJSONSerializer):
132132
133133 __slots__ = ()
134134
135- def encode (self , data : Any , * , as_bytes : bool = False ) -> Union [ str , bytes ] :
135+ def encode (self , data : Any , * , as_bytes : bool = False ) -> str | bytes :
136136 """Encode data using orjson."""
137137 from orjson import (
138138 OPT_NAIVE_UTC , # pyright: ignore[reportUnknownVariableType]
@@ -146,7 +146,7 @@ def encode(self, data: Any, *, as_bytes: bool = False) -> Union[str, bytes]:
146146 )
147147 return result if as_bytes else result .decode ("utf-8" )
148148
149- def decode (self , data : Union [ str , bytes ] , * , decode_bytes : bool = True ) -> Any :
149+ def decode (self , data : str | bytes , * , decode_bytes : bool = True ) -> Any :
150150 """Decode data using orjson."""
151151 from orjson import loads as _orjson_loads # pyright: ignore[reportMissingImports]
152152
@@ -162,12 +162,12 @@ class StandardLibSerializer(BaseJSONSerializer):
162162
163163 __slots__ = ()
164164
165- def encode (self , data : Any , * , as_bytes : bool = False ) -> Union [ str , bytes ] :
165+ def encode (self , data : Any , * , as_bytes : bool = False ) -> str | bytes :
166166 """Encode data using standard library json."""
167167 json_str = json .dumps (data , default = _type_to_string )
168168 return json_str .encode ("utf-8" ) if as_bytes else json_str
169169
170- def decode (self , data : Union [ str , bytes ] , * , decode_bytes : bool = True ) -> Any :
170+ def decode (self , data : str | bytes , * , decode_bytes : bool = True ) -> Any :
171171 """Decode data using standard library json."""
172172 if isinstance (data , bytes ):
173173 if decode_bytes :
@@ -176,7 +176,7 @@ def decode(self, data: Union[str, bytes], *, decode_bytes: bool = True) -> Any:
176176 return json .loads (data )
177177
178178
179- _default_serializer : Optional [ JSONSerializer ] = None
179+ _default_serializer : JSONSerializer | None = None
180180
181181
182182def get_default_serializer () -> JSONSerializer :
@@ -213,7 +213,7 @@ def encode_json(data: Any, *, as_bytes: Literal[False] = ...) -> str: ... # pra
213213def encode_json (data : Any , * , as_bytes : Literal [True ]) -> bytes : ... # pragma: no cover
214214
215215
216- def encode_json (data : Any , * , as_bytes : bool = False ) -> Union [ str , bytes ] :
216+ def encode_json (data : Any , * , as_bytes : bool = False ) -> str | bytes :
217217 """Encode to JSON, optionally returning bytes for optimal performance.
218218
219219 Args:
@@ -226,7 +226,7 @@ def encode_json(data: Any, *, as_bytes: bool = False) -> Union[str, bytes]:
226226 return get_default_serializer ().encode (data , as_bytes = as_bytes )
227227
228228
229- def decode_json (data : Union [ str , bytes ] , * , decode_bytes : bool = True ) -> Any :
229+ def decode_json (data : str | bytes , * , decode_bytes : bool = True ) -> Any :
230230 """Decode from JSON string or bytes efficiently.
231231
232232 Args:
0 commit comments