chore: add type annotations to cloudinit.distros.parsers.hosts#6916
Open
PhinehasNarh wants to merge 1 commit into
Open
chore: add type annotations to cloudinit.distros.parsers.hosts#6916PhinehasNarh wants to merge 1 commit into
PhinehasNarh wants to merge 1 commit into
Conversation
Enable mypy check_untyped_defs for cloudinit/distros/parsers/hosts.py and its test module by adding full type annotations. Changes: - Replace None-sentinel _contents with a _parsed bool flag and an empty-list default, matching the pattern used in resolv_conf.py (canonicalGH-6909) - Annotate all method signatures with parameter and return types - Use List/Tuple/Any from typing for Python 3.8 compatibility - Rename shadowed variable in __str__ to avoid reassignment type mismatch - Change add_entry to store option components as a list instead of a tuple to keep _contents type consistent - Remove cloudinit.distros.parsers.hosts and tests.unittests.distros.test_hosts from the mypy check_untyped_defs=false overrides in pyproject.toml Closes part of canonical#5445
holmanb
requested changes
Jun 29, 2026
holmanb
left a comment
Member
There was a problem hiding this comment.
This looks like it is going the right direction, thanks. I left a few nitpicks. Please make sure to sign the CLA. Also please run tox -e do_format to fix formatting issues.
| pieces = [str(p) for p in pieces] | ||
| pieces = "\t".join(pieces) | ||
| contents.write("%s%s\n" % (pieces, tail)) | ||
| (raw_pieces, tail) = components |
Member
There was a problem hiding this comment.
Please remove the parenthesis around the assignment - they are not needed.
| contents.write("%s%s\n" % (pieces, tail)) | ||
| (raw_pieces, tail) = components | ||
| str_pieces = [str(p) for p in raw_pieces] | ||
| contents.write("%s%s\n" % ("\t".join(str_pieces), tail)) |
Member
There was a problem hiding this comment.
I think an f-string here would make this easier to read.
Comment on lines
+60
to
+61
| def _parse(self, contents: str) -> List[Tuple[str, List[Any]]]: | ||
| entries: List[Tuple[str, List[Any]]] = [] |
Member
There was a problem hiding this comment.
Is there any reason this can't be List[Tuple[str, List[str]]]?
| def parse(self): | ||
| if self._contents is None: | ||
| def parse(self) -> None: | ||
| if not self._parsed: |
Member
There was a problem hiding this comment.
Rather than introducing the new attribute self._parsed, can we just check the truthiness of self._contents?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds full type annotations to
cloudinit/distros/parsers/hosts.pyso it can be removed from thecheck_untyped_defs = falsemypy override inpyproject.toml. Part of #5445.None-sentinel_contentswith a_parsed: boolflag and an empty-list default, consistent with the approach used inresolv_conf.py(chore: add typing to cloudinit.distros.parsers hostname and resolv_conf #6909), to avoid needingOptionaland narrowing assertsList,Tuple,Anyfromtypingfor Python 3.8 compatibility__str__wherepieceswas reassigned across incompatible types; use distinctraw_pieces/str_piecesnames insteadadd_entryto store option entries as a list rather than a tuple, keeping_contentstype consistent with what_parseproducescloudinit.distros.parsers.hostsandtests.unittests.distros.test_hostsfrom the mypy override blockTest plan
tests/unittests/distros/test_hosts.pypass unchangedmypy cloudinit/distros/parsers/hosts.pyreports no errors