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

chore: fix tip-ruff #5676

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cloudinit/sources/azure/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import datetime
from io import StringIO
from typing import Any, Dict, List, Optional, Tuple
from xml.etree import ElementTree # nosec B405
from xml.etree import ElementTree as ET # nosec B405

import requests

Expand Down Expand Up @@ -177,7 +177,7 @@ def __init__(self, message: str) -> None:


class ReportableErrorOvfParsingException(ReportableError):
def __init__(self, *, exception: ElementTree.ParseError) -> None:
def __init__(self, *, exception: ET.ParseError) -> None:
message = exception.msg
super().__init__(f"error parsing ovf-env.xml: {message}")

Expand Down
14 changes: 6 additions & 8 deletions cloudinit/sources/helpers/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from datetime import datetime
from time import sleep, time
from typing import Callable, List, Optional, TypeVar, Union
from xml.etree import ElementTree # nosec B405
from xml.etree import ElementTree as ET # nosec B405
from xml.sax.saxutils import escape # nosec B406

from cloudinit import distros, subp, temp_utils, url_helper, util, version
Expand Down Expand Up @@ -363,8 +363,8 @@ def __init__(
self.azure_endpoint_client = azure_endpoint_client

try:
self.root = ElementTree.fromstring(unparsed_xml) # nosec B314
except ElementTree.ParseError as e:
self.root = ET.fromstring(unparsed_xml) # nosec B314
except ET.ParseError as e:
report_diagnostic_event(
"Failed to parse GoalState XML: %s" % e,
logger_func=LOG.warning,
Expand Down Expand Up @@ -496,9 +496,7 @@ def _decrypt_certs_from_xml(self, certificates_xml):
"""Decrypt the certificates XML document using the our private key;
return the list of certs and private keys contained in the doc.
"""
tag = ElementTree.fromstring(certificates_xml).find( # nosec B314
".//Data"
)
tag = ET.fromstring(certificates_xml).find(".//Data") # nosec B314
certificates_content = tag.text
lines = [
b"MIME-Version: 1.0",
Expand Down Expand Up @@ -1006,8 +1004,8 @@ def parse_text(cls, ovf_env_xml: str) -> "OvfEnvXml":
unparsable or invalid.
"""
try:
root = ElementTree.fromstring(ovf_env_xml) # nosec B314
except ElementTree.ParseError as e:
root = ET.fromstring(ovf_env_xml) # nosec B314
except ET.ParseError as e:
raise errors.ReportableErrorOvfParsingException(exception=e) from e

# If there's no provisioning section, it's not Azure ovf-env.xml.
Expand Down
6 changes: 3 additions & 3 deletions tests/unittests/sources/azure/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import base64
import datetime
from unittest import mock
from xml.etree import ElementTree
from xml.etree import ElementTree as ET

import pytest
import requests
Expand Down Expand Up @@ -211,8 +211,8 @@ def test_imds_metadata_parsing_exception():
def test_ovf_parsing_exception():
error = None
try:
ElementTree.fromstring("<badxml")
except ElementTree.ParseError as exception:
ET.fromstring("<badxml")
except ET.ParseError as exception:
error = errors.ReportableErrorOvfParsingException(exception=exception)

assert (
Expand Down
12 changes: 6 additions & 6 deletions tests/unittests/sources/test_azure_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import unittest
from textwrap import dedent
from xml.etree import ElementTree
from xml.etree import ElementTree as ET
from xml.sax.saxutils import escape, unescape

import pytest
Expand Down Expand Up @@ -231,7 +231,7 @@ def test_missing_certificates_skips_http_get(self):

def test_invalid_goal_state_xml_raises_parse_error(self):
xml = "random non-xml data"
with self.assertRaises(ElementTree.ParseError):
with self.assertRaises(ET.ParseError):
azure_helper.GoalState(xml, mock.MagicMock())

def test_missing_container_id_in_goal_state_xml_raises_exc(self):
Expand Down Expand Up @@ -717,7 +717,7 @@ def test_build_report_for_ready_signal_health_document(self):

self.assertEqual(health_document, generated_health_document)

generated_xroot = ElementTree.fromstring(generated_health_document)
generated_xroot = ET.fromstring(generated_health_document)
self.assertEqual(
self._text_from_xpath_in_xroot(
generated_xroot, "./GoalStateIncarnation"
Expand Down Expand Up @@ -780,7 +780,7 @@ def test_build_report_for_failure_signal_health_document(self):

self.assertEqual(health_document, generated_health_document)

generated_xroot = ElementTree.fromstring(generated_health_document)
generated_xroot = ET.fromstring(generated_health_document)
self.assertEqual(
self._text_from_xpath_in_xroot(
generated_xroot, "./GoalStateIncarnation"
Expand Down Expand Up @@ -923,7 +923,7 @@ def test_build_report_conforms_to_length_limits(self):
description=long_err_msg,
)

generated_xroot = ElementTree.fromstring(generated_health_document)
generated_xroot = ET.fromstring(generated_health_document)
generated_health_report_description = self._text_from_xpath_in_xroot(
generated_xroot,
"./Container/RoleInstanceList/Role/Health/Details/Description",
Expand Down Expand Up @@ -976,7 +976,7 @@ def test_trim_description_then_escape_conforms_to_len_limits_worst_case(
description=long_err_msg,
)

generated_xroot = ElementTree.fromstring(generated_health_document)
generated_xroot = ET.fromstring(generated_health_document)
generated_health_report_description = self._text_from_xpath_in_xroot(
generated_xroot,
"./Container/RoleInstanceList/Role/Health/Details/Description",
Expand Down