Skip to content

Commit 091e124

Browse files
SaiShashank12claude
andcommitted
Fix Python 3.9 compatibility by using Union/Optional instead of | syntax
Replace Python 3.10+ union syntax (X | Y) with Union[X, Y] and Optional[X] to ensure compatibility with Python 3.9 used in PreCommit tests. Changes: - Add Union import from typing - Replace X | None with Optional[X] - Replace X | Y with Union[X, Y] - Replace dict[str, Any] with Dict[str, Any] 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 8696501 commit 091e124

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

sdks/python/apache_beam/ml/inference/trt_handler_numpy_compact.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from typing import Optional
2525
from typing import Sequence
2626
from typing import Tuple
27+
from typing import Union
2728

2829
import numpy as np
2930

@@ -179,8 +180,8 @@ def _d(v: int, default: int) -> int:
179180
# ---------------------------------------------------------------------
180181
# Shape & batch helpers
181182
# ---------------------------------------------------------------------
182-
def _resolve_output_shape(shape: Sequence[int] | None,
183-
batch_size: int) -> Tuple[int, ...] | None:
183+
def _resolve_output_shape(shape: Optional[Sequence[int]],
184+
batch_size: int) -> Optional[Tuple[int, ...]]:
184185
"""Replace a leading -1 (batch) with batch_size; any other -1 is an error."""
185186
if shape is None:
186187
return None
@@ -192,7 +193,7 @@ def _resolve_output_shape(shape: Sequence[int] | None,
192193
return tuple(shp)
193194

194195

195-
def _to_contiguous_batch(x: Sequence[np.ndarray] | np.ndarray) -> np.ndarray:
196+
def _to_contiguous_batch(x: Union[Sequence[np.ndarray], np.ndarray]) -> np.ndarray:
196197
"""
197198
Accept either an ndarray (already a batch) or a list of ndarrays (concat on axis 0).
198199
This avoids accidental rank-5 shapes from upstream batching.
@@ -368,9 +369,9 @@ def ensure_buffers(
368369
# Inference function (TRT 10)
369370
# ---------------------------------------------------------------------
370371
def _trt10_inference_fn(
371-
batch: Sequence[np.ndarray] | np.ndarray,
372+
batch: Union[Sequence[np.ndarray], np.ndarray],
372373
engine_obj: TensorRTEngine,
373-
inference_args: Optional[dict[str, Any]] = None,
374+
inference_args: Optional[Dict[str, Any]] = None,
374375
) -> Iterable[PredictionResult]:
375376
"""Default inference fn using TensorRT 10 Tensor API."""
376377
from cuda import cuda
@@ -511,13 +512,13 @@ def load_model(self) -> TensorRTEngine:
511512

512513
def run_inference(
513514
self,
514-
batch: Sequence[np.ndarray] | np.ndarray,
515+
batch: Union[Sequence[np.ndarray], np.ndarray],
515516
model: TensorRTEngine,
516-
inference_args: Optional[dict[str, Any]] = None,
517+
inference_args: Optional[Dict[str, Any]] = None,
517518
) -> Iterable[PredictionResult]:
518519
return self.inference_fn(batch, model, inference_args)
519520

520-
def get_num_bytes(self, batch: Sequence[np.ndarray] | np.ndarray) -> int:
521+
def get_num_bytes(self, batch: Union[Sequence[np.ndarray], np.ndarray]) -> int:
521522
if isinstance(batch, np.ndarray):
522523
return int(batch.nbytes)
523524
if isinstance(batch, (list, tuple)) and all(isinstance(a, np.ndarray)

0 commit comments

Comments
 (0)