Skip to content

Deprecate legacy top level exports#8294

Merged
jenshnielsen merged 8 commits into
microsoft:mainfrom
jenshnielsen:avoid_top_level_export
Jul 16, 2026
Merged

Deprecate legacy top level exports#8294
jenshnielsen merged 8 commits into
microsoft:mainfrom
jenshnielsen:avoid_top_level_export

Conversation

@jenshnielsen

@jenshnielsen jenshnielsen commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator
  • Make legacy imports in qcodes and qcodes.instrument lazy (Does not run unless used)
  • Deprecate the same imports and point to the official api end point
  • Move at exit registration of close_all to qcodes.instrument to avoid importing qcodes.instrument by default
  • configure close_all to not log by default and specifically during at exit. This eliminates an error when using newest open telemetry

The main benefits of the import changes are significantly faster import of qcodes top level module + a much smaller set of interconnected modules during type checking

@jenshnielsen
jenshnielsen requested a review from a team as a code owner July 14, 2026 17:09
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.14286% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.11%. Comparing base (4210a4c) to head (96db6db).

Files with missing lines Patch % Lines
src/qcodes/instrument/instrument.py 57.14% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8294      +/-   ##
==========================================
- Coverage   71.12%   71.11%   -0.01%     
==========================================
  Files         305      305              
  Lines       32001    32004       +3     
==========================================
  Hits        22760    22760              
- Misses       9241     9244       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jenshnielsen
jenshnielsen enabled auto-merge July 14, 2026 22:33
jenshnielsen and others added 7 commits July 16, 2026 09:44
The eager imports of the dataset, instrument, parameters, monitor and
station submodules in ``qcodes/__init__.py`` glued 83 modules into a
single import cycle. Combined with a generic ``ParameterBase`` whose
TypeVar default forward-references ``InstrumentBase`` and the
``ParamMeasT`` union alias used in a ``TypedDict``, this triggered an
internal error in mypy 2.2 ("Must not defer during final iteration").

Expose the discouraged top-level shorthands (``qcodes.Parameter``,
``qcodes.Measurement``, etc.) lazily via a module level ``__getattr__``
instead. This breaks the cycle so ``mypy src`` succeeds again. The
shorthands remain available at runtime but are intentionally no longer
statically typed; import from the respective submodules for type info.

The ``atexit`` handler that closes all instruments is moved into
``qcodes.instrument`` so importing the top level package no longer
eagerly imports the instrument submodule.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 05b20136-29a2-4bfe-834c-894d3efa9d4c
The parameter classes (Parameter, ManualParameter, DelegateParameter, etc.)
were re-exported from qcodes.instrument for backwards compatibility. These now
live in qcodes.parameters. Access them via a module level __getattr__ that emits
a QCoDeSDeprecationWarning, and update the Legacy_Parameters example to import
ArrayParameter from qcodes.parameters.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 05b20136-29a2-4bfe-834c-894d3efa9d4c
Expose qcodes.validators via the module level __getattr__ instead of importing
it eagerly on 'import qcodes', keeping the top level import cheap. Also tidy the
now-stale module comment and update the newsfragment.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 05b20136-29a2-4bfe-834c-894d3efa9d4c
Guard the module level __getattr__ in qcodes and qcodes.instrument behind
'if not TYPE_CHECKING' so the lazily provided short hands and deprecated
parameter re-exports are reported as unknown attributes by static type checkers
instead of silently resolving to Any. Runtime behaviour is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 05b20136-29a2-4bfe-834c-894d3efa9d4c
Accessing the short hand aliases (qcodes.Parameter, qcodes.Measurement,
qcodes.Monitor, qcodes.Station, etc.) from the top level qcodes namespace now
emits a QCoDeSDeprecationWarning; import them from their respective submodules
instead. The submodules these names live in (dataset, instrument, parameters,
monitor, station) are added to the lazily imported submodules so that
qcodes.dataset and friends keep working once the deprecated short hands are
removed. validators is unchanged and not deprecated.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 05b20136-29a2-4bfe-834c-894d3efa9d4c
Verify that the deprecated short hand aliases on the top level qcodes namespace
and the parameter classes re-exported from qcodes.instrument still resolve to
the correct objects while emitting a QCoDeSDeprecationWarning. The direct
attribute accesses are also intentionally type errors, silenced with
'# type: ignore[attr-defined]' pragmas to document that behaviour. Also check
that non-deprecated submodule access is warning free and that unknown attributes
raise AttributeError.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 05b20136-29a2-4bfe-834c-894d3efa9d4c
The deprecation of the top level qcodes short hands and the qcodes.instrument
parameter re-exports is a change to the public API, so classify the
newsfragment as breaking rather than improved.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 05b20136-29a2-4bfe-834c-894d3efa9d4c
@jenshnielsen
jenshnielsen force-pushed the avoid_top_level_export branch from 9c03747 to e72b589 Compare July 16, 2026 16:49
@jenshnielsen
jenshnielsen force-pushed the avoid_top_level_export branch from e72b589 to 96db6db Compare July 16, 2026 16:54
@jenshnielsen
jenshnielsen added this pull request to the merge queue Jul 16, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jul 16, 2026
@jenshnielsen
jenshnielsen added this pull request to the merge queue Jul 16, 2026
Merged via the queue into microsoft:main with commit 5737689 Jul 16, 2026
16 of 17 checks passed
@jenshnielsen
jenshnielsen deleted the avoid_top_level_export branch July 16, 2026 20:39
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.

2 participants