From 32723e04f5edddf4ccca280efa05afbb122929d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sun, 20 Oct 2024 11:34:46 -0400 Subject: [PATCH] style: Fix if-else-block-instead-of-dict-get (SIM401) (#4558) Ruff rule: https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-get/ Python docs: https://docs.python.org/3/library/stdtypes.html#dict.get The get method never raises a KeyError. --- gui/wxpython/iclass/frame.py | 2 +- gui/wxpython/mapdisp/frame.py | 2 +- gui/wxpython/vnet/dialogs.py | 5 +---- gui/wxpython/vnet/vnet_core.py | 5 +---- pyproject.toml | 1 - python/grass/temporal/register.py | 5 +---- scripts/i.pansharpen/i.pansharpen.py | 5 +---- 7 files changed, 6 insertions(+), 19 deletions(-) diff --git a/gui/wxpython/iclass/frame.py b/gui/wxpython/iclass/frame.py index 606c364c76f..b19d8d7d3ac 100644 --- a/gui/wxpython/iclass/frame.py +++ b/gui/wxpython/iclass/frame.py @@ -515,7 +515,7 @@ def ActivateSecondMap(self, event=None): def GetMapToolbar(self): """Returns toolbar with zooming tools""" - return self.toolbars["iClassMap"] if "iClassMap" in self.toolbars else None + return self.toolbars.get("iClassMap", None) def GetClassColor(self, cat): """Get class color as string diff --git a/gui/wxpython/mapdisp/frame.py b/gui/wxpython/mapdisp/frame.py index c9b5d86e4ba..0a1660ff06a 100644 --- a/gui/wxpython/mapdisp/frame.py +++ b/gui/wxpython/mapdisp/frame.py @@ -1569,7 +1569,7 @@ def SetProperties( def GetMapToolbar(self): """Returns toolbar with zooming tools""" - return self.toolbars["map"] if "map" in self.toolbars else None + return self.toolbars.get("map", None) def GetDialog(self, name): """Get selected dialog if exist""" diff --git a/gui/wxpython/vnet/dialogs.py b/gui/wxpython/vnet/dialogs.py index 8d24b08df91..ee553a0b741 100644 --- a/gui/wxpython/vnet/dialogs.py +++ b/gui/wxpython/vnet/dialogs.py @@ -1015,10 +1015,7 @@ def OnAnalysisChanged(self, event): attrCols = an_props["cmdParams"]["cols"] for col in attrCols.keys(): - if "inputField" in attrCols[col]: - colInptF = attrCols[col]["inputField"] - else: - colInptF = col + colInptF = attrCols[col].get("inputField", col) if col in skip: continue diff --git a/gui/wxpython/vnet/vnet_core.py b/gui/wxpython/vnet/vnet_core.py index 2e98aa2ee8f..f55550e65e2 100644 --- a/gui/wxpython/vnet/vnet_core.py +++ b/gui/wxpython/vnet/vnet_core.py @@ -744,10 +744,7 @@ def _setInputParams(self, analysis, params, flags): inParams = [] for col, v in self.data.GetAnalysisProperties()["cmdParams"]["cols"].items(): - if "inputField" in v: - colInptF = v["inputField"] - else: - colInptF = col + colInptF = v.get("inputField", col) inParams.append(col + "=" + params[colInptF]) diff --git a/pyproject.toml b/pyproject.toml index 6aa9e062c29..eeffbc0ec95 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -249,7 +249,6 @@ ignore = [ "SIM116", # if-else-block-instead-of-dict-lookup "SIM118", # in-dict-keys "SIM223", # expr-and-false - "SIM401", # if-else-block-instead-of-dict-get "SLF001", # private-member-access "TRY002", # raise-vanilla-class "TRY003", # raise-vanilla-args diff --git a/python/grass/temporal/register.py b/python/grass/temporal/register.py index 8b7852ab557..92be36a3547 100644 --- a/python/grass/temporal/register.py +++ b/python/grass/temporal/register.py @@ -264,10 +264,7 @@ def register_maps_in_space_time_dataset( end = row["end"] # Use the semantic label from file - if "semantic_label" in row: - semantic_label = row["semantic_label"] - else: - semantic_label = None + semantic_label = row.get("semantic_label", None) is_in_db = map_object.is_in_db(dbif, mapset) diff --git a/scripts/i.pansharpen/i.pansharpen.py b/scripts/i.pansharpen/i.pansharpen.py index bb271c07deb..e78581c8845 100755 --- a/scripts/i.pansharpen/i.pansharpen.py +++ b/scripts/i.pansharpen/i.pansharpen.py @@ -750,10 +750,7 @@ def matchhist(original, target, matched): ) for n in range(256): - if str(n) in stats_dict: - num_cells = stats_dict[str(n)] - else: - num_cells = 0 + num_cells = stats_dict.get(str(n), 0) cum_cells += num_cells