onnxruntime
8d24011e - Fix runtime-unresolvable type annotations in Session and InferenceSession (#27802)

Commit
97 days ago
Fix runtime-unresolvable type annotations in Session and InferenceSession (#27802) ## Summary - Replace `onnxruntime.X` type annotations with `C.X` (`_pybind_state`) in `onnxruntime_inference_collection.py` so they resolve at runtime via `typing.get_type_hints()` - Fix incorrect `onnxruntime.MemoryInfo` annotation → `C.OrtMemoryInfo` (the actual exported class name) - Remove unused `import onnxruntime` from `TYPE_CHECKING` block ## Motivation Fixes #17676 The `Session` and `InferenceSession` classes use `onnxruntime.SessionOptions`, `onnxruntime.NodeArg`, etc. in type annotations. However, `import onnxruntime` is guarded under `typing.TYPE_CHECKING`, which means it is only available to static type checkers — not at runtime. Combined with `from __future__ import annotations` (PEP 563), calling `typing.get_type_hints()` on any of these methods raises `NameError: name 'onnxruntime' is not defined`. This breaks any tool that relies on runtime type introspection (e.g., `help()`, Sphinx autodoc, pydantic, FastAPI). The `_pybind_state` module is already imported unconditionally as `C` on line 16 and used extensively throughout the file for runtime references (e.g., `C.GraphOptimizationLevel` on line 749). Switching annotations to use `C.X` is consistent with the existing file style and makes them resolvable both statically and at runtime. Additionally, `onnxruntime.MemoryInfo` was used in annotations for `get_input_memory_infos()` and `get_output_memory_infos()`, but `MemoryInfo` does not exist in the Python API — the pybind11-exported class is `OrtMemoryInfo`. This was a latent bug masked by the fact that annotations were never evaluated at runtime. ## Changes - `onnxruntime/python/onnxruntime_inference_collection.py`: - `onnxruntime.SessionOptions` → `C.SessionOptions` (3 locations: `Session.get_session_options`, `InferenceSession.__init__`, `ModelCompiler.__init__`) - `onnxruntime.NodeArg` → `C.NodeArg` (3 locations: `get_inputs`, `get_outputs`, `get_overridable_initializers`) - `onnxruntime.ModelMetadata` → `C.ModelMetadata` (`get_modelmeta`) - `onnxruntime.MemoryInfo` → `C.OrtMemoryInfo` (`get_input_memory_infos`, `get_output_memory_infos`) - `onnxruntime.OrtEpDevice` → `C.OrtEpDevice` (`get_input_epdevices`) - `onnxruntime.OrtEpAssignedSubgraph` → `C.OrtEpAssignedSubgraph` (`get_provider_graph_assignment_info`) - Removed unused `import onnxruntime` from `TYPE_CHECKING` block ## Test Plan - Verified all six `C.X` type names exist in the pybind11 bindings (`onnxruntime_pybind_state.cc`) - `ruff check` — all checks passed - `ruff format --check` — already formatted - `lintrunner` — no lint issues - No functional behavior changes; only type annotation metadata is affected
Author
Parents
Loading