Fix runtime-unresolvable type annotations in Session and InferenceSession#27802
Fix runtime-unresolvable type annotations in Session and InferenceSession#27802Rishi-Dave wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…sion (microsoft#17676) Replace `onnxruntime.X` type annotations with `C.X` (where C is the already-imported `_pybind_state` module) so that `typing.get_type_hints()` can resolve them at runtime. The previous annotations referenced `onnxruntime`, which was only imported under `TYPE_CHECKING` and therefore unavailable in the module's globals at runtime. Also fixes `onnxruntime.MemoryInfo` → `C.OrtMemoryInfo` in the return type of `get_input_memory_infos()` and `get_output_memory_infos()`, since `MemoryInfo` does not exist in the Python API — the correct class is `OrtMemoryInfo`.
There was a problem hiding this comment.
Pull request overview
This PR fixes Python runtime type introspection issues in onnxruntime_inference_collection.py by replacing annotations that referenced onnxruntime.* (unavailable at runtime due to TYPE_CHECKING + postponed evaluation) with _pybind_state (C.*) types that are always importable, and by correcting an invalid MemoryInfo annotation.
Changes:
- Replace
onnxruntime.*annotations withC.*forSession/InferenceSession/ModelCompilerAPIs sotyping.get_type_hints()can resolve them at runtime. - Fix
onnxruntime.MemoryInfo→C.OrtMemoryInfoin memory info getters. - Remove the unused
import onnxruntimefrom theTYPE_CHECKINGblock.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if typing.TYPE_CHECKING: | ||
| import numpy as np | ||
| import numpy.typing as npt | ||
|
|
There was a problem hiding this comment.
The PR description/title suggest runtime-resolvable annotations via typing.get_type_hints(), but this module still uses np/npt in many annotations while importing NumPy only under TYPE_CHECKING. Calling get_type_hints() on methods like Session.run, OrtValue.__init__, or SparseTensor APIs will still raise NameError unless NumPy is available in module globals. Consider either importing NumPy at runtime, avoiding np/npt in runtime-evaluated annotations (e.g., fall back to Any), or clarifying the scope to just the onnxruntime.* references being fixed here.
Summary
onnxruntime.Xtype annotations withC.X(_pybind_state) inonnxruntime_inference_collection.pyso they resolve at runtime viatyping.get_type_hints()onnxruntime.MemoryInfoannotation →C.OrtMemoryInfo(the actual exported class name)import onnxruntimefromTYPE_CHECKINGblockMotivation
Fixes #17676
The
SessionandInferenceSessionclasses useonnxruntime.SessionOptions,onnxruntime.NodeArg, etc. in type annotations. However,import onnxruntimeis guarded undertyping.TYPE_CHECKING, which means it is only available to static type checkers — not at runtime. Combined withfrom __future__ import annotations(PEP 563), callingtyping.get_type_hints()on any of these methods raisesNameError: name 'onnxruntime' is not defined.This breaks any tool that relies on runtime type introspection (e.g.,
help(), Sphinx autodoc, pydantic, FastAPI).The
_pybind_statemodule is already imported unconditionally asCon line 16 and used extensively throughout the file for runtime references (e.g.,C.GraphOptimizationLevelon line 749). Switching annotations to useC.Xis consistent with the existing file style and makes them resolvable both statically and at runtime.Additionally,
onnxruntime.MemoryInfowas used in annotations forget_input_memory_infos()andget_output_memory_infos(), butMemoryInfodoes not exist in the Python API — the pybind11-exported class isOrtMemoryInfo. 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)import onnxruntimefromTYPE_CHECKINGblockTest Plan
C.Xtype names exist in the pybind11 bindings (onnxruntime_pybind_state.cc)ruff check— all checks passedruff format --check— already formattedlintrunner— no lint issues