Skip to content

chore: add type annotations to vmware imc config_file - #7098

Open
anujsingh-cse wants to merge 3 commits into
canonical:mainfrom
anujsingh-cse:fix/untyped-defs-5445
Open

anujsingh-cse wants to merge 3 commits into
canonical:mainfrom
anujsingh-cse:fix/untyped-defs-5445

Conversation

@anujsingh-cse

Copy link
Copy Markdown
Contributor

Proposed Commit Message

chore: add type annotations to vmware imc config_file

Removing cloudinit.sources.helpers.vmware.imc.config_file and
tests.unittests.sources.vmware.test_vmware_config_file from the mypy
override list in pyproject.toml surfaces six errors.

config_file.py: the standard configparser idiom
config.optionxform = str assigns a class to a method, which mypy flags
as both method-assign and assignment; this is intentional (disables
option lowercasing) and follows the existing ignore style used in
cloudinit/config/schema.py.

test_vmware_config_file.py: two assertions chain .get("nameservers").get(...)
which mypy sees as union-attr because dict.get returns Optional. Adding
the {} default keeps runtime behavior identical when the key is present
and makes the Optional case explicit.

Refs GH-5445

Additional Context

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

Removing both modules from the override list surfaces six errors:

config_file.py:61: error: Cannot assign to a method  [method-assign]
config_file.py:61: error: Incompatible types in assignment
                (expression has type "type[str]",
                 variable has type "Callable[[str], str]")  [assignment]
test_vmware_config_file.py:164: error: Item "None" of "Any | None"
                has no attribute "get"  [union-attr]
test_vmware_config_file.py:165: error: ... [union-attr]
test_vmware_config_file.py:188: error: ... [union-attr]
test_vmware_config_file.py:189: error: ... [union-attr]
  • config.optionxform = str is the documented configparser pattern for
    preserving key case; mypy cannot express "assign the builtin str as
    the transform function" against the bound-method type, so it is
    ignored with the same comment style already used at
    cloudinit/config/schema.py:523.
  • The four test errors are .get("nameservers").get(...) chains where
    the first .get is Optional. Supplying {} as the default makes
    the chain total; when the key is present (as in every fixture used
    here) behavior is unchanged.

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

Test Steps

$ python -m mypy cloudinit/ tests/ tools/
(with override entries removed; the two target files report 0 errors)

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

$ ruff check (target files)
All checks passed!

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>)

@holmanb holmanb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, one comment.


config = configparser.ConfigParser()
config.optionxform = str
config.optionxform = str # type: ignore [method-assign, assignment]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please include a comment documenting why this is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a comment above the line explaining why the ignore is needed (the documented configparser idiom for preserving key case, which mypy cannot express against a method type) — 0a37e72.

anujsingh-cse added a commit to anujsingh-cse/cloud-init that referenced this pull request Oct 2, 2026
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 pushed a commit to anujsingh-cse/cloud-init that referenced this pull request Oct 8, 2026
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 pushed a commit that referenced this pull request Oct 8, 2026
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
Removing cloudinit.sources.helpers.vmware.imc.config_file and
tests.unittests.sources.vmware.test_vmware_config_file from the mypy
override list in pyproject.toml surfaces six errors.

config_file.py: the standard configparser idiom
config.optionxform = str assigns a class to a method, which mypy flags
as both method-assign and assignment; this is intentional (disables
option lowercasing) and follows the existing ignore style used in
cloudinit/config/schema.py.

test_vmware_config_file.py: two assertions chain .get("nameservers").get(...)
which mypy sees as union-attr because dict.get returns Optional. Adding
the {} default keeps runtime behavior identical when the key is present
and makes the Optional case explicit.

Refs canonicalGH-5445
Per review: add a comment explaining why the ignore is needed. The
assignment is the documented configparser idiom for preserving key
case; mypy sees it as assigning a class to a method.

Refs canonicalGH-5445
canonical#6958 added base64 error-message parameters to
test_azure_helper.py with implicitly concatenated strings inside
collection literals, which the pinned ruff flags as ISC004. CI for
every open PR runs against the merge ref, so this fails check_format
across the board. Wrap the two concatenations in parentheses; the
string values are unchanged.
@anujsingh-cse

Copy link
Copy Markdown
Contributor Author

Rebased onto current main to clear the conflict with #7107 (both PRs edited the same mypy override list region). The pyproject resolution keeps both removals, and the shadowing/Optional test commit dropped from this branch since #7107 already carried the identical content upstream — the module now type-checks and passes against the merged state, as intended.

Also carrying one lint fix so check_format is green on the fresh run: #6958 introduced two ISC004 (implicit string concatenation) errors in test_azure_helper.py on main, which every merge-ref CI run currently inherits; the two concatenations are parenthesized with no value change.

Verified locally: ruff check . (pinned 0.16.2) passes on the whole tree, mypy is clean for config_file and the vmware test module, and all 22 vmware config-file tests pass.

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