Skip to content

Fix runtime-unresolvable type annotations in Session and InferenceSession#27802

Open
Rishi-Dave wants to merge 1 commit intomicrosoft:mainfrom
Rishi-Dave:rishidave/fix/inference-session-typing
Open

Fix runtime-unresolvable type annotations in Session and InferenceSession#27802
Rishi-Dave wants to merge 1 commit intomicrosoft:mainfrom
Rishi-Dave:rishidave/fix/inference-session-typing

Conversation

@Rishi-Dave
Copy link
Contributor

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.SessionOptionsC.SessionOptions (3 locations: Session.get_session_options, InferenceSession.__init__, ModelCompiler.__init__)
    • onnxruntime.NodeArgC.NodeArg (3 locations: get_inputs, get_outputs, get_overridable_initializers)
    • onnxruntime.ModelMetadataC.ModelMetadata (get_modelmeta)
    • onnxruntime.MemoryInfoC.OrtMemoryInfo (get_input_memory_infos, get_output_memory_infos)
    • onnxruntime.OrtEpDeviceC.OrtEpDevice (get_input_epdevices)
    • onnxruntime.OrtEpAssignedSubgraphC.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

…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`.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with C.* for Session/InferenceSession/ModelCompiler APIs so typing.get_type_hints() can resolve them at runtime.
  • Fix onnxruntime.MemoryInfoC.OrtMemoryInfo in memory info getters.
  • Remove the unused import onnxruntime from the TYPE_CHECKING block.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 18 to 21
if typing.TYPE_CHECKING:
import numpy as np
import numpy.typing as npt

Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

onnxruntime InferenceSession sess_options typing

2 participants