44import re
55from collections .abc import Sequence
66from pathlib import Path
7- from typing import Annotated , Any , Literal , Optional , Union , get_args
7+ from typing import Annotated , Any , Literal , get_args
88from uuid import uuid4
99
1010import numpy as np
4545 "complex128" ,
4646]
4747EllipsisType = type (Ellipsis )
48- ArrayLike = Union [ np .ndarray , np .number , np .bool_ ]
49- ShapeType = Union [ tuple [Optional [ int ] , ...], EllipsisType ]
48+ ArrayLike = np .ndarray | np .number | np .bool_
49+ ShapeType = tuple [int | None , ...] | EllipsisType
5050
5151MAX_BINREF_BUFFER_SIZE = 100 * 1024 * 1024 # 100 MB
5252
@@ -94,12 +94,12 @@ class EncodedArrayModel(BaseModel):
9494 object_type : Literal ["array" ]
9595 shape : tuple [PositiveInt , ...]
9696 dtype : AllowedDtypes
97- data : Union [ BinrefArrayData , Base64ArrayData , JsonArrayData ]
97+ data : BinrefArrayData | Base64ArrayData | JsonArrayData
9898 model_config = ConfigDict (extra = "forbid" )
9999
100100
101101def get_array_model (
102- expected_shape : ShapeType , expected_dtype : Optional [ str ] , flags : Sequence [str ]
102+ expected_shape : ShapeType , expected_dtype : str | None , flags : Sequence [str ]
103103) -> type [EncodedArrayModel ]:
104104 """Create a Pydantic model for an encoded array that does validation on the given expected shape and dtype."""
105105 if expected_dtype is None :
@@ -177,7 +177,7 @@ def get_array_model(
177177 ),
178178 # Choose the appropriate data structure based on the encoding
179179 "data" : (
180- Union [ BinrefArrayData , Base64ArrayData , JsonArrayData ] ,
180+ BinrefArrayData | Base64ArrayData | JsonArrayData ,
181181 Field (discriminator = "encoding" ),
182182 ),
183183 "model_config" : (ConfigDict , config ),
@@ -203,12 +203,12 @@ def get_array_model(
203203
204204
205205def _dump_binref_arraydict (
206- arr : Union [ np .ndarray , np .number , np .bool_ ] ,
207- base_dir : Union [ Path , str ] ,
208- subdir : Optional [ Union [ Path , str ]] ,
206+ arr : np .ndarray | np .number | np .bool_ ,
207+ base_dir : Path | str ,
208+ subdir : Path | str | None ,
209209 current_binref_uuid : str ,
210210 max_file_size : int = MAX_BINREF_BUFFER_SIZE ,
211- ) -> tuple [dict [str , Union [ str , dict [str , str ] ]], str ]:
211+ ) -> tuple [dict [str , str | dict [str , str ]], str ]:
212212 """Dump array to json+binref encoded array dict.
213213
214214 Writes a .bin file and returns json encoded data.
@@ -243,8 +243,8 @@ def _dump_binref_arraydict(
243243
244244
245245def _dump_base64_arraydict (
246- arr : Union [ np .ndarray , np .number , np .bool_ ] ,
247- ) -> dict [str , Union [ str , dict [str , str ] ]]:
246+ arr : np .ndarray | np .number | np .bool_ ,
247+ ) -> dict [str , str | dict [str , str ]]:
248248 """Dump array to json+base64 encoded array dict."""
249249 data = {
250250 "buffer" : pybase64 .b64encode (arr .tobytes ()).decode (),
@@ -260,8 +260,8 @@ def _dump_base64_arraydict(
260260
261261
262262def _dump_json_arraydict (
263- arr : Union [ np .ndarray , np .number , np .bool_ ] ,
264- ) -> dict [str , Union [ str , dict [str , str ] ]]:
263+ arr : np .ndarray | np .number | np .bool_ ,
264+ ) -> dict [str , str | dict [str , str ]]:
265265 """Dump array to json encoded array dict."""
266266 data = {
267267 "buffer" : arr .tolist (),
@@ -282,7 +282,7 @@ def _load_base64_arraydict(val: dict) -> np.ndarray:
282282 return np .frombuffer (buffer , dtype = val ["dtype" ]).reshape (val ["shape" ])
283283
284284
285- def _load_binref_arraydict (val : dict , base_dir : Union [ str , Path , None ] ) -> np .ndarray :
285+ def _load_binref_arraydict (val : dict , base_dir : str | Path | None ) -> np .ndarray :
286286 """Load array from json+binref encoded array dict."""
287287 path_match = re .match (r"^(?P<path>.+?)(\:(?P<offset>\d+))?$" , val ["data" ]["buffer" ])
288288 if not path_match :
@@ -316,7 +316,7 @@ def _load_binref_arraydict(val: dict, base_dir: Union[str, Path, None]) -> np.nd
316316
317317
318318def _coerce_shape_dtype (
319- arr : ArrayLike , expected_shape : ShapeType , expected_dtype : Optional [ str ]
319+ arr : ArrayLike , expected_shape : ShapeType , expected_dtype : str | None
320320) -> ArrayLike :
321321 """Coerce the shape and dtype of the passed array to the expected values."""
322322 if expected_shape is Ellipsis :
@@ -363,7 +363,7 @@ def _coerce_shape_dtype(
363363
364364
365365def python_to_array (
366- val : Any , expected_shape : ShapeType , expected_dtype : Optional [ str ]
366+ val : Any , expected_shape : ShapeType , expected_dtype : str | None
367367) -> ArrayLike :
368368 """Convert a Python object to a NumPy array."""
369369 val = np .asarray (val , order = "C" )
@@ -380,7 +380,7 @@ def decode_array(
380380 val : EncodedArrayModel ,
381381 info : ValidationInfo ,
382382 expected_shape : ShapeType ,
383- expected_dtype : Optional [ str ] ,
383+ expected_dtype : str | None ,
384384) -> ArrayLike :
385385 """Decode an EncodedArrayModel to a NumPy array."""
386386 from tesseract_core .runtime .config import get_config
@@ -422,8 +422,8 @@ def decode_array(
422422
423423
424424def encode_array (
425- arr : ArrayLike , info : Any , expected_shape : ShapeType , expected_dtype : Optional [ str ]
426- ) -> Union [ EncodedArrayModel , ArrayLike ] :
425+ arr : ArrayLike , info : Any , expected_shape : ShapeType , expected_dtype : str | None
426+ ) -> EncodedArrayModel | ArrayLike :
427427 """Encode a NumPy array as an EncodedArrayModel."""
428428 from tesseract_core .runtime .config import get_config
429429
0 commit comments