Skip to content

fix(cmd): enable mypy strict checking for cloudinit.cmd#6861

Open
jaborvs wants to merge 4 commits intocanonical:mainfrom
jaborvs:fix/typing-cloudinit-cmd
Open

fix(cmd): enable mypy strict checking for cloudinit.cmd#6861
jaborvs wants to merge 4 commits intocanonical:mainfrom
jaborvs:fix/typing-cloudinit-cmd

Conversation

@jaborvs
Copy link
Copy Markdown

@jaborvs jaborvs commented May 4, 2026

Partially-fixes: GH-5445

As part of my onboarding, I fixed the untyped defs errors found the following files:

  • cloudinit.cmd.devel.make_mime
  • cloudinit.cmd.devel.net_convert
  • cloudinit.cmd.main

Proposed Commit Message

fix(typing): Enable mypy strict checking for cloudinit.cmd

Remove cloudinit.cmd.main, cloudinit.cmd.devel.net_convert and
cloudinit.cmd.devel.make_mime from the check_untyped_defs=false
override list in pyproject.toml and resolve all mypy errors for
each module.

Partially-fixes: GH-5445

Additional Context

N/A

Test Steps

N/A

Merge type

  • Squash merge using "Proposed Commit Message"
  • Rebase and merge unique commits. Requires commit messages per-commit each referencing the pull request number (#<PR_NUM>)

@jaborvs jaborvs changed the title Enable mypy strict checking for cloudinit.cmd (Partially-fixes GH-5445) Enable mypy strict checking for cloudinit.cmd (partially-fixes GH-5445) May 4, 2026
@jaborvs jaborvs marked this pull request as draft May 4, 2026 13:36
@jaborvs jaborvs force-pushed the fix/typing-cloudinit-cmd branch 4 times, most recently from 2d61b2a to 7599c4f Compare May 5, 2026 08:13
@jaborvs jaborvs force-pushed the fix/typing-cloudinit-cmd branch from 7599c4f to db7b5a7 Compare May 5, 2026 09:38
@jaborvs jaborvs marked this pull request as ready for review May 5, 2026 09:48
@jaborvs
Copy link
Copy Markdown
Author

jaborvs commented May 5, 2026

There is an extra commit I will remove once the review is completed. This is to fix the error in the "Unit: Alternate Distros / build (pull_request)" job, which fails if the package py3-python-discovery is not installed in the install dependencies step. There is an open PR which implements this fix, pending to be approved and merged (see PR-6864)

@jaborvs jaborvs changed the title Enable mypy strict checking for cloudinit.cmd (partially-fixes GH-5445) fix: enable mypy strict checking for cloudinit.cmd (partially-fixes GH-5445) May 5, 2026
@jaborvs jaborvs changed the title fix: enable mypy strict checking for cloudinit.cmd (partially-fixes GH-5445) fix: enable mypy strict checking for cloudinit.cmd May 5, 2026
@jaborvs jaborvs changed the title fix: enable mypy strict checking for cloudinit.cmd fix(cmd): enable mypy strict checking for cloudinit.cmd May 5, 2026
Copy link
Copy Markdown
Member

@holmanb holmanb left a comment

Choose a reason for hiding this comment

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

Thanks for this. I left some feedback.

Comment on lines +35 to +37
err_msg = (
"content type %r for attachment %s may be incorrect!"
) % (
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.

It is odd that this type is not a string in the first place. Creating a tuple to append to a list of strings is unnecessary.

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.

That said, this doesn't need to turn into refactoring. I'd just name the later re-assignment something else like "mime_msg".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the feedback!

Just to clarify my understanding: (content_type, i + 1) does form a tuple as the right operand to %, which is standard for %-formatting with multiple arguments. However, the % operator itself returns a str directly, so what gets appended to errors is already a string, not a tuple. I believe the parentheses around the template string on the left were purely for line wrapping clarity. I have done a quick test to verify the typing and this assumption seems to be correct!

That said, I'm happy to rename the variable from err_msg to mime_msg or to split the formatting logic into several variables if it helps making the code clearer.

What do you think is the best option?

Comment thread cloudinit/cmd/main.py
)
)
if not isinstance(handler, loggers.LogExporter):
raise RuntimeError("LogExporter handler not found in root logger")
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.

It would be preferred to avoid changing control flow. Same comment elsewhere in this file.

Comment thread cloudinit/cmd/main.py
if self._subparsers is None:
self.print_help(file=sys.stderr)
sys.exit(2)
choices = self._subparsers._group_actions[0].choices
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.

This doesn't seem ideal. Is a way to do this without accessing internal attributes?

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.

[enhancement]: fix typing of untyped-defs

2 participants