From 4be87a11c8b23d0abce992b9c85afa3d387922e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Mon, 8 Jul 2024 21:41:32 -0400 Subject: [PATCH] style: Fix pylint try-except-raise (W0706) (#3997) * python(grass.temporal): Fix pylint try-except-raise (W0706) Do not catch exception only to raise it again * gui/wxpython/core/utils: Fix pylint try-except-raise (W0706) * python: Fix pylint try-except-raise (W0706) * style: Enable ruff useless-try-except (TRY302) as fixed --- gui/wxpython/core/utils.py | 19 ++++++-------- pyproject.toml | 1 - python/grass/imaging/images2swf.py | 2 -- .../grass/temporal/c_libraries_interface.py | 26 ------------------- python/grass/temporal/datetime_math.py | 10 ++----- 5 files changed, 10 insertions(+), 48 deletions(-) diff --git a/gui/wxpython/core/utils.py b/gui/wxpython/core/utils.py index 58faad45253..fd6803ecf74 100644 --- a/gui/wxpython/core/utils.py +++ b/gui/wxpython/core/utils.py @@ -572,17 +572,14 @@ def GetListOfLocations(dbase): """ listOfLocations = [] - try: - for location in glob.glob(os.path.join(dbase, "*")): - try: - if os.path.join(location, "PERMANENT") in glob.glob( - os.path.join(location, "*") - ): - listOfLocations.append(os.path.basename(location)) - except: - pass - except (UnicodeEncodeError, UnicodeDecodeError) as e: - raise e + for location in glob.glob(os.path.join(dbase, "*")): + try: + if os.path.join(location, "PERMANENT") in glob.glob( + os.path.join(location, "*") + ): + listOfLocations.append(os.path.basename(location)) + except: + pass ListSortLower(listOfLocations) diff --git a/pyproject.toml b/pyproject.toml index b4a3b84fa57..a193930b18c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -312,7 +312,6 @@ ignore = [ "TRY201", # verbose-raise "TRY300", # try-consider-else "TRY301", # raise-within-try - "TRY302", # useless-try-except "UP015", # redundant-open-modes "UP018", # native-literals "UP030", # format-literals diff --git a/python/grass/imaging/images2swf.py b/python/grass/imaging/images2swf.py index 72354e751a6..df6eb023c24 100644 --- a/python/grass/imaging/images2swf.py +++ b/python/grass/imaging/images2swf.py @@ -828,8 +828,6 @@ def writeSwf(filename, images, duration=0.1, repeat=True): fp = open(filename, "wb") try: buildFile(fp, taglist, nframes=nframes, framesize=wh, fps=fps) - except Exception: - raise finally: fp.close() diff --git a/python/grass/temporal/c_libraries_interface.py b/python/grass/temporal/c_libraries_interface.py index acd24f2a81b..6b67dfb9869 100644 --- a/python/grass/temporal/c_libraries_interface.py +++ b/python/grass/temporal/c_libraries_interface.py @@ -80,8 +80,6 @@ def _read_map_full_info(lock, conn, data): info = _read_raster_full_info(name, mapset) elif maptype == RPCDefs.TYPE_VECTOR: info = _read_vector_full_info(name, mapset) - except: - raise finally: conn.send(info) @@ -295,8 +293,6 @@ def _get_database_name(lock, conn, data): dbstring = dbstring.replace(encode("$GISDBASE"), libgis.G_gisdbase()) dbstring = dbstring.replace(encode("$LOCATION_NAME"), libgis.G_location()) dbstring = dbstring.replace(encode("$MAPSET"), mapset) - except: - raise finally: conn.send(decode(dbstring)) @@ -353,8 +349,6 @@ def _available_mapsets(lock, conn, data): mapset_list.reverse() mapset_list.append(current_mapset) mapset_list.reverse() - except: - raise finally: conn.send(mapset_list) @@ -387,8 +381,6 @@ def _has_timestamp(lock, conn, data): elif maptype == RPCDefs.TYPE_RASTER3D: if libgis.G_has_raster3d_timestamp(name, mapset) == 1: check = True - except: - raise finally: conn.send(check) @@ -437,8 +429,6 @@ def _read_timestamp(lock, conn, data): check = libgis.G_read_raster3d_timestamp(name, mapset, byref(ts)) dates = _convert_timestamp_from_grass(ts) - except: - raise finally: conn.send((check, dates)) @@ -483,8 +473,6 @@ def _write_timestamp(lock, conn, data): check = libgis.G_write_vector_timestamp(name, layer, byref(ts)) elif maptype == RPCDefs.TYPE_RASTER3D: check = libgis.G_write_raster3d_timestamp(name, byref(ts)) - except: - raise finally: conn.send(check) @@ -520,8 +508,6 @@ def _remove_timestamp(lock, conn, data): check = libgis.G_remove_vector_timestamp(name, layer, mapset) elif maptype == RPCDefs.TYPE_RASTER3D: check = libgis.G_remove_raster3d_timestamp(name, mapset) - except: - raise finally: conn.send(check) @@ -561,8 +547,6 @@ def _read_semantic_label(lock, conn, data): maptype=maptype, ) return -1 - except: - raise finally: conn.send(semantic_label) @@ -599,8 +583,6 @@ def _write_semantic_label(lock, conn, data): maptype=maptype, ) return -2 - except: - raise finally: conn.send(True) @@ -634,8 +616,6 @@ def _remove_semantic_label(lock, conn, data): maptype=maptype, ) return -2 - except: - raise finally: conn.send(check) @@ -668,8 +648,6 @@ def _map_exists(lock, conn, data): if mapset: check = True - except: - raise finally: conn.send(check) @@ -696,8 +674,6 @@ def _read_map_info(lock, conn, data): kvp = _read_vector_info(name, mapset) elif maptype == RPCDefs.TYPE_RASTER3D: kvp = _read_raster3d_info(name, mapset) - except: - raise finally: conn.send(kvp) @@ -1006,8 +982,6 @@ def _read_map_history(lock, conn, data): kvp = _read_vector_history(name, mapset) elif maptype == RPCDefs.TYPE_RASTER3D: kvp = _read_raster3d_history(name, mapset) - except: - raise finally: conn.send(kvp) diff --git a/python/grass/temporal/datetime_math.py b/python/grass/temporal/datetime_math.py index 9924284ed9b..7ed3c7fb8f5 100644 --- a/python/grass/temporal/datetime_math.py +++ b/python/grass/temporal/datetime_math.py @@ -323,10 +323,7 @@ def modify_datetime( if residual_months == 0: residual_months = 1 - try: - dt1 = dt1.replace(year=year + years_to_add, month=residual_months) - except: - raise + dt1 = dt1.replace(year=year + years_to_add, month=residual_months) tdelta_months = dt1 - mydate elif months < 0: @@ -350,10 +347,7 @@ def modify_datetime( if residual_months <= 0: residual_months += 12 - try: - dt1 = dt1.replace(year=year - years_to_remove, month=residual_months) - except: - raise + dt1 = dt1.replace(year=year - years_to_remove, month=residual_months) tdelta_months = dt1 - mydate