Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 26, 2026

Python code produces numerous type errors in PyLance "standard" mode. This PR reduces pyright errors from 144 to 103 (28% reduction).

Type annotation fixes

  • Config._config: Dict[str, str]Dict[str, Any] (stores List[str] and Optional[str] values)
  • ShotResult.events: List[Output]List[Any] (contains Output, StateDump, and str)
  • noisy_simulator/_noisy_simulator.pyi: Raw strings for LaTeX docstrings (\sum, \otimes)
  • **kwargs signatures: Optional[Dict[str, Any]]Any per Python typing spec

Null safety

  • Guard StateDump construction with null check on output.state_dump() which returns Optional[StateDumpData]
  • Optional[float] for time attributes in qiskit job classes with assertions documenting invariants

Type narrowing for callable pattern

# Before: pyright can't narrow Union[str, Callable] in else branch
if isinstance(entry_expr, Callable) and hasattr(entry_expr, "__global_callable"):
    ...
else:
    interpreter.run(entry_expr)  # Error: entry_expr still includes Callable

# After: explicit isinstance check enables narrowing
elif isinstance(entry_expr, str):
    interpreter.run(entry_expr)  # OK

Other fixes

  • EstimatorResult._is_succeeded: instance method → @staticmethod (called as EstimatorResult._is_succeeded(data))
  • Pass all required args to interpreter methods (qir, estimate, logical_counts)
  • cast() for _coerce_estimator_params return type

Remaining issues

~50 errors remain, primarily reportMissingImports for external deps (pyqir, qiskit, IPython) without type stubs.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Dev] Our Python code doesn't typecheck correctly</issue_title>
<issue_description>Describe the bug

A lot of our Python code have type checking errors when we set PyLance type checking mode to "standard".

To Reproduce

Steps to reproduce the behavior:

  1. Set PyLance type checking mode to "standard"
  2. Open one of our Python files such as source\pip\qsharp\_qsharp.py
  3. You will see multiple warnings and errors coming from PyLance

Expected behavior

Our Python code should type check as much as possible.

#2884 is an example of a PR fixing one of these PyLance typing issues.

Screenshots

Image</issue_description>

<agent_instructions>Go through our Python files and make them type check as much as possible. You won't be able to make it work perfectly due to some dependencies not type-checking properly, such as pyqir, or due to some limitations of Python 3.10 which is our minimum supported version.

You can use PR #2884 as a reference.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits January 26, 2026 22:57
Co-authored-by: orpuente-MS <156957451+orpuente-MS@users.noreply.github.com>
Co-authored-by: orpuente-MS <156957451+orpuente-MS@users.noreply.github.com>
Co-authored-by: orpuente-MS <156957451+orpuente-MS@users.noreply.github.com>
Co-authored-by: orpuente-MS <156957451+orpuente-MS@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix type checking errors in Python code Fix Python type checking errors for PyLance standard mode Jan 26, 2026
Copilot AI requested a review from orpuente-MS January 26, 2026 23:12
@orpuente-MS orpuente-MS marked this pull request as ready for review January 30, 2026 18:20
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.

[Dev] Our Python code doesn't typecheck correctly

2 participants