Repository navigation
chore: add type annotations to vmware imc config_nic - #7107
Conversation
There was a problem hiding this comment.
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.
|
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. |
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
left a comment
There was a problem hiding this comment.
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
303f8ed to
dbf8fe1
Compare
blackboxsw
left a comment
There was a problem hiding this comment.
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.
Proposed Commit Message
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:
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_dhcp4keepsDict[str, Any]because the dict holdsboth bools and a nested dict.
One interaction worth noting: annotating
generate()meansethernets_dictis now typed intest_vmware_config_file.py, whichsurfaced a pre-existing shadowing (the loop reused
config, alreadybound to the
Configinstance) and Optional.get("match").get(...)chains in the two net-config test loops. Both loops now use
nic_configand{}defaults, so the test module passes mypy withcheck_untyped_defsthe moment its own override is removed. Theidentical test change is also applied to #7098, so the two PRs merge
cleanly in either order.
Test Steps
tox -e py3andtox -e check_formatto be confirmed by CI.(Developed on Windows where the full tox suite cannot run.)
Merge type