Skip to content

Commit

Permalink
refactor: Ensure '_cfg' in Init class is dict (canonical#4674)
Browse files Browse the repository at this point in the history
Optional just complicates the typing and is unnecessary.
  • Loading branch information
TheRealFalcon committed Dec 8, 2023
1 parent a6ef369 commit 949af54
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
10 changes: 3 additions & 7 deletions cloudinit/stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __init__(self, ds_deps: Optional[List[str]] = None, reporter=None):
else:
self.ds_deps = [sources.DEP_FILESYSTEM, sources.DEP_NETWORK]
# Created on first use
self._cfg: Optional[dict] = None
self._cfg: Dict = {}
self._paths: Optional[helpers.Paths] = None
self._distro: Optional[distros.Distro] = None
# Changed only when a fetch occurs
Expand All @@ -136,7 +136,7 @@ def __init__(self, ds_deps: Optional[List[str]] = None, reporter=None):

def _reset(self):
# Recreated on access
self._cfg = None
self._cfg = {}
self._paths = None
self._distro = None

Expand Down Expand Up @@ -172,8 +172,6 @@ def _extract_cfg(self, restriction):
ocfg = util.get_cfg_by_path(ocfg, ("system_info",), {})
elif restriction == "paths":
ocfg = util.get_cfg_by_path(ocfg, ("system_info", "paths"), {})
if not isinstance(ocfg, (dict)):
ocfg = {}
return ocfg

@property
Expand Down Expand Up @@ -255,10 +253,8 @@ def _initialize_filesystem(self):
)

def read_cfg(self, extra_fns=None):
# None check so that we don't keep on re-loading if empty
if self._cfg is None:
if not self._cfg:
self._cfg = self._read_cfg(extra_fns)
# LOG.debug("Loaded 'init' config %s", self._cfg)

def _read_cfg(self, extra_fns):
no_cfg_paths = helpers.Paths({}, self.datasource)
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/test_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,9 @@ def init(self, paths):
yield init

@mock.patch(M_PATH + "util.ensure_file")
@mock.patch(f"{M_PATH}Init._read_cfg")
def test_ensure_file_not_called_if_no_log_file_configured(
self, m_ensure_file, init
self, m_read_cfg, m_ensure_file, init
):
"""If no log file is configured, we should not ensure its existence."""
init._cfg = {}
Expand Down

0 comments on commit 949af54

Please sign in to comment.