Skip to content

Commit

Permalink
style: Fix needless-bool (SIM103) (OSGeo#4055)
Browse files Browse the repository at this point in the history
* style: Fix needless-bool (SIM103)

Return the condition directly
Ruff rule: https://docs.astral.sh/ruff/rules/needless-bool/

Added bool return type annotations when possible to confirm no other type was possible on changed functions

* Update frame.py

* Apply suggestions from code review

Co-authored-by: Edouard Choinière <27212526+echoix@users.noreply.github.com>

* style: Merge more if conditions per review

---------

Co-authored-by: Stefan Blumentrath <stefan.blumentrath@gmx.de>
  • Loading branch information
2 people authored and Mahesh1998 committed Sep 19, 2024
1 parent 8b59a5e commit b096d86
Show file tree
Hide file tree
Showing 45 changed files with 275 additions and 574 deletions.
7 changes: 2 additions & 5 deletions gui/wxpython/animation/nviztask.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,8 @@ def _join(self, toJoin, delim=","):
toJoin = filter(self._ignore, toJoin)
return delim.join(map(str, toJoin))

def _ignore(self, value):
if value == "" or value is None:
return False
else:
return True
def _ignore(self, value) -> bool:
return not (value == "" or value is None)

def ListMapParameters(self):
# params = self.task.get_list_params()
Expand Down
15 changes: 5 additions & 10 deletions gui/wxpython/core/globalvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,18 @@ def version_as_string(version):
return ".".join(texts)


def CheckWxPhoenix():
if "phoenix" in wx.version():
return True
return False
def CheckWxPhoenix() -> bool:
return "phoenix" in wx.version()


def CheckWxVersion(version):
def CheckWxVersion(version) -> bool:
"""Check wx version.
:return: True if current wx version is greater or equal than
specifed version otherwise False
specified version otherwise False
"""
parsed_version = parse_version_string(wx.__version__)
if parsed_version < version:
return False

return True
return not parsed_version < version


def CheckForWx():
Expand Down
6 changes: 2 additions & 4 deletions gui/wxpython/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,9 @@ def IsHidden(self):
"""Check if layer is hidden"""
return self.hidden

def IsRendered(self):
def IsRendered(self) -> bool:
"""!Check if layer was rendered (if the image file exists)"""
if os.path.exists(self.mapfile):
return True
return False
return bool(os.path.exists(self.mapfile))

def SetType(self, ltype):
"""Set layer type"""
Expand Down
14 changes: 5 additions & 9 deletions gui/wxpython/core/treemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,13 @@ def nprint(self, text, indent=0):
for child in self.children:
child.nprint(text, indent + 2)

def match(self, key, value):
def match(self, key, value) -> bool:
"""Method used for searching according to given parameters.
:param value: dictionary value to be matched
:param key: data dictionary key
"""
if key in self.data and self.data[key] == value:
return True
return False
return bool(key in self.data and self.data[key] == value)


class DictFilterNode(DictNode):
Expand Down Expand Up @@ -269,21 +267,19 @@ def _match_exact(self, **kwargs):
return False
return True

def _match_filtering(self, **kwargs):
def _match_filtering(self, **kwargs) -> bool:
"""Match method for filtering."""
if (
"type" in kwargs
and "type" in self.data
and kwargs["type"] != self.data["type"]
):
return False
if (
return not (
"name" in kwargs
and "name" in self.data
and not kwargs["name"].search(self.data["name"])
):
return False
return True
)


class ModuleNode(DictNode):
Expand Down
15 changes: 5 additions & 10 deletions gui/wxpython/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ def autoCropImageFromFile(filename):
return wx.Image(filename)


def isInRegion(regionA, regionB):
def isInRegion(regionA, regionB) -> bool:
"""Tests if 'regionA' is inside of 'regionB'.
For example, region A is a display region and region B is some reference
Expand All @@ -1097,15 +1097,12 @@ def isInRegion(regionA, regionB):
:return: True if region A is inside of region B
:return: False otherwise
"""
if (
return bool(
regionA["s"] >= regionB["s"]
and regionA["n"] <= regionB["n"]
and regionA["w"] >= regionB["w"]
and regionA["e"] <= regionB["e"]
):
return True

return False
)


def do_doctest_gettext_workaround():
Expand Down Expand Up @@ -1187,11 +1184,9 @@ def get_shell_pid(env=None):
return None


def is_shell_running():
def is_shell_running() -> bool:
"""Return True if a separate shell is registered in the GIS environment"""
if get_shell_pid() is None:
return False
return True
return get_shell_pid() is not None


def parse_mapcalc_cmd(command):
Expand Down
7 changes: 2 additions & 5 deletions gui/wxpython/dbmgr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,12 +716,9 @@ def GetSortImages(self):
def OnGetItemImage(self, item):
return -1

def IsEmpty(self):
def IsEmpty(self) -> bool:
"""Check if list if empty"""
if self.columns:
return False

return True
return not self.columns

def _updateColSortFlag(self):
"""
Expand Down
21 changes: 6 additions & 15 deletions gui/wxpython/gmodeler/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,9 @@ def AddItem(self, newItem, pos=-1):
# item.SetId(i)
# i += 1

def IsValid(self):
def IsValid(self) -> bool:
"""Return True if model is valid"""
if self.Validate():
return False

return True
return not self.Validate()

def Validate(self):
"""Validate model, return None if model is valid otherwise
Expand Down Expand Up @@ -820,12 +817,9 @@ def Update(self):
for item in self.items:
item.Update()

def IsParameterized(self):
def IsParameterized(self) -> bool:
"""Return True if model is parameterized"""
if self.Parameterize():
return True

return False
return bool(self.Parameterize())

def Parameterize(self):
"""Return parameterized options"""
Expand Down Expand Up @@ -3785,9 +3779,6 @@ def GetErrors(self):

return errList

def DeleteIntermediateData(self):
def DeleteIntermediateData(self) -> bool:
"""Check if to detele intermediate data"""
if self.interData.IsShown() and self.interData.IsChecked():
return True

return False
return bool(self.interData.IsShown() and self.interData.IsChecked())
7 changes: 2 additions & 5 deletions gui/wxpython/gui_core/vselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def _onMapClickHandler(self, event):
if self._dialog:
self._dialog.Raise()

def AddVecInfo(self, vInfoDictTMP):
def AddVecInfo(self, vInfoDictTMP) -> bool:
"""Update vector in list
Note: click on features add category
Expand All @@ -232,10 +232,7 @@ def AddVecInfo(self, vInfoDictTMP):
if self._dialog:
self.slist.AddItem(vInfoDictTMP)

if len(self.selectedFeatures) == 0:
return False

return True
return not len(self.selectedFeatures) == 0

def _draw(self):
"""Call class 'VectorSelectHighlighter' to draw selected features"""
Expand Down
18 changes: 6 additions & 12 deletions gui/wxpython/iclass/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def CreateTempVector(self):

return vectorName

def RemoveTempVector(self):
def RemoveTempVector(self) -> bool:
"""Removes temporary vector map with training areas"""
ret = RunCommand(
prog="g.remove",
Expand All @@ -272,20 +272,16 @@ def RemoveTempVector(self):
type="vector",
name=self.trainingAreaVector,
)
if ret != 0:
return False
return True
return bool(ret == 0)

def RemoveTempRaster(self, raster):
def RemoveTempRaster(self, raster) -> bool:
"""Removes temporary raster maps"""
self.GetFirstMap().Clean()
self.GetSecondMap().Clean()
ret = RunCommand(
prog="g.remove", parent=self, flags="f", type="raster", name=raster
)
if ret != 0:
return False
return True
return bool(ret == 0)

def AddToolbar(self, name):
"""Add defined toolbar to the window
Expand Down Expand Up @@ -817,7 +813,7 @@ def OnExportAreas(self, event):
parent=self,
)

def ExportAreas(self, vectorName, withTable):
def ExportAreas(self, vectorName, withTable) -> bool:
"""Export training areas to new vector map (with attribute table).
:param str vectorName: name of exported vector map
Expand Down Expand Up @@ -930,9 +926,7 @@ def ExportAreas(self, vectorName, withTable):
)
wx.EndBusyCursor()
os.remove(dbFile.name)
if ret != 0:
return False
return True
return bool(ret == 0)

def _runDBUpdate(self, tmpFile, table, column, value, cat):
"""Helper function for UPDATE statement
Expand Down
7 changes: 2 additions & 5 deletions gui/wxpython/iscatt/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def _rendDtFilesToMemmaps(rend_dt):
del rend_dt[k]["sh"]


def _renderCat(cat_id, rend_dt, scatt, styles):
def _renderCat(cat_id, rend_dt, scatt, styles) -> bool:
return True

if cat_id not in rend_dt:
Expand All @@ -574,10 +574,7 @@ def _renderCat(cat_id, rend_dt, scatt, styles):
return False
if scatt["render"]:
return True
if cat_id != 0 and rend_dt[cat_id]["color"] != styles[cat_id]["color"]:
return True

return False
return bool(cat_id != 0 and rend_dt[cat_id]["color"] != styles[cat_id]["color"])


def _getColorMap(cat_id, styles):
Expand Down
6 changes: 2 additions & 4 deletions gui/wxpython/location_wizard/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,9 @@ def _nameValidationFailed(self, ctrl):
).format(ctrl.GetValue(), "/\"'@,=*~")
GError(parent=self, message=message, caption=_("Invalid name"))

def _checkLocationNotExists(self, text):
def _checkLocationNotExists(self, text) -> bool:
"""Check whether user's input location exists or not."""
if location_exists(self.tgisdbase.GetLabel(), text):
return False
return True
return not location_exists(self.tgisdbase.GetLabel(), text)

def _locationAlreadyExists(self, ctrl):
message = _(
Expand Down
7 changes: 2 additions & 5 deletions gui/wxpython/mapdisp/gprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ def OnEndPrinting(self):
def OnPreparePrinting(self):
super().OnPreparePrinting()

def HasPage(self, page):
if page <= 2:
return True
else:
return False
def HasPage(self, page) -> bool:
return page <= 2

def GetPageInfo(self):
return (1, 2, 1, 2)
Expand Down
6 changes: 2 additions & 4 deletions gui/wxpython/mapdisp/statusbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,14 @@ def GetProperty(self, name):
"""
return self.statusbarItems[name].GetValue()

def HasProperty(self, name):
def HasProperty(self, name) -> bool:
"""Checks whether property is represented by one of contained SbItems
:param name: name of SbItem (from name attribute)
:return: True if particular SbItem is contained, False otherwise
"""
if name in self.statusbarItems:
return True
return False
return name in self.statusbarItems

def AddStatusbarItem(self, item):
"""Adds item to statusbar"""
Expand Down
6 changes: 2 additions & 4 deletions gui/wxpython/mapswipe/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,8 @@ def GetValues(self):
else:
return (self._firstLayerList, self._secondLayerList)

def IsSimpleMode(self):
if self._switchSizer.IsShown(self._firstPanel):
return True
return False
def IsSimpleMode(self) -> bool:
return bool(self._switchSizer.IsShown(self._firstPanel))

def GetFirstSimpleLmgr(self):
return self._firstLmgr
Expand Down
Loading

0 comments on commit b096d86

Please sign in to comment.