Repository navigation
chore: add type annotations to DataSourceConfigDrive - #7137
anujsingh-cse wants to merge 4 commits into
Conversation
Removing cloudinit.sources.DataSourceConfigDrive from the mypy
override list in pyproject.toml surfaces four errors.
The base class declares self.ec2_metadata = UNSET (a str sentinel), so
mypy types it as str; ConfigDrive assigns None in __init__ and
replaces it with an optional dict from results.get("ec2-metadata").
Annotate the base declaration as Optional[Any], matching the adjacent
self.network_json: Optional[str] = UNSET which already carries the
same sentinel-or-data pattern. The only reader compares against UNSET,
and the only other writer (DataSourceOpenStack) stays in the override
list until its own slice.
_get_subplatform calls self.source.startswith("/dev") and
self.source is Optional; the method is only invoked after get_data()
succeeded and set source, so raise ValueError on the impossible None
branch instead of crashing with AttributeError.
read_config_drive builds a list of (reader method, args, kwargs)
tuples that mypy cannot infer from an empty-containing literal;
annotate finders.
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.
95c940d to
ab17955
Compare
nryanl
left a comment
There was a problem hiding this comment.
See review comment in #7118, and also this commentary in #5445.
Cover the datasource and module helpers instead of only the spots needed to pass check_untyped_defs. read_config_drive's finder table only ever held empty args/kwargs, so it is now a plain callable list, which also removes the Any-based tuple type.
|
Rebased onto current main by merge — head 4d3d3c9, no force-push after review. The duplicate All 13 functions in The one supporting change outside the datasource is Gates on this head: black 26.5.1, ruff and mypy clean. |
Proposed Commit Message
Additional Context
Refs GH-5445. Claimed in #5445 (comment).
Removing the module from the override list surfaces four errors:
self.ec2_metadata = UNSET("_unset", a str). The attributegenuinely holds the sentinel, a metadata dict, or None across
datasources, so the declaration in
cloudinit/sources/__init__.pyis annotated
Optional[Any] = UNSET, the same sentinel-or-datapattern the adjacent
self.network_json: Optional[str] = UNSETalready uses. The only reader (
__init__.py:551) compares againstUNSET, and the only other writer (
DataSourceOpenStack.py) remainsin the override list until its own slice.
_get_subplatformreadsself.source.startswith("/dev")wheresourceis Optional. The method is only invoked after a successfulget_data(), which always sets source, so the impossible Nonebranch raises
ValueErrorwith a clear message instead of anuncaught AttributeError.
findersis a list of(reader method, args, kwargs)tuples thatmypy cannot infer; it is annotated.
Test Steps
tox -e py3andtox -e check_formatto be confirmed by CI.Merge type