From 87dc9a738b3f4b6f538289ca78854cc71d02c24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sun, 26 Oct 2025 22:00:50 +0000 Subject: [PATCH 1/4] CQ: Fix Ruff PTH113: `os.path.isfile()` should be replaced by `Path.is_file()` Ruff rule: https://docs.astral.sh/ruff/rules/os-path-isfile/ --- gui/wxpython/animation/dialogs.py | 2 +- gui/wxpython/core/render.py | 25 +++++++------ gui/wxpython/core/utils.py | 5 +-- gui/wxpython/gcp/manager.py | 4 +-- gui/wxpython/gui_core/forms.py | 2 +- gui/wxpython/gui_core/ghelp.py | 4 +-- gui/wxpython/gui_core/toolbars.py | 2 +- gui/wxpython/icons/icon.py | 2 +- gui/wxpython/image2target/ii2t_gis_set.py | 16 +++++---- gui/wxpython/image2target/ii2t_manager.py | 8 ++--- gui/wxpython/location_wizard/wizard.py | 13 +++---- gui/wxpython/mapwin/buffered.py | 4 +-- gui/wxpython/modules/colorrules.py | 2 +- gui/wxpython/modules/histogram.py | 2 +- gui/wxpython/photo2image/ip2i_manager.py | 2 +- gui/wxpython/web_services/dialogs.py | 3 +- imagery/i.cluster/testsuite/test_i_cluster.py | 4 +-- .../i.svm.train/testsuite/test_i_svm_train.py | 36 +++++++++---------- .../test_imagery_signature_management.py | 36 +++++++++---------- lib/init/grass.py | 4 +-- pyproject.toml | 1 - python/grass/gunittest/case.py | 2 +- python/grass/gunittest/reporters.py | 2 +- python/grass/imaging/images2avi.py | 6 ++-- python/grass/imaging/images2gif.py | 4 +-- python/grass/imaging/images2ims.py | 6 ++-- python/grass/imaging/images2swf.py | 3 +- python/grass/script/core.py | 2 +- python/grass/temporal/stds_import.py | 2 +- raster/r.contour/testsuite/test_r_contour.py | 28 +++++++-------- .../testsuite/test_r_object_geometry.py | 8 ++--- raster/r.report/testsuite/test_r_report.py | 5 ++- raster/r.topidx/arc_to_gridatb.py | 6 ++-- raster/r.topidx/gridatb_to_arc.py | 6 ++-- raster/r.what/testsuite/test_r_what.py | 12 +++---- scripts/d.frame/d.frame.py | 3 +- scripts/d.polar/testsuite/test_d_polar.py | 7 ++-- scripts/g.extension/g.extension.py | 2 +- scripts/g.search.modules/g.search.modules.py | 5 +-- scripts/r.in.srtm/r.in.srtm.py | 4 +-- scripts/r.out.xyz/testsuite/test_r_out_xyz.py | 7 ++-- scripts/r.pack/r.pack.py | 7 ++-- scripts/r.pack/testsuite/test_r_pack.py | 6 ++-- scripts/v.db.addcolumn/v.db.addcolumn.py | 6 ++-- scripts/v.import/v.import.py | 3 +- scripts/v.in.mapgen/v.in.mapgen.py | 3 +- scripts/v.in.wfs/v.in.wfs.py | 4 +-- scripts/v.pack/testsuite/test_v_pack.py | 6 ++-- scripts/v.unpack/testsuite/test_v_unpack.py | 6 ++-- .../testsuite/test_distr_tgis_db_raster.py | 12 +++---- .../testsuite/test_distr_tgis_db_raster3d.py | 12 +++---- .../testsuite/test_distr_tgis_db_vector.py | 12 +++---- .../v.db.select/testsuite/test_v_db_select.py | 5 ++- .../v.in.lidar/testsuite/decimation_test.py | 5 ++- vector/v.in.lidar/testsuite/mask_test.py | 6 ++-- .../testsuite/test_v_in_lidar_basic.py | 5 ++- .../testsuite/test_v_in_lidar_filter.py | 5 ++- .../testsuite/test_v_in_pdal_basic.py | 7 ++-- .../testsuite/test_v_in_pdal_filter.py | 8 ++--- .../v.out.lidar/testsuite/test_v_out_lidar.py | 5 ++- vector/v.profile/testsuite/test_v_profile.py | 5 ++- 61 files changed, 202 insertions(+), 223 deletions(-) diff --git a/gui/wxpython/animation/dialogs.py b/gui/wxpython/animation/dialogs.py index 36704f13f64..483e328506e 100644 --- a/gui/wxpython/animation/dialogs.py +++ b/gui/wxpython/animation/dialogs.py @@ -1660,7 +1660,7 @@ def __init__( self.tselect = Select(parent=self, type="strds") iconTheme = UserSettings.Get(group="appearance", key="iconTheme", subkey="type") bitmapPath = os.path.join(globalvar.ICONDIR, iconTheme, "layer-open.png") - if os.path.isfile(bitmapPath) and Path(bitmapPath).stat().st_size: + if Path(bitmapPath).is_file() and Path(bitmapPath).stat().st_size: bitmap = wx.Bitmap(name=bitmapPath) else: bitmap = wx.ArtProvider.GetBitmap( diff --git a/gui/wxpython/core/render.py b/gui/wxpython/core/render.py index d20c051bb44..b75b678d504 100644 --- a/gui/wxpython/core/render.py +++ b/gui/wxpython/core/render.py @@ -57,7 +57,7 @@ def get_tempfile_name(suffix, create=False): if not create: # remove empty file to have a clean state later - os.remove(name) + Path(name).unlink() return name @@ -428,8 +428,8 @@ def Render(self, cmd, env): if self.layer.GetType() in {"vector", "thememap"}: if not self.layer._legrow: self.layer._legrow = grass.tempfile(create=True) - if os.path.isfile(self.layer._legrow): - os.remove(self.layer._legrow) + if (p := Path(self.layer._legrow)).is_file(): + p.unlink() env_cmd["GRASS_LEGEND_FILE"] = text_to_string(self.layer._legrow) cmd_render = copy.deepcopy(cmd) @@ -531,8 +531,8 @@ def _init(self): self.layers = [] # re-render from scratch - if Path(self.Map.mapfile).exists(): - os.remove(self.Map.mapfile) + if (p := Path(self.Map.mapfile)).is_file(): + p.unlink() def _checkRenderedSizes(self, env, layers): """Check if rendering size in current env differs from size of @@ -663,7 +663,7 @@ def OnRenderDone(self, env): if layer.GetType() == "overlay": continue - if os.path.isfile(layer.mapfile): + if Path(layer.mapfile).is_file(): maps.append(layer.mapfile) masks.append(layer.maskfile) opacities.append(str(layer.opacity)) @@ -707,8 +707,8 @@ def OnRenderDone(self, env): if layer.GetType() not in {"vector", "thememap"}: continue - if os.path.isfile(layer._legrow) and not layer.hidden: - line = Path(layer._legrow).read_text() + if (p := Path(layer._legrow)).is_file() and not layer.hidden: + line = p.read_text() outfile.write(line) new_legend.append(line) @@ -1414,11 +1414,10 @@ def DeleteLayer(self, layer, overlay=False): # it as it is, although it does not really fit with the # new system (but probably works well enough) for f in glob.glob(basefile): - os.remove(f) + Path(f).unlink() if layer.GetType() in {"vector", "thememap"}: - if os.path.isfile(layer._legrow): - os.remove(layer._legrow) + Path(layer._legrow).unlink(missing_ok=True) list_.remove(layer) @@ -1534,8 +1533,8 @@ def RemoveLayer(self, name=None, id=None): for layer in self.layers: if layer.name == name: retlayer = layer - os.remove(layer.mapfile) - os.remove(layer.maskfile) + Path(layer.mapfile).unlink() + Path(layer.maskfile).unlink() self.layers.remove(layer) return retlayer # del by id diff --git a/gui/wxpython/core/utils.py b/gui/wxpython/core/utils.py index 32f40289a00..c9473febb2f 100644 --- a/gui/wxpython/core/utils.py +++ b/gui/wxpython/core/utils.py @@ -611,8 +611,9 @@ def GetListOfMapsets(dbase, location, selectable=False): listOfMapsets += line.split(" ") else: for mapset in glob.glob(os.path.join(dbase, location, "*")): - if os.path.isdir(mapset) and os.path.isfile( - os.path.join(dbase, location, mapset, "WIND") + if ( + Path(mapset).is_dir() + and Path(os.path.join(dbase, location, mapset, "WIND")).is_file() ): listOfMapsets.append(os.path.basename(mapset)) diff --git a/gui/wxpython/gcp/manager.py b/gui/wxpython/gcp/manager.py index e151f09e3c2..3b58674d7eb 100644 --- a/gui/wxpython/gcp/manager.py +++ b/gui/wxpython/gcp/manager.py @@ -2411,7 +2411,7 @@ def LoadData(self): self.DeleteAllItems() self.render = False - if os.path.isfile(self.gcp.file["points"]): + if Path(self.gcp.file["points"]).is_file(): self.gcp.ReadGCPs() else: # 3 gcp is minimum @@ -2636,7 +2636,7 @@ def __init__( # self.listMap = CheckListBox(parent=self, id=wx.ID_ANY, choices=vectlist) - if os.path.isfile(self.vgrpfile): + if Path(self.vgrpfile).is_file(): with open(self.vgrpfile) as f: checked = [] for line in f: diff --git a/gui/wxpython/gui_core/forms.py b/gui/wxpython/gui_core/forms.py index 7adcc1a9c54..707a6f652e4 100644 --- a/gui/wxpython/gui_core/forms.py +++ b/gui/wxpython/gui_core/forms.py @@ -2033,7 +2033,7 @@ def __init__(self, parent, giface, task, id=wx.ID_ANY, frame=None, *args, **kwar style=wx.TE_MULTILINE, size=(-1, 75), ) - if p.get("value", "") and os.path.isfile(p["value"]): + if p.get("value", "") and Path(p["value"]).is_file(): ifbb.Clear() try: # Python >= 3.11 diff --git a/gui/wxpython/gui_core/ghelp.py b/gui/wxpython/gui_core/ghelp.py index dea3a156e2a..6ab7e000909 100644 --- a/gui/wxpython/gui_core/ghelp.py +++ b/gui/wxpython/gui_core/ghelp.py @@ -865,14 +865,14 @@ def LoadPage(self, path=None): def GetFile(self): """Get HTML file""" fMan = os.path.join(self.content.fspath, self.command + ".html") - if os.path.isfile(fMan): + if Path(fMan).is_file(): return fMan # check also addons faMan = os.path.join( os.getenv("GRASS_ADDON_BASE"), "docs", "html", self.command + ".html" ) - if os.getenv("GRASS_ADDON_BASE") and os.path.isfile(faMan): + if os.getenv("GRASS_ADDON_BASE") and Path(faMan).is_file(): return faMan return None diff --git a/gui/wxpython/gui_core/toolbars.py b/gui/wxpython/gui_core/toolbars.py index 61dd1cd4873..939727f2c25 100644 --- a/gui/wxpython/gui_core/toolbars.py +++ b/gui/wxpython/gui_core/toolbars.py @@ -282,7 +282,7 @@ def CreateSelectionButton(self, tooltip): Button must be custom (not toolbar tool) to set smaller width. """ arrowPath = os.path.join(IMGDIR, "small_down_arrow.png") - if os.path.isfile(arrowPath) and Path(arrowPath).stat().st_size: + if Path(arrowPath).is_file() and Path(arrowPath).stat().st_size: bitmap = wx.Bitmap(name=arrowPath) else: bitmap = wx.ArtProvider.GetBitmap( diff --git a/gui/wxpython/icons/icon.py b/gui/wxpython/icons/icon.py index 49cc30719dc..99f00e42771 100644 --- a/gui/wxpython/icons/icon.py +++ b/gui/wxpython/icons/icon.py @@ -81,7 +81,7 @@ def GetBitmap(self, size=None): id=self.imagepath, client=wx.ART_TOOLBAR, size=size ) elif self.type == "img": - if os.path.isfile(self.imagepath) and Path(self.imagepath).stat().st_size: + if Path(self.imagepath).is_file() and Path(self.imagepath).stat().st_size: if size and len(size) == 2: image = wx.Image(name=self.imagepath) image.Rescale(size[0], size[1]) diff --git a/gui/wxpython/image2target/ii2t_gis_set.py b/gui/wxpython/image2target/ii2t_gis_set.py index 37248e06141..eae5b0452a9 100644 --- a/gui/wxpython/image2target/ii2t_gis_set.py +++ b/gui/wxpython/image2target/ii2t_gis_set.py @@ -539,8 +539,8 @@ def _readGisRC(self): gisrc = os.getenv("GISRC") - if gisrc and os.path.isfile(gisrc): - with open(gisrc) as rc: + if gisrc and (gisrc_path := Path(gisrc)).is_file(): + with gisrc_path.open() as rc: for line in rc: try: key, val = line.split(":", 1) @@ -920,8 +920,9 @@ def UpdateMapsets(self, location): disabled = [] idx = 0 for mapset in self.listOfMapsets: - if mapset not in self.listOfMapsetsSelectable or os.path.isfile( - os.path.join(self.gisdbase, locationName, mapset, ".gislock") + if ( + mapset not in self.listOfMapsetsSelectable + or Path(self.gisdbase, locationName, mapset, ".gislock").is_file() ): disabled.append(idx) idx += 1 @@ -952,8 +953,9 @@ def OnSelectLocation(self, event): locationName = "" for mapset in self.listOfMapsets: - if mapset not in self.listOfMapsetsSelectable or os.path.isfile( - os.path.join(self.gisdbase, locationName, mapset, ".gislock") + if ( + mapset not in self.listOfMapsetsSelectable + or Path(self.gisdbase, locationName, mapset, ".gislock").is_file() ): disabled.append(idx) idx += 1 @@ -1093,7 +1095,7 @@ def OnStart(self, event): mapset = self.listOfMapsets[self.lbmapsets.GetSelection()] lockfile = os.path.join(dbase, location, mapset, ".gislock") - if os.path.isfile(lockfile): + if Path(lockfile).is_file(): dlg = wx.MessageDialog( parent=self, message=_( diff --git a/gui/wxpython/image2target/ii2t_manager.py b/gui/wxpython/image2target/ii2t_manager.py index 1c86785323e..b2dd81c9ef3 100644 --- a/gui/wxpython/image2target/ii2t_manager.py +++ b/gui/wxpython/image2target/ii2t_manager.py @@ -2356,7 +2356,7 @@ def LoadData(self): self.DeleteAllItems() self.render = False - if os.path.isfile(self.gcp.file["control_points"]): + if Path(self.gcp.file["control_points"]).is_file(): self.gcp.ReadGCPs() else: # 3 gcp is minimum @@ -2559,7 +2559,7 @@ def __init__( ).iterdir() ] for dir in vectlist: - if not os.path.isfile( + if not Path( os.path.join( self.grassdatabase, self.xylocation, @@ -2568,7 +2568,7 @@ def __init__( dir, "coor", ) - ): + ).is_file(): vectlist.remove(dir) utils.ListSortLower(vectlist) @@ -2595,7 +2595,7 @@ def __init__( # self.listMap = CheckListBox(parent=self, id=wx.ID_ANY, choices=vectlist) - if os.path.isfile(self.vgrpfile): + if Path(self.vgrpfile).is_file(): with open(self.vgrpfile) as f: checked = [] for line in f: diff --git a/gui/wxpython/location_wizard/wizard.py b/gui/wxpython/location_wizard/wizard.py index 331029b4ede..240e5285322 100644 --- a/gui/wxpython/location_wizard/wizard.py +++ b/gui/wxpython/location_wizard/wizard.py @@ -1479,7 +1479,7 @@ def OnEnterPage(self, event: WizardEvent) -> None: event.Skip() def OnPageChanging(self, event: WizardEvent) -> None: - if event.GetDirection() and not os.path.isfile(self.georeffile): + if event.GetDirection() and not Path(self.georeffile).is_file(): event.Veto() self.GetNext().SetPrev(self) @@ -1487,7 +1487,7 @@ def OnText(self, event): """File changed""" self.georeffile = event.GetString() nextButton = wx.FindWindowById(wx.ID_FORWARD) - if len(self.georeffile) > 0 and os.path.isfile(self.georeffile): + if len(self.georeffile) > 0 and Path(self.georeffile).is_file(): if not nextButton.IsEnabled(): nextButton.Enable(True) elif nextButton.IsEnabled(): @@ -2644,7 +2644,7 @@ def OnWizFinished(self): location = self.startpage.location # location already exists? - if os.path.isdir(os.path.join(database, location)): + if Path(os.path.join(database, location)).is_dir(): GError( parent=self.wizard, message="%s <%s>: %s" @@ -2660,7 +2660,7 @@ def OnWizFinished(self): current_gdb = decode(grass.gisenv()["GISDBASE"]) if current_gdb != database: # change to new GISDbase or create new one - if not os.path.isdir(database): + if not Path(database).is_dir(): # create new directory try: os.mkdir(database) @@ -2730,8 +2730,9 @@ def OnWizFinished(self): desc=self.startpage.locTitle, ) elif coordsys == "file": - if not self.filepage.georeffile or not os.path.isfile( - self.filepage.georeffile + if ( + not self.filepage.georeffile + or not Path(self.filepage.georeffile).is_file() ): return _("File <%s> not found.") % self.filepage.georeffile diff --git a/gui/wxpython/mapwin/buffered.py b/gui/wxpython/mapwin/buffered.py index 56ca776c21b..b24fffc721b 100644 --- a/gui/wxpython/mapwin/buffered.py +++ b/gui/wxpython/mapwin/buffered.py @@ -799,7 +799,7 @@ def GetOverlay(self): for overlay in self.Map.GetListOfLayers(ltype="overlay", active=True): if ( overlay.mapfile is None - or not os.path.isfile(overlay.mapfile) + or not Path(overlay.mapfile).is_file() or (not Path(overlay.mapfile).stat().st_size) ): continue @@ -824,7 +824,7 @@ def GetImage(self): imgId = 99 if ( self.Map.mapfile - and os.path.isfile(self.Map.mapfile) + and Path(self.Map.mapfile).is_file() and Path(self.Map.mapfile).stat().st_size ): img = wx.Image(self.Map.mapfile, wx.BITMAP_TYPE_ANY) diff --git a/gui/wxpython/modules/colorrules.py b/gui/wxpython/modules/colorrules.py index a372a950d64..d505b6fbd44 100644 --- a/gui/wxpython/modules/colorrules.py +++ b/gui/wxpython/modules/colorrules.py @@ -2050,7 +2050,7 @@ def GetImage(self): """Converts files to wx.Image""" if ( self.Map.mapfile - and os.path.isfile(self.Map.mapfile) + and Path(self.Map.mapfile).is_file() and Path(self.Map.mapfile).stat().st_size ): img = wx.Image(self.Map.mapfile, wx.BITMAP_TYPE_ANY) diff --git a/gui/wxpython/modules/histogram.py b/gui/wxpython/modules/histogram.py index 90d2a88dc3f..a5dec29d0e7 100644 --- a/gui/wxpython/modules/histogram.py +++ b/gui/wxpython/modules/histogram.py @@ -213,7 +213,7 @@ def GetImage(self): """Converts files to wx.Image""" if ( self.Map.mapfile - and os.path.isfile(self.Map.mapfile) + and Path(self.Map.mapfile).is_file() and Path(self.Map.mapfile).stat().st_size ): img = wx.Image(self.Map.mapfile, wx.BITMAP_TYPE_ANY) diff --git a/gui/wxpython/photo2image/ip2i_manager.py b/gui/wxpython/photo2image/ip2i_manager.py index 11aeaad9613..84a0f15835c 100644 --- a/gui/wxpython/photo2image/ip2i_manager.py +++ b/gui/wxpython/photo2image/ip2i_manager.py @@ -1670,7 +1670,7 @@ def LoadData(self): self.DeleteAllItems() self.render = False - if os.path.isfile(self.gcp.file["points"]): + if Path(self.gcp.file["points"]).is_file(): self.gcp.ReadGCPs() else: # 3 gcp is minimum diff --git a/gui/wxpython/web_services/dialogs.py b/gui/wxpython/web_services/dialogs.py index 1240ffb88b6..34acb0f6754 100644 --- a/gui/wxpython/web_services/dialogs.py +++ b/gui/wxpython/web_services/dialogs.py @@ -24,6 +24,7 @@ import shutil from copy import deepcopy +from pathlib import Path import grass.script as gs from grass.script.task import cmdlist_to_tuple, cmdtuple_to_list @@ -676,7 +677,7 @@ def __del__(self): def _setRevertCapFiles(self, ws_cap_files): for ws, f in ws_cap_files.items(): - if os.path.isfile(f): + if Path(f).is_file(): shutil.copyfile(f, self.revert_ws_cap_files[ws]) else: # delete file content diff --git a/imagery/i.cluster/testsuite/test_i_cluster.py b/imagery/i.cluster/testsuite/test_i_cluster.py index 779286160f6..aeab5241afe 100644 --- a/imagery/i.cluster/testsuite/test_i_cluster.py +++ b/imagery/i.cluster/testsuite/test_i_cluster.py @@ -63,7 +63,7 @@ def _signature_exists(self): "sig", "sig", ) - return os.path.isfile(sig_file_path) + return Path(sig_file_path).is_file() def test_cluster_creation(self): """Test the creation of a signature file and validate the number of classes.""" @@ -109,7 +109,7 @@ def test_sample_interval(self): sample=sample_interval, overwrite=True, ) - self.assertTrue(os.path.isfile(report_file)) + self.assertTrue(Path(report_file).is_file()) content = Path(report_file).read_text() expected_cells = (100 // sample_interval[0]) * (100 // sample_interval[1]) match = re.search(r"Sample size:\s+(\d+)", content) diff --git a/imagery/i.svm.train/testsuite/test_i_svm_train.py b/imagery/i.svm.train/testsuite/test_i_svm_train.py index 2aa5f4eaba3..af2ab843172 100644 --- a/imagery/i.svm.train/testsuite/test_i_svm_train.py +++ b/imagery/i.svm.train/testsuite/test_i_svm_train.py @@ -9,10 +9,10 @@ for details. """ -import os import unittest import ctypes import shutil +from pathlib import Path from grass.script import core as grass from grass.gunittest.case import TestCase @@ -210,19 +210,19 @@ def test_creation_of_misc_files(self): cpath = ctypes.create_string_buffer(GPATH_MAX) G_file_name_misc(cpath, sigdir, "version", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "sig", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "cats", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "colr", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "history", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) @unittest.skipIf(shutil.which("i.svm.train") is None, "i.svm.train not found.") def test_dont_fail_if_misc_files_missing(self): @@ -243,19 +243,19 @@ def test_dont_fail_if_misc_files_missing(self): cpath = ctypes.create_string_buffer(GPATH_MAX) G_file_name_misc(cpath, sigdir, "version", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "sig", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "cats", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "colr", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertFalse(os.path.isfile(misc_file)) + self.assertFalse(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "history", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) @unittest.skipIf(shutil.which("i.svm.train") is None, "i.svm.train not found.") def test_rescaling(self): @@ -276,23 +276,23 @@ def test_rescaling(self): cpath = ctypes.create_string_buffer(GPATH_MAX) G_file_name_misc(cpath, sigdir, "version", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "sig", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "cats", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "colr", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertFalse(os.path.isfile(misc_file)) + self.assertFalse(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "history", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) + self.assertTrue(Path(misc_file).is_file()) G_file_name_misc(cpath, sigdir, "scale", sigfile, self.mapset_name) misc_file = utils.decode(cpath.value) - self.assertTrue(os.path.isfile(misc_file)) - with open(misc_file) as rf: + self.assertTrue(Path(misc_file).is_file()) + with Path(misc_file).open() as rf: lines = rf.readlines() M, R = lines[0].strip().split(" ") self.assertTrue(float(M) > -1 and float(M) < 1) diff --git a/lib/imagery/testsuite/test_imagery_signature_management.py b/lib/imagery/testsuite/test_imagery_signature_management.py index e180090c655..c6b2cef2998 100644 --- a/lib/imagery/testsuite/test_imagery_signature_management.py +++ b/lib/imagery/testsuite/test_imagery_signature_management.py @@ -83,34 +83,34 @@ def tearDownClass(cls): def test_make_sig(self): I_make_signatures_dir(I_SIGFILE_TYPE_SIG) self.assertTrue( - os.path.isdir(os.path.join(self.tmp_mapset_path, "signatures", "sig")) + Path(os.path.join(self.tmp_mapset_path, "signatures", "sig")).is_dir() ) # There should not be any side effects of calling function multiple times I_make_signatures_dir(I_SIGFILE_TYPE_SIG) self.assertTrue( - os.path.isdir(os.path.join(self.tmp_mapset_path, "signatures", "sig")) + Path(os.path.join(self.tmp_mapset_path, "signatures", "sig")).is_dir() ) def test_make_sigset(self): I_make_signatures_dir(I_SIGFILE_TYPE_SIGSET) self.assertTrue( - os.path.isdir(os.path.join(self.tmp_mapset_path, "signatures", "sigset")) + Path(os.path.join(self.tmp_mapset_path, "signatures", "sigset")).is_dir() ) # There should not be any side effects of calling function multiple times I_make_signatures_dir(I_SIGFILE_TYPE_SIGSET) self.assertTrue( - os.path.isdir(os.path.join(self.tmp_mapset_path, "signatures", "sigset")) + Path(os.path.join(self.tmp_mapset_path, "signatures", "sigset")).is_dir() ) def test_make_libsvm(self): I_make_signatures_dir(I_SIGFILE_TYPE_LIBSVM) self.assertTrue( - os.path.isdir(os.path.join(self.tmp_mapset_path, "signatures", "libsvm")) + Path(os.path.join(self.tmp_mapset_path, "signatures", "libsvm")).is_dir() ) # There should not be any side effects of calling function multiple times I_make_signatures_dir(I_SIGFILE_TYPE_LIBSVM) self.assertTrue( - os.path.isdir(os.path.join(self.tmp_mapset_path, "signatures", "libsvm")) + Path(os.path.join(self.tmp_mapset_path, "signatures", "libsvm")).is_dir() ) @@ -470,7 +470,7 @@ def test_success_unqualified_sig(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{self.mpath}/signatures/sig/{dst}/sig")) + self.assertTrue(Path(f"{self.mpath}/signatures/sig/{dst}/sig").is_file()) def test_success_fq_sig(self): dst_name = tempname(10) @@ -491,7 +491,7 @@ def test_success_fq_sig(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{self.mpath}/signatures/sig/{dst_name}/sig")) + self.assertTrue(Path(f"{self.mpath}/signatures/sig/{dst_name}/sig").is_file()) def test_success_unqualified_sigset(self): dst = tempname(10) @@ -510,7 +510,7 @@ def test_success_unqualified_sigset(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{self.mpath}/signatures/sigset/{dst}/sig")) + self.assertTrue(Path(f"{self.mpath}/signatures/sigset/{dst}/sig").is_file()) def test_success_fq_sigset(self): dst_name = tempname(10) @@ -534,7 +534,7 @@ def test_success_fq_sigset(self): ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) self.assertTrue( - os.path.isfile(f"{self.mpath}/signatures/sigset/{dst_name}/sig") + Path(f"{self.mpath}/signatures/sigset/{dst_name}/sig").is_file() ) def test_success_unqualified_libsvm(self): @@ -554,7 +554,7 @@ def test_success_unqualified_libsvm(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{self.mpath}/signatures/libsvm/{dst}/sig")) + self.assertTrue(Path(f"{self.mpath}/signatures/libsvm/{dst}/sig").is_file()) def test_success_fq_libsvm(self): dst = tempname(10) @@ -578,7 +578,7 @@ def test_success_fq_libsvm(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{dst_dir}/sig")) + self.assertTrue(Path(f"{dst_dir}/sig").is_file()) class SignaturesRenameTestCase(TestCase): @@ -638,7 +638,7 @@ def test_success_unqualified_sig(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{self.mpath}/signatures/sig/{dst}/sig")) + self.assertTrue(Path(f"{self.mpath}/signatures/sig/{dst}/sig").is_file()) def test_success_fq_sig(self): src_sig = tempname(10) @@ -663,7 +663,7 @@ def test_success_fq_sig(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{self.mpath}/signatures/sig/{dst_name}/sig")) + self.assertTrue(Path(f"{self.mpath}/signatures/sig/{dst_name}/sig").is_file()) def test_success_unqualified_sigset(self): src_sigset = tempname(10) @@ -683,7 +683,7 @@ def test_success_unqualified_sigset(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{self.mpath}/signatures/sigset/{dst}/sig")) + self.assertTrue(Path(f"{self.mpath}/signatures/sigset/{dst}/sig").is_file()) def test_success_fq_sigset(self): src_sigset = tempname(10) @@ -709,7 +709,7 @@ def test_success_fq_sigset(self): ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) self.assertTrue( - os.path.isfile(f"{self.mpath}/signatures/sigset/{dst_name}/sig") + Path(f"{self.mpath}/signatures/sigset/{dst_name}/sig").is_file() ) def test_success_unqualified_libsvm(self): @@ -730,7 +730,7 @@ def test_success_unqualified_libsvm(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{self.mpath}/signatures/libsvm/{dst}/sig")) + self.assertTrue(Path(f"{self.mpath}/signatures/libsvm/{dst}/sig").is_file()) def test_success_fq_libsvm(self): src_sig = tempname(10) @@ -756,7 +756,7 @@ def test_success_fq_libsvm(self): self.assertTrue(ret) ms = utils.decode(ret) self.assertEqual(ms, self.mapset_name) - self.assertTrue(os.path.isfile(f"{dst_dir}/sig")) + self.assertTrue(Path(f"{dst_dir}/sig").is_file()) class SignaturesListByTypeTestCase(TestCase): diff --git a/lib/init/grass.py b/lib/init/grass.py index 270803ddc81..7d10913cdbd 100755 --- a/lib/init/grass.py +++ b/lib/init/grass.py @@ -697,7 +697,7 @@ def cannot_create_location_reason(gisdbase: StrPath, location: str) -> str: " the project <{location}>" " already exists." ).format(**locals()) - if os.path.isfile(path): + if Path(path).is_file(): return _( "Unable to create new project <{location}> because <{path}> is a file." ).format(**locals()) @@ -843,7 +843,7 @@ def set_mapset( else: # 'location_name' is a valid GRASS location, # create new mapset - if os.path.isfile(path): + if Path(path).is_file(): # not a valid mapset, but dir exists, assuming # broken/incomplete mapset fatal( diff --git a/pyproject.toml b/pyproject.toml index f58e088bc43..972dead3cc6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -219,7 +219,6 @@ ignore = [ "PTH108", # os-unlink "PTH111", # os-path-expanduser "PTH112", # os-path-isdir - "PTH113", # os-path-isfile "PTH117", # os-path-isabs "PTH118", # os-path-join "PTH119", # os-path-basename diff --git a/python/grass/gunittest/case.py b/python/grass/gunittest/case.py index 30c1f1b88c7..92bc38f8237 100644 --- a/python/grass/gunittest/case.py +++ b/python/grass/gunittest/case.py @@ -645,7 +645,7 @@ def assertFileExists( if the file is accessible for reading since we expect that user wants to look at created files. """ - if not os.path.isfile(filename): + if not Path(filename).is_file(): stdmsg = "File %s does not exist" % filename self.fail(self._formatMessage(msg, stdmsg)) if not skip_size_check and not Path(filename).stat().st_size: diff --git a/python/grass/gunittest/reporters.py b/python/grass/gunittest/reporters.py index 93245648e9a..fde3d6800a6 100644 --- a/python/grass/gunittest/reporters.py +++ b/python/grass/gunittest/reporters.py @@ -474,7 +474,7 @@ def wrap_stdstream_to_html( def html_file_preview(filename): before = "
"
after = ""
- if not os.path.isfile(filename):
+ if not Path(filename).is_file():
return ' not found") % infile)
name = output or ""
diff --git a/scripts/v.in.wfs/v.in.wfs.py b/scripts/v.in.wfs/v.in.wfs.py
index b9fe74ee6f6..3828524850a 100755
--- a/scripts/v.in.wfs/v.in.wfs.py
+++ b/scripts/v.in.wfs/v.in.wfs.py
@@ -162,14 +162,14 @@ def main():
# Set user and password if given
if options["username"] and options["password"]:
grass.message(_("Setting username and password..."))
- if os.path.isfile(options["username"]):
+ if Path(options["username"]).is_file():
filecontent = Path(options["username"]).read_text()
user = filecontent.strip()
elif options["username"] in os.environ:
user = os.environ[options["username"]]
else:
user = options["username"]
- if os.path.isfile(options["password"]):
+ if Path(options["password"]).is_file():
filecontent = Path(options["password"]).read_text()
pw = filecontent.strip()
elif options["password"] in os.environ:
diff --git a/scripts/v.pack/testsuite/test_v_pack.py b/scripts/v.pack/testsuite/test_v_pack.py
index 41d394a618e..a2d9aaf450a 100644
--- a/scripts/v.pack/testsuite/test_v_pack.py
+++ b/scripts/v.pack/testsuite/test_v_pack.py
@@ -4,12 +4,11 @@
@author: Sanjeet Bhatti
"""
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.gmodules import SimpleModule
-import os
-
class TestVPack(TestCase):
"""Test v.pack script"""
@@ -20,8 +19,7 @@ class TestVPack(TestCase):
@classmethod
def tearDownClass(cls):
"""Remove output file"""
- if os.path.isfile(cls.outFile):
- os.remove(cls.outFile)
+ Path(cls.outFile).unlink(missing_ok=True)
def test_v_pack(self):
"""Create a pack file test"""
diff --git a/scripts/v.unpack/testsuite/test_v_unpack.py b/scripts/v.unpack/testsuite/test_v_unpack.py
index e5df76e7834..919e18d4363 100644
--- a/scripts/v.unpack/testsuite/test_v_unpack.py
+++ b/scripts/v.unpack/testsuite/test_v_unpack.py
@@ -4,12 +4,11 @@
@author: Sanjeet Bhatti
"""
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.gmodules import SimpleModule
-import os
-
class TestVUnpack(TestCase):
"""Test v.unpack script"""
@@ -26,8 +25,7 @@ def setUpClass(cls):
def tearDownClass(cls):
"""Remove pack file created region"""
cls.runModule("g.remove", type="vector", name=cls.mapName, flags="f")
- if os.path.isfile(cls.packFile):
- os.remove(cls.packFile)
+ Path(cls.packFile).unlink(missing_ok=True)
def test_v_pack(self):
"""Unpack file test"""
diff --git a/temporal/t.connect/testsuite/test_distr_tgis_db_raster.py b/temporal/t.connect/testsuite/test_distr_tgis_db_raster.py
index 6f578e33eee..97ea6ee9611 100644
--- a/temporal/t.connect/testsuite/test_distr_tgis_db_raster.py
+++ b/temporal/t.connect/testsuite/test_distr_tgis_db_raster.py
@@ -108,12 +108,12 @@ def test_tlist(self):
)
self.assertModule(t_list)
self.assertFileExists(self.outfile)
- read_data = Path(self.outfile).read_text()
+ outfile_path = Path(self.outfile)
+ read_data = outfile_path.read_text()
for a, b in zip(list_string.split("\n"), read_data.split("\n"), strict=False):
self.assertEqual(a.strip(), b.strip())
# self.assertLooksLike(reference=read_data, actual=list_string)
- if os.path.isfile(self.outfile):
- os.remove(self.outfile)
+ outfile_path.unlink(missing_ok=True)
def test_trast_list(self):
self.runModule("g.mapset", mapset="test1")
@@ -183,11 +183,11 @@ def test_trast_list(self):
)
self.assertModule(trast_list)
self.assertFileExists(self.outfile)
- read_data = Path(self.outfile).read_text()
+ outfile_path = Path(self.outfile)
+ read_data = outfile_path.read_text()
for a, b in zip(list_string.split("\n"), read_data.split("\n"), strict=False):
self.assertEqual(a.strip(), b.strip())
- if os.path.isfile(self.outfile):
- os.remove(self.outfile)
+ outfile_path.unlink(missing_ok=True)
def test_strds_info(self):
self.runModule("g.mapset", mapset="test4")
diff --git a/temporal/t.connect/testsuite/test_distr_tgis_db_raster3d.py b/temporal/t.connect/testsuite/test_distr_tgis_db_raster3d.py
index 4a312db5de9..322b03f1087 100644
--- a/temporal/t.connect/testsuite/test_distr_tgis_db_raster3d.py
+++ b/temporal/t.connect/testsuite/test_distr_tgis_db_raster3d.py
@@ -108,12 +108,12 @@ def test_tlist(self):
)
self.assertModule(t_list)
self.assertFileExists(self.outfile)
- read_data = Path(self.outfile).read_text()
+ outfile_path = Path(self.outfile)
+ read_data = outfile_path.read_text()
for a, b in zip(list_string.split("\n"), read_data.split("\n"), strict=False):
self.assertEqual(a.strip(), b.strip())
# self.assertLooksLike(reference=read_data, actual=list_string)
- if os.path.isfile(self.outfile):
- os.remove(self.outfile)
+ outfile_path.unlink(missing_ok=True)
def test_trast_list(self):
self.runModule("g.mapset", mapset="test3d1")
@@ -183,11 +183,11 @@ def test_trast_list(self):
)
self.assertModule(trast_list)
self.assertFileExists(self.outfile)
- read_data = Path(self.outfile).read_text()
+ outfile_path = Path(self.outfile)
+ read_data = outfile_path.read_text()
for a, b in zip(list_string.split("\n"), read_data.split("\n"), strict=False):
self.assertEqual(a.strip(), b.strip())
- if os.path.isfile(self.outfile):
- os.remove(self.outfile)
+ outfile_path.unlink(missing_ok=True)
def test_strds_info(self):
self.runModule("g.mapset", mapset="test3d4")
diff --git a/temporal/t.connect/testsuite/test_distr_tgis_db_vector.py b/temporal/t.connect/testsuite/test_distr_tgis_db_vector.py
index 0b9911c6e30..a7735b3e89b 100644
--- a/temporal/t.connect/testsuite/test_distr_tgis_db_vector.py
+++ b/temporal/t.connect/testsuite/test_distr_tgis_db_vector.py
@@ -108,12 +108,12 @@ def test_tlist(self):
)
self.assertModule(t_list)
self.assertFileExists(self.outfile)
- read_data = Path(self.outfile).read_text()
+ outfile_path = Path(self.outfile)
+ read_data = outfile_path.read_text()
for a, b in zip(list_string.split("\n"), read_data.split("\n"), strict=False):
self.assertEqual(a.strip(), b.strip())
# self.assertLooksLike(reference=read_data, actual=list_string)
- if os.path.isfile(self.outfile):
- os.remove(self.outfile)
+ outfile_path.unlink(missing_ok=True)
def test_tvect_list(self):
self.runModule("g.mapset", mapset="testvect1")
@@ -200,11 +200,11 @@ def test_tvect_list(self):
)
self.assertModule(trast_list)
self.assertFileExists(self.outfile)
- read_data = Path(self.outfile).read_text()
+ outfile_path = Path(self.outfile)
+ read_data = outfile_path.read_text()
for a, b in zip(list_string.split("\n"), read_data.split("\n"), strict=False):
self.assertEqual(a.strip(), b.strip())
- if os.path.isfile(self.outfile):
- os.remove(self.outfile)
+ outfile_path.unlink(missing_ok=True)
def test_stvds_info(self):
self.runModule("g.mapset", mapset="testvect4")
diff --git a/vector/v.db.select/testsuite/test_v_db_select.py b/vector/v.db.select/testsuite/test_v_db_select.py
index 7591a2c3ae7..a5152c1bfc5 100644
--- a/vector/v.db.select/testsuite/test_v_db_select.py
+++ b/vector/v.db.select/testsuite/test_v_db_select.py
@@ -9,7 +9,7 @@
for details.
"""
-import os
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.gmodules import SimpleModule
@@ -211,8 +211,7 @@ def testFileExists(self):
"""This function checks if the output file is written correctly"""
self.runModule("v.db.select", map=self.invect, file=self.outfile)
self.assertFileExists(self.outfile)
- if os.path.isfile(self.outfile):
- os.remove(self.outfile)
+ Path(self.outfile).unlink(missing_ok=True)
def testGroup(self):
"""Testing v.db.select with group option"""
diff --git a/vector/v.in.lidar/testsuite/decimation_test.py b/vector/v.in.lidar/testsuite/decimation_test.py
index 7fb596343b9..960c1583bc3 100644
--- a/vector/v.in.lidar/testsuite/decimation_test.py
+++ b/vector/v.in.lidar/testsuite/decimation_test.py
@@ -9,7 +9,7 @@
for details.
"""
-import os
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
@@ -46,8 +46,7 @@ def setUpClass(cls):
def tearDownClass(cls):
"""Remove the temporary region and generated data"""
cls.runModule("g.remove", flags="f", type="vector", name=cls.vector_points)
- if os.path.isfile(cls.las_file):
- os.remove(cls.las_file)
+ Path(cls.las_file).unlink(missing_ok=True)
cls.del_temp_region()
def tearDown(self):
diff --git a/vector/v.in.lidar/testsuite/mask_test.py b/vector/v.in.lidar/testsuite/mask_test.py
index 99244e72a23..4c66066bab4 100644
--- a/vector/v.in.lidar/testsuite/mask_test.py
+++ b/vector/v.in.lidar/testsuite/mask_test.py
@@ -9,8 +9,7 @@
for details.
"""
-import os
-
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
@@ -118,8 +117,7 @@ def tearDownClass(cls):
cls.runModule(
"g.remove", flags="f", type="vector", name=(cls.points, cls.areas)
)
- if os.path.isfile(cls.las_file):
- os.remove(cls.las_file)
+ Path(cls.las_file).unlink(missing_ok=True)
cls.del_temp_region()
def tearDown(self):
diff --git a/vector/v.in.lidar/testsuite/test_v_in_lidar_basic.py b/vector/v.in.lidar/testsuite/test_v_in_lidar_basic.py
index 50f02a55477..cdaa0a9be56 100644
--- a/vector/v.in.lidar/testsuite/test_v_in_lidar_basic.py
+++ b/vector/v.in.lidar/testsuite/test_v_in_lidar_basic.py
@@ -9,7 +9,7 @@
for details.
"""
-import os
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
@@ -46,8 +46,7 @@ def setUpClass(cls):
def tearDownClass(cls):
"""Remove the temporary region and generated data"""
cls.runModule("g.remove", flags="f", type="vector", name=cls.vector_points)
- if os.path.isfile(cls.las_file):
- os.remove(cls.las_file)
+ Path(cls.las_file).unlink(missing_ok=True)
cls.del_temp_region()
def tearDown(self):
diff --git a/vector/v.in.lidar/testsuite/test_v_in_lidar_filter.py b/vector/v.in.lidar/testsuite/test_v_in_lidar_filter.py
index 95264940d0d..e5b7fe330aa 100644
--- a/vector/v.in.lidar/testsuite/test_v_in_lidar_filter.py
+++ b/vector/v.in.lidar/testsuite/test_v_in_lidar_filter.py
@@ -9,7 +9,7 @@
for details.
"""
-import os
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
@@ -81,8 +81,7 @@ def setUpClass(cls):
def tearDownClass(cls):
"""Remove the temporary region and generated data"""
cls.runModule("g.remove", flags="f", type="vector", name=cls.vector_points)
- if os.path.isfile(cls.las_file):
- os.remove(cls.las_file)
+ Path(cls.las_file).unlink(missing_ok=True)
cls.del_temp_region()
def tearDown(self):
diff --git a/vector/v.in.pdal/testsuite/test_v_in_pdal_basic.py b/vector/v.in.pdal/testsuite/test_v_in_pdal_basic.py
index c28386dab81..3a40855c808 100644
--- a/vector/v.in.pdal/testsuite/test_v_in_pdal_basic.py
+++ b/vector/v.in.pdal/testsuite/test_v_in_pdal_basic.py
@@ -9,11 +9,11 @@
for details.
"""
-import os
import shutil
+import unittest
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
-import unittest
class BasicTest(TestCase):
@@ -61,8 +61,7 @@ def tearDownClass(cls):
type="vector",
name=(cls.vector_points, cls.vector_generated),
)
- if os.path.isfile(cls.las_file):
- os.remove(cls.las_file)
+ Path(cls.las_file).unlink(missing_ok=True)
cls.del_temp_region()
def tearDown(self):
diff --git a/vector/v.in.pdal/testsuite/test_v_in_pdal_filter.py b/vector/v.in.pdal/testsuite/test_v_in_pdal_filter.py
index 3c10c9b8331..34e4e9dd0f2 100644
--- a/vector/v.in.pdal/testsuite/test_v_in_pdal_filter.py
+++ b/vector/v.in.pdal/testsuite/test_v_in_pdal_filter.py
@@ -9,11 +9,12 @@
for details.
"""
-import os
import shutil
+import unittest
+from pathlib import Path
+
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
-import unittest
POINTS = """\
17.46938776,18.67346939,143,1,1,2
@@ -82,8 +83,7 @@ def setUpClass(cls):
def tearDownClass(cls):
"""Remove the temporary region and generated data"""
cls.runModule("g.remove", flags="f", type="vector", name=cls.vector_points)
- if os.path.isfile(cls.las_file):
- os.remove(cls.las_file)
+ Path(cls.las_file).unlink(missing_ok=True)
cls.del_temp_region()
def tearDown(self):
diff --git a/vector/v.out.lidar/testsuite/test_v_out_lidar.py b/vector/v.out.lidar/testsuite/test_v_out_lidar.py
index 613cb14b61a..16511d51526 100644
--- a/vector/v.out.lidar/testsuite/test_v_out_lidar.py
+++ b/vector/v.out.lidar/testsuite/test_v_out_lidar.py
@@ -9,7 +9,7 @@
for details.
"""
-import os
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
from grass.gunittest.utils import xfail_windows
@@ -52,8 +52,7 @@ def tearDown(self):
This is executed after each test run.
"""
- if os.path.isfile(self.las_file):
- os.remove(self.las_file)
+ Path(self.las_file).unlink(missing_ok=True)
self.runModule("g.remove", flags="f", type="vector", name=self.imported_points)
def test_module_runs_output_created(self):
diff --git a/vector/v.profile/testsuite/test_v_profile.py b/vector/v.profile/testsuite/test_v_profile.py
index 1ec5d731c77..d7f882492e2 100644
--- a/vector/v.profile/testsuite/test_v_profile.py
+++ b/vector/v.profile/testsuite/test_v_profile.py
@@ -12,7 +12,7 @@
Cover more input/output combinations.
"""
-import os
+from pathlib import Path
from grass.gunittest.case import TestCase
from grass.gunittest.main import test
@@ -177,8 +177,7 @@ def testFileExists(self):
output=self.outfile,
)
self.assertFileExists(self.outfile)
- if os.path.isfile(self.outfile):
- os.remove(self.outfile)
+ Path(self.outfile).unlink(missing_ok=True)
def testOutput(self):
"""Test correctness of output"""
From a498626fdda4ab2c8eb0929085ba268207bd643d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Edouard=20Choini=C3=A8re?=
<27212526+echoix@users.noreply.github.com>
Date: Thu, 6 Nov 2025 17:15:59 -0500
Subject: [PATCH 2/4] Apply suggestions from code review
Co-authored-by: Stefan Blumentrath