Skip to content

Commit c4fac41

Browse files
committed
chore(opennebula): enable mypy type checking
Add type annotations to DataSourceOpenNebula.py to address concerns raised in PR canonical#6810 about type checking being disabled for the OpenNebula datasource. Refs canonicalGH-6810
1 parent ffd26f1 commit c4fac41

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

cloudinit/sources/DataSourceOpenNebula.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import re
2121
import shlex
2222
import textwrap
23+
from typing import Any, Dict, List, Optional, Union
2324

2425
from cloudinit import atomic_helper, net, sources, subp, util
2526

@@ -41,7 +42,6 @@
4142

4243

4344
class DataSourceOpenNebula(sources.DataSource):
44-
4545
dsname = "OpenNebula"
4646

4747
def __init__(self, sys_cfg, distro, paths):
@@ -56,8 +56,8 @@ def __str__(self):
5656

5757
def _get_data(self):
5858
defaults = {"instance-id": DEFAULT_IID}
59-
results = None
60-
seed = None
59+
results: Optional[Dict[str, Any]] = None
60+
seed: Optional[str] = None
6161

6262
# decide parseuser for context.sh shell reader
6363
parseuser = DEFAULT_PARSEUSER
@@ -97,6 +97,8 @@ def _get_data(self):
9797
if not seed:
9898
return False
9999

100+
assert results is not None
101+
100102
# merge fetched metadata with datasource defaults
101103
md = results["metadata"]
102104
md = util.mergemanydict([md, defaults])
@@ -117,6 +119,7 @@ def _get_data(self):
117119

118120
def _get_subplatform(self):
119121
"""Return the subplatform metadata source details."""
122+
assert self.seed is not None
120123
if self.seed_dir in self.seed:
121124
subplatform_type = "seed-dir"
122125
else:
@@ -233,20 +236,18 @@ def get_field(self, dev, name, default=None):
233236
# allow empty string to return the default.
234237
return default if val in (None, "") else val
235238

236-
def gen_conf(self):
237-
netconf = {}
238-
netconf["version"] = 2
239-
netconf["ethernets"] = {}
239+
def gen_conf(self) -> Dict[str, Any]:
240+
netconf: Dict[str, Any] = {"version": 2, "ethernets": {}}
240241

241-
ethernets = {}
242+
ethernets: Dict[str, Dict[str, Any]] = {}
242243
for mac, dev in self.ifaces.items():
243244
mac = mac.lower()
244245

245246
# c_dev stores name in context 'ETHX' for this device.
246247
# dev stores the current system name.
247248
c_dev = self.context_devname.get(mac, dev)
248249

249-
devconf = {}
250+
devconf: Dict[str, Any] = {}
250251

251252
# Set MAC address
252253
devconf["match"] = {"macaddress": mac}
@@ -394,13 +395,15 @@ def parse_shell_config(content, asuser=None):
394395
return ret
395396

396397

397-
def read_context_disk_dir(source_dir, distro, asuser=None):
398+
def read_context_disk_dir(
399+
source_dir: str, distro: Any, asuser: Optional[str] = None
400+
) -> Dict[str, Any]:
398401
"""
399402
read_context_disk_dir(source_dir):
400403
read source_dir and return a tuple with metadata dict and user-data
401404
string populated. If not a valid dir, raise a NonContextDiskDir
402405
"""
403-
found = {}
406+
found: Dict[str, str] = {}
404407
for af in CONTEXT_DISK_FILES:
405408
fn = os.path.join(source_dir, af)
406409
if os.path.isfile(fn):
@@ -409,8 +412,8 @@ def read_context_disk_dir(source_dir, distro, asuser=None):
409412
if not found:
410413
raise NonContextDiskDir("%s: %s" % (source_dir, "no files found"))
411414

412-
context = {}
413-
results = {"userdata": None, "metadata": {}}
415+
context: Dict[str, str] = {}
416+
results: Dict[str, Any] = {"userdata": None, "metadata": {}}
414417

415418
if "context.sh" in found:
416419
if asuser is not None:
@@ -450,7 +453,7 @@ def read_context_disk_dir(source_dir, distro, asuser=None):
450453
ssh_key_var = "SSH_PUBLIC_KEY"
451454

452455
if ssh_key_var:
453-
lines = context.get(ssh_key_var).splitlines()
456+
lines = context[ssh_key_var].splitlines()
454457
results["metadata"]["public-keys"] = [
455458
line for line in lines if len(line) and not line.startswith("#")
456459
]

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ module = [
7878
"cloudinit.sources.DataSourceHetzner",
7979
"cloudinit.sources.DataSourceNoCloud",
8080
"cloudinit.sources.DataSourceOVF",
81-
"cloudinit.sources.DataSourceOpenNebula",
8281
"cloudinit.sources.DataSourceOpenStack",
8382
"cloudinit.sources.DataSourceOracle",
8483
"cloudinit.sources.DataSourceRbxCloud",
@@ -146,7 +145,6 @@ module = [
146145
"tests.unittests.sources.test_gce",
147146
"tests.unittests.sources.test_init",
148147
"tests.unittests.sources.test_nocloud",
149-
"tests.unittests.sources.test_opennebula",
150148
"tests.unittests.sources.test_openstack",
151149
"tests.unittests.sources.test_oracle",
152150
"tests.unittests.sources.test_scaleway",

tests/unittests/sources/test_opennebula.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,12 @@ def my_devs_with(criteria):
365365
}.get(criteria, [])
366366

367367
m_find_devs_with.side_effect = my_devs_with
368-
util.find_devs_with = my_devs_with
368+
util.find_devs_with = my_devs_with # type: ignore[assignment]
369369
assert ["/dev/sdb", "/dev/sr0", "/dev/vdb"] == ds.find_candidate_devs()
370370

371371

372372
@mock.patch(DS_PATH + ".net.get_interfaces_by_mac", mock.Mock(return_value={}))
373373
class TestOpenNebulaNetwork:
374-
375374
system_nics = ("eth0", "ens3")
376375

377376
def test_context_devname(self):
@@ -1016,8 +1015,8 @@ class TestGetPhysicalNicsByMac:
10161015
)
10171016
def test(self, interfaces_by_mac, physical_devs, expected_return):
10181017
distro = mock.Mock()
1019-
distro.networking.is_physical.side_effect = (
1020-
lambda devname: devname in physical_devs
1018+
distro.networking.is_physical.side_effect = lambda devname: (
1019+
devname in physical_devs
10211020
)
10221021
with mock.patch(
10231022
DS_PATH + ".net.get_interfaces_by_mac",

0 commit comments

Comments
 (0)