Skip to content

chore: add type annotations to vmware imc config_nic - #7107

Merged
blackboxsw merged 2 commits into
canonical:mainfrom
anujsingh-cse:fix/untyped-defs-config-nic-5445
Oct 8, 2026
Merged

blackboxsw merged 2 commits into
canonical:mainfrom
anujsingh-cse:fix/untyped-defs-config-nic-5445

Conversation

@anujsingh-cse

@anujsingh-cse anujsingh-cse commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Proposed Commit Message

chore: add type annotations to vmware imc config_nic

Removing cloudinit.sources.helpers.vmware.imc.config_nic from the
mypy override list in pyproject.toml surfaces two errors, and review
feedback pushed the module to full annotation coverage.

gen_dhcp4 seeds an empty dict that later holds both a bool
({"dhcp4": True}) and a nested dict ({"dhcp4-overrides": {...}}), so
mypy cannot infer a single value type once check_untyped_defs is
enabled. Annotate it as Dict[str, Any].

clear_dhcp reached IscDhclient through the cloudinit.net package
attribute net.dhcp, which mypy cannot resolve without an explicit
submodule import. Import dhcp from cloudinit.net directly, matching
how cloudinit.sources.DataSourceCloudStack already does it, and drop
the now-unused net import.

NicConfigurator is now fully annotated: constructor parameters and
instance attributes, the gen_* helpers, generate(), configure(), and
find_devices(). Empty collections that mypy cannot infer get explicit
annotations.

The new generate() return type exposes a pre-existing shadowing in
the vmware config tests: the loop variable `config` collided with the
Config instance once ethernets_dict became typed. Rename the loop
variable to nic_config and give the Optional .get("match") /
.get("dhcp4-overrides") chains a {} default, so the test module stays
clean once #7098 removes its mypy override.

Refs GH-5445

Additional Context

Refs GH-5445. Claimed in #5445 (comment).

Initial scope (commit 2c04314) removed the module from the override
list and fixed the two surfaced errors:

config_nic.py:129: error: Dict entry 0 has incompatible type
                   "str": "dict[str, bool]"; expected "str": "bool"  [dict-item]
config_nic.py:243: error: Module has no attribute "dhcp"  [attr-defined]

Follow-up (commit 303f8ed), after review feedback asking for more
annotation coverage: all NicConfigurator methods and instance
attributes are now annotated (nics: List[Nic], mac2Name: Dict[str, str], -> Optional[Nic], -> Dict[str, ...] per helper,
etc.). The gen_* return types record the actual shapes the tests
assert on; gen_dhcp4 keeps Dict[str, Any] because the dict holds
both bools and a nested dict.

One interaction worth noting: annotating generate() means
ethernets_dict is now typed in test_vmware_config_file.py, which
surfaced a pre-existing shadowing (the loop reused config, already
bound to the Config instance) and Optional .get("match").get(...)
chains in the two net-config test loops. Both loops now use
nic_config and {} defaults, so the test module passes mypy with
check_untyped_defs the moment its own override is removed. The
identical test change is also applied to #7098, so the two PRs merge
cleanly in either order.

Test Steps

$ python -m mypy cloudinit/ tests/ tools/
(0 errors in the target module with the override removed; with the
 test-file override also removed locally, only the 4 union-attr that
 #7098 fixes remain)

$ pytest tests/unittests/sources/vmware/test_vmware_config_file.py -q
22 passed

$ ruff check / black --check / isort --check-only / pylint
All green

tox -e py3 and tox -e check_format to be confirmed by CI.
(Developed on Windows where the full tox suite cannot run.)

Merge type

  • Squash merge using "Proposed Commit Message"
  • Rebase and merge unique commits. Requires commit messages per-commit each referencing the pull request number (#<PR_NUM>)

@nryanl nryanl left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for contributing to cloud-init!

With #5445, Our aim is to improve type coverage in each disabled module. You successfully resolved errors from tox -e mypy, but there are still many opportunities for coverage in config_nic.py, so I'd like to see more annotations before moving forward with this PR.

@anujsingh-cse

Copy link
Copy Markdown
Contributor Author

Apps for more coverage now pushed in 303f8ed: NicConfigurator is fully annotated (constructor parameters, instance attributes, all gen_* helpers, generate(), configure(), find_devices()). Type shapes come from what the tests assert on.

The new generate() return type also surfaced a pre-existing shadowing in the vmware config tests (the loop reused \config, already bound to the Config instance) and Optional .get() chains in the two net-config loops, so the test module now passes mypy with check_untyped_defs the moment its own override is removed. The identical test change is applied to #7098 as well, so the two PRs merge cleanly in either order.

@anujsingh-cse
anujsingh-cse requested a review from nryanl October 2, 2026 08:52
anujsingh-cse added a commit to anujsingh-cse/cloud-init that referenced this pull request Oct 2, 2026
The two net-config test loops reused `config`, already bound to the
Config instance above; once the test module is checked with check
_untyped_defs, mypy reports the reassignment of a typed fixture from
canonical#7107's annotated generate() as an incompatible assignment. Rename the
loop variable to nic_config, and give the Optional .get("match") /
.get("dhcp4-overrides") chains a {} default, so the module passes
whether canonical#7107 lands before or after this PR.

Refs canonicalGH-5445

@nryanl nryanl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for those! Followup PRs on other files in this directory are welcome. A few others use Nic (such as nic.py) and need annotations.

Removing cloudinit.sources.helpers.vmware.imc.config_nic from the mypy
override list in pyproject.toml surfaces two errors.

gen_dhcp4 seeds an empty dict that later holds both a bool
({"dhcp4": True}) and a nested dict ({"dhcp4-overrides": {...}}), so
mypy cannot infer a single value type once check_untyped_defs is
enabled. Annotate it as Dict[str, Any].

clear_dhcp reached IscDhclient through the cloudinit.net package
attribute net.dhcp, which mypy cannot resolve without an explicit
submodule import. Import dhcp from cloudinit.net directly, matching
how cloudinit.sources.DataSourceCloudStack already does it, and drop
the now-unused net import.

No signatures change, so there is no effect on callers.

Refs canonicalGH-5445
Follows review feedback: cover the module's public surface rather
than only the two errors check_untyped_defs surfaced.

NicConfigurator is now fully annotated: constructor parameters and
instance attributes, gen_* helpers, generate(), configure(), and
find_devices(). gen_dhcp4 keeps its Dict[str, Any] because the dict
holds both bools and a nested dict; empty collections that mypy
cannot infer get explicit annotations.

The new generate() return type exposes a pre-existing shadowing in
the vmware config tests: the loop variable `config` collided with
the Config instance once ethernets_dict became typed. Rename the
loop variable to nic_config and give the Optional .get("match") /
.get("dhcp4-overrides") chains a {} default, so the test module
stays clean once canonical#7098 removes its mypy override.

Refs canonicalGH-5445
@blackboxsw
blackboxsw force-pushed the fix/untyped-defs-config-nic-5445 branch from 303f8ed to dbf8fe1 Compare October 8, 2026 13:41

@blackboxsw blackboxsw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you @anujsingh-cse for the contribution.

I've redacted a bit of the proposed commit message as it is stating the process of this fix more than describing the substantive changes beyond the type hints.

We appreciate the help.

@blackboxsw
blackboxsw merged commit 502fbed into canonical:main Oct 8, 2026
18 checks passed
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.

3 participants