Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions cloudinit/sources/helpers/vmware/imc/config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
class ConfigFile(ConfigSource, dict):
"""ConfigFile module to load the content from a specified source."""

def __init__(self, filename):
def __init__(self, filename: str) -> None:
self._loadConfigFile(filename)

def _insertKey(self, key, val):
def _insertKey(self, key: str, val: str) -> None:
"""
Inserts a Key Value pair.

Expand All @@ -44,7 +44,7 @@ def _insertKey(self, key, val):

self[key] = val

def _loadConfigFile(self, filename):
def _loadConfigFile(self, filename: str) -> None:
"""
Parses properties from the specified config file.

Expand All @@ -58,7 +58,10 @@ def _loadConfigFile(self, filename):
logger.info("Parsing the config file %s.", filename)

config = configparser.ConfigParser()
config.optionxform = str
# Assigning the builtin str as the optionxform transform is the
# documented configparser idiom for preserving key case; mypy
# cannot express assigning a class to a method, hence the ignore.
config.optionxform = str # type: ignore [method-assign, assignment]

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.

Please include a comment documenting why this is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a comment above the line explaining why the ignore is needed (the documented configparser idiom for preserving key case, which mypy cannot express against a method type) — 0a37e72.

config.read(filename)

self.clear()
Expand All @@ -69,7 +72,7 @@ def _loadConfigFile(self, filename):
for key, value in config.items(category):
self._insertKey(category + "|" + key, value)

def get_count_with_prefix(self, prefix):
def get_count_with_prefix(self, prefix: str) -> int:
"""
Return the total count of keys that start with the specified prefix.

Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ module = [
"cloudinit.sources.DataSourceSmartOS",
"cloudinit.sources.helpers.azure",
"cloudinit.sources.helpers.openstack",
"cloudinit.sources.helpers.vmware.imc.config_file",
"tests.unittests.analyze.test_show",
"tests.unittests.config.test_apt_configure_sources_list_v3",
"tests.unittests.config.test_apt_source_v1",
Expand Down Expand Up @@ -116,7 +115,6 @@ module = [
"tests.unittests.sources.test_smartos",
"tests.unittests.sources.test_upcloud",
"tests.unittests.sources.test_vultr",
"tests.unittests.sources.vmware.test_vmware_config_file",
"tests.unittests.test__init__",
"tests.unittests.test_apport",
"tests.unittests.test_builtin_handlers",
Expand Down
8 changes: 4 additions & 4 deletions tests/unittests/sources/vmware/test_vmware_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def test_get_config_nameservers(self):

for _, config in ethernets.items():
assert isinstance(config, dict)
name_servers = config.get("nameservers").get("addresses")
dns_suffixes = config.get("nameservers").get("search")
name_servers = config.get("nameservers", {}).get("addresses")
dns_suffixes = config.get("nameservers", {}).get("search")
assert ["10.20.145.1", "10.20.145.2"] == name_servers, "dns"
assert [
"eng.vmware.com",
Expand All @@ -185,8 +185,8 @@ def test_get_config_dns_suffixes(self):

for _, config in ethernets.items():
assert isinstance(config, dict)
name_servers = config.get("nameservers").get("addresses")
dns_suffixes = config.get("nameservers").get("search")
name_servers = config.get("nameservers", {}).get("addresses")
dns_suffixes = config.get("nameservers", {}).get("search")
assert None is name_servers, "dns"
assert ["eng.vmware.com"] == dns_suffixes, "suffixes"

Expand Down
Loading