Skip to content

Example notebook type fixes - #8834

Open
Jens Hedegaard Nielsen (jenshnielsen) wants to merge 12 commits into
microsoft:mainfrom
jenshnielsen:example_notebook_type_fixes
Open

Jens Hedegaard Nielsen (jenshnielsen) wants to merge 12 commits into
microsoft:mainfrom
jenshnielsen:example_notebook_type_fixes

Conversation

@jenshnielsen

Copy link
Copy Markdown
Collaborator

Fix type checking issues in notebooks discovered by checking with TY

The test cells imported the Weinschel8320 driver from qcodes, replacing
the class of the same name that the notebook defines, which ty reports
as an invalid assignment. Remove those imports so the tests use the
driver from the notebook, and annotate the fixtures and tests.
test_init_v2 requested a driver fixture that does not exist, so request
weinschel_driver_2 instead. Also clear the stale output of the first
cell.
set_netcdf_location exists on DataSetInMem but not on the protocol that
load_by_run_spec returns, so the exporting notebook narrows the dataset
before calling it. The walkthrough notebook loses the two cells reading
DataSet.started and DataSet.parameters, which are likewise not part of
the protocol.
The parameters in these examples read the point count off the
instrument they belong to, which only the concrete instrument class
declares. Override root_instrument to return that class, so that the
examples state what they already assume.
Overriding set_raw with a differently named argument is an invalid
override, since callers are free to pass value as a keyword.
qc.config.current_config is optional, so the notebook has to establish
that a configuration is loaded before reading values out of it.
dond returns a single dataset rather than a tuple of them unless
squeeze=False is passed, so notebooks that unpack the result could not
be type checked. Pass squeeze=False and index into the result instead.
dond returns a single dataset rather than a tuple of them unless
squeeze=False is passed, so unpacking the result in the AMI430 example
does not type check. Pass squeeze=False and take the first dataset.
by_kind and by_channel are keyed by ModuleKind and ChNr. Those are a
StrEnum and an IntEnum, so a plain string or int is the same key at
runtime, but the dicts are typed as taking the enums.

Use constants.ModuleKind.SMU for the by_kind lookup, which is what the
markdown just above it points at. The by_channel cell deliberately shows
both the enum and the plain int and asserts they select the same module,
so keep that and note that only the enum matches the type of the dict.
instrument.parameters is a dict of ParameterBase, which does not carry a
label. Parameter and ArrayParameter do, but MultiParameter has labels
instead, so the listing would raise for an instrument holding one.

Read it with getattr and a default, and say why in the notebook.
The sample count and timer of the Keysight 344xxA are annotated as
float | str, since they can also be set to the MIN, MAX and DEF
keywords. Their values are always parsed as numbers when read, so
convert them to int and float before computing the time of each
sample, which makes the arithmetic type check.
The file is named with a placeholder, to be renamed to the number of the
pull request once it is opened.
"# Top-level parameters\n",
"for name in sorted(awg1.parameters):\n",
" print(name, \": \", awg1.parameters[name].label)\n",
" print(name, \": \", getattr(awg1.parameters[name], \"label\", \"\"))\n",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

narrow to parameter instrad of using getattr hack

@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.55%. Comparing base (e4b6800) to head (7ef6c37).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8834   +/-   ##
=======================================
  Coverage   78.55%   78.55%           
=======================================
  Files         311      311           
  Lines       33162    33162           
=======================================
  Hits        26049    26049           
  Misses       7113     7113           

☔ 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.

This branch has not been deployed

No deployments
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.

1 participant