Skip to content

typing: add type hints to cloudinit.distros.parsers.hostname - #6996

Open
GoUrAv-MaLiK wants to merge 1 commit into
canonical:mainfrom
GoUrAv-MaLiK:type/hostname-conf
Open

typing: add type hints to cloudinit.distros.parsers.hostname#6996
GoUrAv-MaLiK wants to merge 1 commit into
canonical:mainfrom
GoUrAv-MaLiK:type/hostname-conf

Conversation

@GoUrAv-MaLiK

@GoUrAv-MaLiK GoUrAv-MaLiK commented Aug 12, 2026

Copy link
Copy Markdown

Enables check_untyped_defs for cloudinit.distros.parsers.resolv_conf by annotating ResolvConf and removing it from the mypy override list in pyproject.toml.

self._contents is Optional; it is now narrowed with an assert after parse() before it is iterated or appended to. Behaviour is unchanged.

Refs GH-5445

  • I have signed the CLA
  • I have included a comprehensive commit message
  • I have added unit tests — N/A: typing-only, no behaviour change; the class is already covered by tests/unittests/distros/test_resolv.py
  • I have kept the change small
  • I have added a reference to the related issue (Refs [enhancement]: fix typing of untyped-defs #5445)
  • I have updated the documentation — N/A: no user-facing change

Proposed Commit Message

typing: add type hints to cloudinit.distros.parsers.resolv_conf

Annotate ResolvConf and enable check_untyped_defs for the module by
removing it from the mypy override list in p

self._contents is Optional; it is now narrow
parse() before it is iterated or appended to. Behaviour is unchanged.

Refs GH-5445

Additional Context

Part of the incremental effort in GH-5445 to enable check_untyped_defs module-by-module.

Test Steps

tox -e mypy    # clean (270 files, --platform linux)
tox -e py3 -- tests/unittests/distros/test_rig/test_cc_resolv_conf.py   # 30 passed
tox -e ruff && tox -e black && tox -e isort   # clean

Merge type

  • Squash merge using "Proposed Commit Message"

Annotate HostnameConf and enable check_untyped_defs for the module by
removing it from the mypy override list in pyproject.toml.

Two issues surfaced by check_untyped_defs are fixed:
- self._contents is Optional and is now narrowed with an assert after
  parse() before it is iterated.
- __str__ no longer reuses the 'contents' name for both the StringIO
  buffer and its rendered string value.

Towards canonicalGH-5445.

@holmanb holmanb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the proposal.

def __str__(self) -> str:
self.parse()
contents = StringIO()
assert self._contents is not None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this technically is a form of type narrowing, it results in an exception when the assertion is fails.

@holmanb holmanb self-assigned this Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants