Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix untyped-defs on /cloudinit/config & /tests/config/ #5985

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

MostafaTarek124eru
Copy link
Contributor

@MostafaTarek124eru MostafaTarek124eru commented Jan 21, 2025

Related to 5445

Part of the Onboarding.

✓ cloudinit.config.cc_power_state_change
✓ cloudinit.config.cc_rsyslog
✓ cloudinit.config.cc_ubuntu_pro
✓ tests/unittests/config/test_cc_rsyslog
✓ tests/unittests/config/test_cc_ubuntu_pro
✓ests/unittests/config/test_cc_power_state_change

Proposed Commit Message

fix untyped-defs on /cloudinit/config & /tests/config/

Additional Context

Test Steps

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>)

Copy link
Contributor Author

@MostafaTarek124eru MostafaTarek124eru left a comment

Choose a reason for hiding this comment

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

contributing to GH5445 as part of my on-boarding.

I've signed the CLA and ran the test.
Thanks to take a loot and check if anything else is needed

Copy link
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.

@MostafaTarek124eru welcome to cloud-init! I left two comments inline. Also, please sign the cla.

@@ -225,7 +226,10 @@ def configure_pro(token, enable=None):
# related. We can distinguish them by checking if `service` is non-null
# or null respectively.

enable_errors: List[dict] = []

def handle_enable_errors(enable_resp: Dict[str, Any]):
Copy link
Member

Choose a reason for hiding this comment

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

Making this a separate function makes the comment above nonsensical - it was describing this code yet it is no longer co-located with the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved the comment under the new function, Please check it

@@ -246,6 +246,7 @@ def __init__(
self.proto = proto

self.addr = addr
self.port = int(port) if port else None
Copy link
Member

Choose a reason for hiding this comment

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

This logic looks functionally identical to the next four lines. Why is it necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed it in the new commit

cloudinit/config/cc_rsyslog.py Outdated Show resolved Hide resolved
except json.JSONDecodeError as e:
raise RuntimeError(
f"Pro response was not json: {enable_stdout}"
) from e


def handle_enable_errors(enable_resp: Dict[str, Any]):
Copy link
Member

Choose a reason for hiding this comment

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

This silences valid mypy warnings without solving the underlying issue

consider:

>>> import json
>>> json.loads("[]")
[]

But this just told mypy that the type will be Dict[str, Any].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check the new commit. It should handle it now

self.port = int(port)
else:
self.port = None
self.port = int(port) if port is not None else None
Copy link
Member

Choose a reason for hiding this comment

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

For what it's worth, the first assignment could just be annotated with Optional[int], but this is fine too.

@@ -284,7 +281,7 @@ def __str__(self):
else:
buf += self.addr

if self.port:
if self.port is not None:
Copy link
Member

Choose a reason for hiding this comment

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

This isn't necessary. Why did you make this change?

@@ -196,56 +196,69 @@ def configure_pro(token, enable=None):

try:
enable_resp = json.loads(enable_stdout)
handle_enable_errors(enable_resp)
Copy link
Member

Choose a reason for hiding this comment

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

The annotation on this function is still wrong. json.loads() could return more types than this.

msg = f'Failure of type `{err["type"]}`: {err["message"]}'
util.logexc(LOG, msg)
def handle_enable_errors(enable_resp: Union[List[Any], Dict[str, Any]]):
if isinstance(enable_resp, list):
Copy link
Member

Choose a reason for hiding this comment

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

Why special case the list type but still use a conditional branch to handle all other types? Both do the same thing: log a warning and exit.

Copy link
Member

Choose a reason for hiding this comment

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

To minimize unnecessary diff size and eliminate unnecessary branches, I would recommend this:

if not isinstance(enable_resp, list):
    LOG.warn(...)
    return

else:
msg = f'Failure of type `{err["type"]}`: {err["message"]}'
util.logexc(LOG, msg)
def handle_enable_errors(enable_resp: Union[List[Any], Dict[str, Any]]):
Copy link
Member

Choose a reason for hiding this comment

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

This type annotation is inaccurate, and it just silences later errors that need to be addressed. Please remove the new function definition and fix the incorrect handling of types later in this module.

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.

3 participants