cloud-init icon indicating copy to clipboard operation
cloud-init copied to clipboard

chore: add type annotations to cloudinit.distros.parsers.hosts

Open PhinehasNarh opened this issue 5 months ago • 0 comments

Summary

Adds full type annotations to cloudinit/distros/parsers/hosts.py so it can be removed from the check_untyped_defs = false mypy override in pyproject.toml. Part of #5445.

  • Replace the None-sentinel _contents with a _parsed: bool flag and an empty-list default, consistent with the approach used in resolv_conf.py (GH-6909), to avoid needing Optional and narrowing asserts
  • Annotate all method signatures (parameter types and return types)
  • Use List, Tuple, Any from typing for Python 3.8 compatibility
  • Clean up the variable shadowing in __str__ where pieces was reassigned across incompatible types; use distinct raw_pieces / str_pieces names instead
  • Change add_entry to store option entries as a list rather than a tuple, keeping _contents type consistent with what _parse produces
  • Remove both cloudinit.distros.parsers.hosts and tests.unittests.distros.test_hosts from the mypy override block

Test plan

  • [ ] Existing tests in tests/unittests/distros/test_hosts.py pass unchanged
  • [ ] mypy cloudinit/distros/parsers/hosts.py reports no errors

PhinehasNarh avatar Jun 27 '26 14:06 PhinehasNarh