chore: add typing to cloudinit.distros.parsers hostname and resolv_conf#6848
chore: add typing to cloudinit.distros.parsers hostname and resolv_conf#6848Ahmedaltu wants to merge 3 commits intocanonical:mainfrom
Conversation
canonicalGH-5445 Signed-off-by: Ahmed <76178825+Ahmedaltu@users.noreply.github.com>
holmanb
left a comment
There was a problem hiding this comment.
Please don't use assert for type narrowing.
| def __str__(self) -> str: | ||
| self.parse() | ||
| contents = StringIO() | ||
| assert self._contents is not None |
There was a problem hiding this comment.
Please don't use assertions in runtime code for type narrowing.
| assert self._contents is not None | ||
| assert self._contents is not None |
| assert self._contents is not None | ||
| assert self._contents is not None |
Signed-off-by: Ahmed <76178825+Ahmedaltu@users.noreply.github.com>
|
Thanks @holmanb. Replaced |
| contents = StringIO() | ||
| buf = StringIO() |
There was a problem hiding this comment.
Please don't make unnecessary variable renames.
Signed-off-by: Ahmed <76178825+Ahmedaltu@users.noreply.github.com>
|
Done — reverted the variable rename. Kept |
| def __init__(self, text: str) -> None: | ||
| self._text = text | ||
| self._contents = None | ||
| self._parsed: bool = False |
There was a problem hiding this comment.
This new variable seems unnecessary and error-prone. It should be possible to simply check the truthy value of self._contents rather than manually using a boolean to track the same thing.
There was a problem hiding this comment.
Hi @holmanb, I considered using if not self._contents but there's a subtle issue — when the input text is empty or contains only comments, _parse() returns an empty list []. Since an empty list is falsy, using if not self._contents would cause parse() to re-run on every call for empty files, defeating the lazy-parse caching.
The _parsed flag correctly distinguishes between "not yet parsed" and "parsed but empty result". This is the same reason network_state.py uses a dedicated _parsed boolean rather than checking the contents directly.
Happy to use a different approach if you have a preferred solution for this case.
|
Hello! Thank you for this proposed change to cloud-init. This pull request is now marked as stale as it has not seen any activity in 14 days. If no activity occurs within the next 7 days, this pull request will automatically close. If you are waiting for code review and you are seeing this message, apologies! Please reply, tagging blackboxsw, and he will ensure that someone takes a look soon. (If the pull request is closed and you would like to continue working on it, please do tag blackboxsw to reopen it.) |
GH-5445
Add type annotations to fix
check_untyped_defsmypy errors in:cloudinit/distros/parsers/hostname.pycloudinit/distros/parsers/resolv_conf.pyChanges:
List,Optional,Tupleimports fromtyping_contentsasOptional[List[Tuple[str, List[str]]]]assert self._contents is not Noneguards afterparse()calls__str__inhostname.py— renamedcontentsvariable to avoidshadowing
StringIOobject withstrreassignmentcloudinit.distros.parsers.hostnameandcloudinit.distros.parsers.resolv_conffrom disabled modules inpyproject.tomlVerified with:
mypy cloudinit/distros/parsers/hostname.py cloudinit/distros/parsers/resolv_conf.py --ignore-missing-imports
Success: no issues found in 2 source files