Update Python API to avoid private types in public signatures#3278
Draft
ScottCarda-MS wants to merge 9 commits into
Draft
Update Python API to avoid private types in public signatures#3278ScottCarda-MS wants to merge 9 commits into
ScottCarda-MS wants to merge 9 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix private API leakage in the
qdkPython packageProblem
Several internal types from private modules (e.g.
qdk._native,qdk._types,qdk.qre._application) were leaking through public function signatures—return types, parameter types, and TypeVar constraints. This caused:qdk._native.Outputas a private-module access when users annotate variables holding values from public functions.Solution
1.
qdk.internalandqdk.qre.internalmodulesNew modules that re-export internal types used in public signatures. These are explicitly labeled as unsupported—they exist only so type checkers can resolve annotations and doc generators can produce links. Users are warned not to depend on them.
Each module uses a
TYPE_CHECKINGguard to provide minimal Protocol definitions at type-check time (limiting the visible surface) while re-exporting the real classes at runtime (preserving identity checks and zero-cost pass-through).qdk.internalexposes:StateDumpData,Circuit,Closure,GlobalCallable,Output,Config,QirInputData,Zone,ZoneTypeqdk.qre.internalexposes:ApplicationContext,DataclassProtocol,Instruction,InstructionSourceNodeReference2. Annotation fixes for ISA enumeration nodes
Three
ISAQuerymethods returned private subtypes in their annotations:__add__→_SumNode(nowISAQuery)__mul__→_ProductNode(nowISAQuery)bind()→_BindingNode(nowISAQuery)All three subtypes inherit from
ISAQueryand users only interact through theISAQueryinterface. Widening the return annotation to the public base class eliminates the leakage with no change in runtime behavior.3.
_Context→ApplicationContextrenameThe private
_Contextclass inqdk.qre._applicationappeared in two public signatures:Application.context()(return type) andTraceQuery.enumerate()(parameter type). Users couldn't annotate variables holding the context. Renamed toApplicationContext(non-private name) and routed throughqdk.qre.internal.4.
check_api_surface.pylint scriptNew CI-ready lint that detects private-symbol leakage in the public API surface:
__all__exports_-prefixed) modules__all__)--jsonfor machine-readable outputbuild.py: runs automatically after qdk build when--checkis enabled (the default); skipped with--no-checkFiles changed
source/qdk_package/check_api_surface.pybuild.py--check)source/qdk_package/qdk/internal.pyqdk.internalre-export modulesource/qdk_package/qdk/qre/internal.pyqdk.qre.internalre-export modulesource/qdk_package/qdk/qre/__init__.pyTraceParametersto__all__source/qdk_package/qdk/qre/_application.py_Context→ApplicationContextsource/qdk_package/qdk/qre/_instruction.pybind()return type toISAQuerysource/qdk_package/qdk/qre/_isa_enumeration.py__add__,__mul__,bindreturn types toISAQuerysource/qdk_package/qdk/qre/_trace.py_Context→ApplicationContextin type hintssource/qdk_package/qdk/_context.pynoiseparameter docstringsource/qdk_package/qdk/_interpreter.pynoiseparameter docstringValidation
check_api_surface.pyreports 0 violations against the current codebase.save_events).