chore: add type annotations to cloudinit.user_data - #7025
Mohith1612 wants to merge 2 commits into
Conversation
| if include_once_on: | ||
| include_once_fn = self._get_include_once_filename(include_url) | ||
| if include_once_on and os.path.isfile(include_once_fn): | ||
| if include_once_fn is not None and os.path.isfile(include_once_fn): |
There was a problem hiding this comment.
It would read more cleanly if these were just nested together. I think this is safe since isfile returns a string, so the None check is redundant anyway.
There was a problem hiding this comment.
Understood, will update the code!
1e1739f to
fc7fbc7
Compare
|
Nested as suggested, thanks. The redundant if include_once_on:
include_once_fn = self._get_include_once_filename(include_url)
if os.path.isfile(include_once_fn):
content = util.load_text_file(include_once_fn)
if content is None:
try:One note on the fall-through, since it moved from Small correction on the reasoning, though it does not change the outcome: I also rewrote the commit message to describe the current state rather than what the code used to do, per @blackboxsw's note on #7021.
|
|
This has conflicts, please fix. |
Two of the three errors are in _do_include. include_once_fn starts as None and is only assigned when include_once_on is true, but the guard below tested include_once_on, which mypy cannot connect to include_once_fn, so it stayed Optional at the isfile and load_text_file calls. Test include_once_fn itself instead. _get_include_once_filename always returns an os.path.join result, so include_once_fn is None exactly when include_once_on is false and the two conditions are equivalent. The third is in _explode_archive, where msg is assigned a MIMEText in one branch and a MIMEBase in the other. MIMEText subclasses MIMEBase, so declare msg as MIMEBase. Drop cloudinit.user_data from the mypy override list in pyproject.toml. Refs canonicalGH-5445
Nest the cached-file check inside the include_once_on branch and fall through on content is None, which drops the redundant None guard.
fc7fbc7 to
366c708
Compare
|
Rebased onto main, conflict resolved. The conflict was in the The code change is unchanged from the version you reviewed. |
Proposed Commit Message
Additional Context
Refs GH-5445. This module is marked as a priority in the issue checklist.
Removing it from the override list surfaces three errors:
The first two.
include_once_fnis only assigned insideif include_once_on:, but the lookup below sat outside that branch, so mypystill saw it as
Optionalat theisfileandload_text_filecalls. Nestingthe lookup inside the branch it depends on resolves that without any extra
guard.
The fall-through changes from
elsetoif content is None, which isequivalent: the only assignment in the nested branch is
util.load_text_file(), which is annotated-> strand so never returnsNone. The fetch therefore runs in exactly the same cases as before, and acached file that happens to be empty still suppresses the fetch, as it did
previously.
I left the later
if include_once_on and resp.ok()guard alone. It does notproduce an error and changing it would be noise.
The third. In
_explode_archive,msggets aMIMETextin thetextbranch and a
MIMEBasein the other, so mypy pins it toMIMETextfrom thefirst assignment.
MIMETextis a subclass ofMIMEBaseand both are alreadyimported, so a bare
msg: MIMEBasedeclaration is enough. No assignmentchanges.
Test Steps
No runtime behaviour change, so no new tests.
tests/unittests/test_data.pyalready covers the include and include-once paths through
_do_include, andthe archive handling through
_explode_archive.Merge type