-
Notifications
You must be signed in to change notification settings - Fork 908
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,10 +246,7 @@ def __init__( | |
self.proto = proto | ||
|
||
self.addr = addr | ||
if port: | ||
self.port = int(port) | ||
else: | ||
self.port = None | ||
self.port = int(port) if port is not None else None | ||
|
||
def validate(self): | ||
if self.port: | ||
|
@@ -284,7 +281,7 @@ def __str__(self): | |
else: | ||
buf += self.addr | ||
|
||
if self.port: | ||
if self.port is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't necessary. Why did you make this change? |
||
buf += ":%s" % self.port | ||
|
||
if self.name: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
import json | ||
import logging | ||
import re | ||
from typing import Any, List | ||
from typing import Any, Dict, List, Union | ||
from urllib.parse import urlparse | ||
|
||
from cloudinit import performance, subp, util | ||
|
@@ -196,56 +196,69 @@ def configure_pro(token, enable=None): | |
|
||
try: | ||
enable_resp = json.loads(enable_stdout) | ||
handle_enable_errors(enable_resp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
except json.JSONDecodeError as e: | ||
raise RuntimeError( | ||
f"Pro response was not json: {enable_stdout}" | ||
) from e | ||
|
||
# At this point we were able to load the json response from Pro. This | ||
# response contains a list of errors under the key 'errors'. E.g. | ||
# | ||
# { | ||
# "errors": [ | ||
# { | ||
# "message": "UA Apps: ESM is already enabled ...", | ||
# "message_code": "service-already-enabled", | ||
# "service": "esm-apps", | ||
# "type": "service" | ||
# }, | ||
# { | ||
# "message": "Cannot enable unknown service 'asdf' ...", | ||
# "message_code": "invalid-service-or-failure", | ||
# "service": null, | ||
# "type": "system" | ||
# } | ||
# ] | ||
# } | ||
# | ||
# From our pov there are two type of errors, service and non-service | ||
# related. We can distinguish them by checking if `service` is non-null | ||
# or null respectively. | ||
|
||
enable_errors: List[dict] = [] | ||
for err in enable_resp.get("errors", []): | ||
if err["message_code"] == "service-already-enabled": | ||
LOG.debug("Service `%s` already enabled.", err["service"]) | ||
continue | ||
enable_errors.append(err) | ||
|
||
if enable_errors: | ||
error_services: List[str] = [] | ||
for err in enable_errors: | ||
service = err.get("service") | ||
if service is not None: | ||
error_services.append(service) | ||
msg = f'Failure enabling `{service}`: {err["message"]}' | ||
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]]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
if isinstance(enable_resp, list): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
LOG.warning( | ||
"Unexpected list response from pro enable: %s", enable_resp | ||
) | ||
return | ||
elif isinstance(enable_resp, dict) and "errors" in enable_resp: | ||
# At this point we were able to load the json response from Pro. This | ||
# response contains a list of errors under the key 'errors'. E.g. | ||
# | ||
# { | ||
# "errors": [ | ||
# { | ||
# "message": "UA Apps: ESM is already enabled ...", | ||
# "message_code": "service-already-enabled", | ||
# "service": "esm-apps", | ||
# "type": "service" | ||
# }, | ||
# { | ||
# "message": "Cannot enable unknown service 'asdf' ...", | ||
# "message_code": "invalid-service-or-failure", | ||
# "service": null, | ||
# "type": "system" | ||
# } | ||
# ] | ||
# } | ||
# | ||
# From our pov there are two type of errors, service and non-service | ||
# related. We can distinguish them by checking if `service` is non-null | ||
# or null respectively. | ||
enable_errors: List[Dict[str, Any]] = [] | ||
for err in enable_resp.get("errors", []): | ||
if err["message_code"] == "service-already-enabled": | ||
LOG.debug("Service `%s` already enabled.", err["service"]) | ||
continue | ||
enable_errors.append(err) | ||
|
||
if enable_errors: | ||
error_services: List[str] = [] | ||
for err in enable_errors: | ||
service = err.get("service") | ||
if service is not None: | ||
error_services.append(service) | ||
msg = f'Failure enabling `{service}`: {err["message"]}' | ||
else: | ||
msg = f'Failure of type `{err["type"]}`: {err["message"]}' | ||
util.logexc(LOG, msg) | ||
|
||
raise RuntimeError( | ||
"Failure enabling Ubuntu Pro service(s): " | ||
+ ", ".join(error_services) | ||
raise RuntimeError( | ||
"Failure enabling Ubuntu Pro service(s): " | ||
+ ", ".join(error_services) | ||
) | ||
else: | ||
LOG.warning( | ||
"Unexpected response format from pro enable: %s", enable_resp | ||
) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,7 @@ metajiji | |
michaelrommel | ||
mitechie | ||
MjMoshiri | ||
MostafaTarek124eru | ||
mxwebdev | ||
nazunalika | ||
nelsonad-ops | ||
|
There was a problem hiding this comment.
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.