From 7e49a2b78c2a40baf3cd5240742792f868b45f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Thu, 2 Jan 2025 01:17:38 -0500 Subject: [PATCH] style: Fix R1716: Simplify chained comparison between the operands (chained-comparison) (#4902) * style: Fix R1716: Simplify chained comparison between the operands (chained-comparison) Pylint rule: https://pylint.readthedocs.io/en/stable/user_guide/messages/refactor/chained-comparison.html * gui: Fix remaining Pylint R1716 issues --- display/d.text/test.py | 2 +- gui/wxpython/core/debug.py | 2 +- gui/wxpython/core/gconsole.py | 2 +- gui/wxpython/core/utils.py | 6 +---- gui/wxpython/mapwin/buffered.py | 6 ++--- gui/wxpython/mapwin/graphics.py | 2 +- gui/wxpython/vdigit/wxdisplay.py | 4 ++-- gui/wxpython/vnet/vnet_data.py | 2 +- gui/wxpython/vnet/widgets.py | 2 +- pyproject.toml | 1 - python/grass/imaging/images2ims.py | 2 +- python/grass/imaging/images2swf.py | 2 +- python/grass/temporal/spatial_extent.py | 24 +++++++++---------- python/grass/temporal/temporal_algebra.py | 2 +- .../temporal/temporal_raster3d_algebra.py | 2 +- .../grass/temporal/temporal_raster_algebra.py | 2 +- temporal/t.rast.accdetect/t.rast.accdetect.py | 4 ++-- 17 files changed, 31 insertions(+), 36 deletions(-) diff --git a/display/d.text/test.py b/display/d.text/test.py index 398cfecf428..dd8836938a5 100755 --- a/display/d.text/test.py +++ b/display/d.text/test.py @@ -52,7 +52,7 @@ def text(in_text): for i in range(36): font(fonts[int(i % len(fonts))]) - size((36 - i if ((i >= 9 and i <= 18) or i > 27) else i) % 9) + size((36 - i if ((9 <= i <= 18) or i > 27) else i) % 9) rotate(i * 10) color(colors[i % len(colors)]) xy( diff --git a/gui/wxpython/core/debug.py b/gui/wxpython/core/debug.py index b6c12a60b62..bcb33f4d1da 100644 --- a/gui/wxpython/core/debug.py +++ b/gui/wxpython/core/debug.py @@ -61,7 +61,7 @@ def msg(self, level, message, *args): :param args: formatting params """ # self.SetLevel() - if self.debuglevel > 0 and level > 0 and level <= self.debuglevel: + if 0 < level <= self.debuglevel: if args: sys.stderr.write( "GUI D%d/%d: " % (level, self.debuglevel) diff --git a/gui/wxpython/core/gconsole.py b/gui/wxpython/core/gconsole.py index cde41710cd9..d5fc21c5b0d 100644 --- a/gui/wxpython/core/gconsole.py +++ b/gui/wxpython/core/gconsole.py @@ -316,7 +316,7 @@ def write(self, s): if "GRASS_INFO_PERCENT" in line: value = int(line.rsplit(":", 1)[1].strip()) - progressValue = value if value >= 0 and value < 100 else 0 + progressValue = value if 0 <= value < 100 else 0 elif "GRASS_INFO_MESSAGE" in line: self.type = "message" self.message += line.split(":", 1)[1].strip() + "\n" diff --git a/gui/wxpython/core/utils.py b/gui/wxpython/core/utils.py index 77f8b5a3c5b..2a7ac31c36f 100644 --- a/gui/wxpython/core/utils.py +++ b/gui/wxpython/core/utils.py @@ -217,11 +217,7 @@ def GetValidLayerName(name): cIdx = 0 retNameList = list(retName) for c in retNameList: - if ( - not (c >= "A" and c <= "Z") - and not (c >= "a" and c <= "z") - and not (c >= "0" and c <= "9") - ): + if not ("A" <= c <= "Z") and not ("a" <= c <= "z") and not ("0" <= c <= "9"): retNameList[cIdx] = "_" cIdx += 1 retName = "".join(retNameList) diff --git a/gui/wxpython/mapwin/buffered.py b/gui/wxpython/mapwin/buffered.py index e30bf99414c..1de86864e61 100644 --- a/gui/wxpython/mapwin/buffered.py +++ b/gui/wxpython/mapwin/buffered.py @@ -555,14 +555,14 @@ def TextBounds(self, textinfo, relcoords=False): boxh = math.fabs(math.sin(math.radians(rotation)) * w) + h boxw = math.fabs(math.cos(math.radians(rotation)) * w) + h - if rotation > 0 and rotation < 90: + if 0 < rotation < 90: bbox[1] -= boxh relCoords = (0, boxh) - elif rotation >= 90 and rotation < 180: + elif 90 <= rotation < 180: bbox[0] -= boxw bbox[1] -= boxh relCoords = (boxw, boxh) - elif rotation >= 180 and rotation < 270: + elif 180 <= rotation < 270: bbox[0] -= boxw relCoords = (boxw, 0) bbox[2] = boxw diff --git a/gui/wxpython/mapwin/graphics.py b/gui/wxpython/mapwin/graphics.py index b0cb7f36d6b..3bfe85f0834 100644 --- a/gui/wxpython/mapwin/graphics.py +++ b/gui/wxpython/mapwin/graphics.py @@ -363,7 +363,7 @@ def SetItemDrawOrder(self, item, drawNum): :return: True if order was changed :return: False if drawNum is out of range or item was not found """ - if drawNum < len(self.itemsList) and drawNum >= 0 and item in self.itemsList: + if 0 <= drawNum < len(self.itemsList) and item in self.itemsList: self.itemsList.insert( drawNum, self.itemsList.pop(self.itemsList.index(item)) ) diff --git a/gui/wxpython/vdigit/wxdisplay.py b/gui/wxpython/vdigit/wxdisplay.py index c52e011e3e2..6679e5c5814 100644 --- a/gui/wxpython/vdigit/wxdisplay.py +++ b/gui/wxpython/vdigit/wxdisplay.py @@ -563,7 +563,7 @@ def _validLine(self, line) -> bool: :return: True valid feature id :return: False invalid """ - return bool(line > 0 and line <= Vect_get_num_lines(self.poMapInfo)) + return bool(0 < line <= Vect_get_num_lines(self.poMapInfo)) def SelectLinesByBox(self, bbox, ltype=None, drawSeg=False, poMapInfo=None): """Select vector objects by given bounding box @@ -913,7 +913,7 @@ def GetRegionSelected(self): for line in self.selected["ids"]: area = Vect_get_centroid_area(self.poMapInfo, line) - if area > 0 and area <= nareas: + if 0 < area <= nareas: if not Vect_get_area_box(self.poMapInfo, area, byref(lineBox)): continue else: # noqa: PLR5501 diff --git a/gui/wxpython/vnet/vnet_data.py b/gui/wxpython/vnet/vnet_data.py index 47ffdb9a371..778f4cf50b1 100644 --- a/gui/wxpython/vnet/vnet_data.py +++ b/gui/wxpython/vnet/vnet_data.py @@ -1450,4 +1450,4 @@ def IsInInterval(self, from_angle, to_angle, angle) -> bool: if angle < from_angle: angle = math.pi * 2 + angle - return bool(angle > from_angle and angle < to_angle) + return bool(from_angle < angle < to_angle) diff --git a/gui/wxpython/vnet/widgets.py b/gui/wxpython/vnet/widgets.py index d997b9c2434..d8926f96fae 100644 --- a/gui/wxpython/vnet/widgets.py +++ b/gui/wxpython/vnet/widgets.py @@ -511,7 +511,7 @@ def ShowColumn(self, colName, pos): :return: True if column was shown :return: False if position is not valid or column is not hidden """ - if pos < 0 and pos >= self.self.GetColumnCount(): + if pos < 0 or pos >= self.GetColumnCount(): return False if colName in self.hiddenCols: col = self.hiddenCols[colName] diff --git a/pyproject.toml b/pyproject.toml index 98bd80243f9..6acab49eca2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -639,7 +639,6 @@ disable = [ "R1713", # Consider using str.join(sequence) for concatenating strings from an iterable (consider-using-join) "R1714", # Consider merging these comparisons with 'in' by using '%s %sin (%s)'. Use a set instead if elements are hashable. (consider-using-in) "R1715", # Consider using dict.get for getting values from a dict if a key is present or a default if not (consider-using-get) - "R1716", # Simplify chained comparison between the operands (chained-comparison) "R1724", # Unnecessary "%s" after "continue", %s (no-else-continue) "R1727", # Boolean condition '%s' will always evaluate to '%s' (condition-evals-to-constant) "R1732", # Consider using 'with' for resource-allocating operations (consider-using-with) diff --git a/python/grass/imaging/images2ims.py b/python/grass/imaging/images2ims.py index d3bbc12afe0..b748ad975db 100644 --- a/python/grass/imaging/images2ims.py +++ b/python/grass/imaging/images2ims.py @@ -65,7 +65,7 @@ def checkImages(images): images2.append(im) # Ok elif im.dtype in [np.float32, np.float64]: theMax = im.max() - if theMax > 128 and theMax < 300: + if 128 < theMax < 300: pass # assume 0:255 else: im = im.copy() diff --git a/python/grass/imaging/images2swf.py b/python/grass/imaging/images2swf.py index d8f93fdfc32..858f9e819e5 100644 --- a/python/grass/imaging/images2swf.py +++ b/python/grass/imaging/images2swf.py @@ -110,7 +110,7 @@ def checkImages(images): images2.append(im) # Ok elif im.dtype in [np.float32, np.float64]: theMax = im.max() - if theMax > 128 and theMax < 300: + if 128 < theMax < 300: pass # assume 0:255 else: im = im.copy() diff --git a/python/grass/temporal/spatial_extent.py b/python/grass/temporal/spatial_extent.py index 3a76b18ab46..8154a02805b 100644 --- a/python/grass/temporal/spatial_extent.py +++ b/python/grass/temporal/spatial_extent.py @@ -868,13 +868,13 @@ def cover_2d(self, extent) -> bool: # We check that at least one edge of extent is located in self edge_count = 0 - if eW > W and eW < E: + if W < eW < E: edge_count += 1 - if eE < E and eE > W: + if W < eE < E: edge_count += 1 - if eN < N and eN > S: + if S < eN < N: edge_count += 1 - if eS > S and eS < N: + if S < eS < N: edge_count += 1 return edge_count != 0 @@ -941,21 +941,21 @@ def cover(self, extent) -> bool: # We check that at least one edge of extent is located in self edge_count = 0 - if eW > W and eW < E: + if W < eW < E: edge_count += 1 - if eE < E and eE > W: + if W < eE < E: edge_count += 1 - if eN < N and eN > S: + if S < eN < N: edge_count += 1 - if eS > S and eS < N: + if S < eS < N: edge_count += 1 - if eN < N and eN > S: + if S < eN < N: edge_count += 1 - if eS > S and eS < N: + if S < eS < N: edge_count += 1 - if eT < T and eT > B: + if B < eT < T: edge_count += 1 - if eB > B and eB < T: + if B < eB < T: edge_count += 1 return edge_count != 0 diff --git a/python/grass/temporal/temporal_algebra.py b/python/grass/temporal/temporal_algebra.py index 3ddb2cbe223..e5dd737ff4e 100644 --- a/python/grass/temporal/temporal_algebra.py +++ b/python/grass/temporal/temporal_algebra.py @@ -3371,7 +3371,7 @@ def p_expr_time_const(self, t) -> None: # Get map index and temporal extent. map_index = map_list.index(map_i) new_index = map_index + t_neighbour - if new_index < max_index and new_index >= 0: + if 0 <= new_index < max_index: # Get neighbouring map and set temporal extent. map_n = map_list[new_index] map_i_t_extent = map_i.get_temporal_extent() diff --git a/python/grass/temporal/temporal_raster3d_algebra.py b/python/grass/temporal/temporal_raster3d_algebra.py index 93817952fff..fdf80661429 100644 --- a/python/grass/temporal/temporal_raster3d_algebra.py +++ b/python/grass/temporal/temporal_raster3d_algebra.py @@ -124,7 +124,7 @@ def p_ts_neighbor_operation(self, t) -> None: # Get map index and temporal extent. map_index = maplist.index(map_i) new_index = map_index + t_neighbor - if new_index < max_index and new_index >= 0: + if 0 <= new_index < max_index: map_i_t_extent = map_i.get_temporal_extent() # Get neighboring map and set temporal extent. map_n = maplist[new_index] diff --git a/python/grass/temporal/temporal_raster_algebra.py b/python/grass/temporal/temporal_raster_algebra.py index 5b49da5841a..053c768ae73 100644 --- a/python/grass/temporal/temporal_raster_algebra.py +++ b/python/grass/temporal/temporal_raster_algebra.py @@ -168,7 +168,7 @@ def p_ts_neighbour_operation(self, t) -> None: # Get map index and temporal extent. map_index = maplist.index(map_i) new_index = map_index + t_neighbour - if new_index < max_index and new_index >= 0: + if 0 <= new_index < max_index: map_i_t_extent = map_i.get_temporal_extent() # Get neighbouring map and set temporal extent. map_n = maplist[new_index] diff --git a/temporal/t.rast.accdetect/t.rast.accdetect.py b/temporal/t.rast.accdetect/t.rast.accdetect.py index 17433e06f3f..8d70bf255f2 100644 --- a/temporal/t.rast.accdetect/t.rast.accdetect.py +++ b/temporal/t.rast.accdetect/t.rast.accdetect.py @@ -402,7 +402,7 @@ def main(): prev_map = curr_map subexpr1 = "null()" subexpr3 = "%i" % (indicator_start) - elif i > 0 and i < num_maps - 1: + elif 0 < i < num_maps - 1: prev_map = occurrence_maps[map.next().get_id()].get_name() next_map = occurrence_maps[map.prev().get_id()].get_name() # In case the previous map is null() set null() or the start @@ -444,7 +444,7 @@ def main(): prev_map = curr_map subexpr1 = "null()" subexpr3 = "%i" % (indicator_start) - elif i > 0 and i < num_maps - 1: + elif 0 < i < num_maps - 1: prev_map = occurrence_maps[map.prev().get_id()].get_name() next_map = occurrence_maps[map.next().get_id()].get_name() # In case the previous map is null() set null() or the start