Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks: Fix E722 do not use bare except #3748

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 44 additions & 64 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ per-file-ignores =
# F841 local variable assigned to but never used
# E741 ambiguous variable name 'l'
__init__.py: F401, F403
lib/init/grass.py: E722, F821, F841, W605
lib/init/grass.py: F821, F841, W605
utils/mkrest.py: W605
utils/gitlog2changelog.py: E722, E712, W605
utils/gitlog2changelog.py: E712, W605
utils/g.html2man/ggroff.py: W605
man/build_check_rest.py: F403, F405
man/build_full_index_rest.py: F403, F405
Expand All @@ -33,93 +33,81 @@ per-file-ignores =
man/build_full_index.py: F403, F405
man/build_index.py: F403, F405
man/build_index_rest.py: F403, F405
man/build_keywords.py: F403, F405, E722
man/build_topics.py: F403, F405, E722
man/build_keywords.py: F403, F405
man/build_topics.py: F403, F405
man/build_html.py: E501
imagery/i.atcorr/create_iwave.py: F632, F821, W293
doc/python/raster_example_ctypes.py: F403, F405
doc/python/vector_example_ctypes.py: F403, F405
doc/python/m.distance.py: F403, F405, E501
doc/gui/wxpython/example/dialogs.py: F401
locale/grass_po_stats.py: E122, E128, E231, E401, E722, E741
locale/grass_po_stats.py: E122, E128, E231, E401, E741
gui/scripts/d.wms.py: E501
gui/wxpython/core/gcmd.py: E402
gui/wxpython/core/gthread.py: F841
gui/wxpython/core/gconsole.py: E722, W605
gui/wxpython/core/gconsole.py: W605
gui/wxpython/core/globalvar.py: W605
gui/wxpython/core/toolboxes.py: E722
gui/wxpython/core/utils.py: E722, F841, W605
gui/wxpython/core/workspace.py: E722
gui/wxpython/core/render.py: E722, F841
gui/wxpython/core/utils.py: F841, W605
gui/wxpython/core/render.py: F841
gui/wxpython/core/ws.py: F841
gui/wxpython/core/settings.py: E722
gui/wxpython/core/watchdog.py: E402
gui/wxpython/datacatalog/tree.py: E731, E402
gui/wxpython/dbmgr/base.py: E722, F841
gui/wxpython/dbmgr/dialogs.py: F841, E722
gui/wxpython/dbmgr/sqlbuilder.py: E722, F841
gui/wxpython/dbmgr/manager.py: E722
gui/wxpython/dbmgr/base.py: F841
gui/wxpython/dbmgr/dialogs.py: F841
gui/wxpython/dbmgr/sqlbuilder.py: F841
gui/wxpython/dbmgr/vinfo.py: F841
gui/wxpython/docs/wxgui_sphinx/conf.py: E402, W291
gui/wxpython/gcp/g.gui.gcp.py: F841
gui/wxpython/gcp/manager.py: F841, E722
gui/wxpython/gcp/manager.py: F841
gui/wxpython/gcp/mapdisplay.py: F841
gui/wxpython/gui_core/*: F841, E266, E722, W605
gui/wxpython/gui_core/dialogs.py: E722, F841, W605
gui/wxpython/gui_core/forms.py: E722, F841
gui/wxpython/gui_core/ghelp.py: E722
gui/wxpython/gui_core/gselect.py: F841, E266, E722
gui/wxpython/gui_core/*: F841, E266, W605
gui/wxpython/gui_core/dialogs.py: F841, W605
gui/wxpython/gui_core/forms.py: F841
gui/wxpython/gui_core/gselect.py: F841, E266
gui/wxpython/gui_core/preferences.py: E266, F841
gui/wxpython/gui_core/treeview.py: F841
gui/wxpython/gui_core/widgets.py: F841, E722, E266
gui/wxpython/image2target/*: F841, E722, E265
gui/wxpython/gui_core/widgets.py: F841, E266
gui/wxpython/image2target/*: F841, E265
gui/wxpython/image2target/g.gui.image2target.py: E501, E265, F841
gui/wxpython/iscatt/*: F841, E722, E741, F405, F403
gui/wxpython/iscatt/*: F841, E741, F405, F403
gui/wxpython/lmgr/giface.py: E741
gui/wxpython/lmgr/frame.py: F841, E722, W605
gui/wxpython/lmgr/frame.py: F841, W605
# layertree still includes some formatting issues (it is ignored by Black)
gui/wxpython/lmgr/layertree.py: E722, E266, W504, E225
gui/wxpython/lmgr/layertree.py: E266, W504, E225
gui/wxpython/lmgr/workspace.py: F841
gui/wxpython/modules/*: F841, E722, W605
gui/wxpython/nviz/*: F841, E266, E722, W605, F403, F405
gui/wxpython/photo2image/*: F841, E722, E265
gui/wxpython/modules/*: F841, W605
gui/wxpython/nviz/*: F841, E266, W605, F403, F405
gui/wxpython/photo2image/*: F841, E265
gui/wxpython/photo2image/g.gui.photo2image.py: E501, F841
gui/wxpython/psmap/*: F841, E266, E722, F405, F403
gui/wxpython/vdigit/*: F841, E722, E741, F405, F403
gui/wxpython/psmap/*: F841, E266, F405, F403
gui/wxpython/vdigit/*: F841, E741, F405, F403
gui/wxpython/vnet/*: F841
gui/wxpython/wxgui.py: F841
gui/wxpython/animation/g.gui.animation.py: E501
gui/wxpython/animation/mapwindow.py: F841
gui/wxpython/animation/provider.py: F841
gui/wxpython/tplot/frame.py: F841, E722, E741
gui/wxpython/tplot/frame.py: F841, E741
gui/wxpython/tplot/g.gui.tplot.py: E501
gui/wxpython/rdigit/g.gui.rdigit.py: F841
gui/wxpython/iclass/dialogs.py: E741
gui/wxpython/iclass/digit.py: F405, F403
gui/wxpython/iclass/frame.py: F405, F403
gui/wxpython/iclass/g.gui.iclass.py: E501
gui/wxpython/iclass/statistics.py: F841, F405, F403
gui/wxpython/wxplot/histogram.py: E722
gui/wxpython/wxplot/profile.py: F841, E722
gui/wxpython/wxplot/base.py: F841, E722
gui/wxpython/wxplot/profile.py: F841
gui/wxpython/wxplot/base.py: F841
gui/wxpython/location_wizard/dialogs.py: F841
gui/wxpython/location_wizard/wizard.py: E722
gui/wxpython/mapdisp/main.py: E722
gui/wxpython/mapdisp/test_mapdisp.py: E501
gui/wxpython/mapdisp/statusbar.py: F841
gui/wxpython/mapswipe/g.gui.mapswipe.py: E501
gui/wxpython/mapwin/base.py: E722
gui/wxpython/mapwin/buffered.py: E722
gui/wxpython/mapwin/graphics.py: E722
gui/wxpython/startup/locdownload.py: E722, E402
gui/wxpython/startup/locdownload.py: E402
gui/wxpython/timeline/g.gui.timeline.py: E501, E741
gui/wxpython/timeline/frame.py: E741
gui/wxpython/tools/build_modules_xml.py: E722
gui/wxpython/web_services/cap_interface.py: E501
gui/wxpython/web_services/widgets.py: F841, E741, W605, E402
gui/wxpython/rlisetup/frame.py: E741
gui/wxpython/rlisetup/sampling_frame.py: F841
gui/wxpython/rlisetup/wizard.py: E722, E741
gui/wxpython/rlisetup/wizard.py: E741
# Generated file
gui/wxpython/menustrings.py: E501
# F821 undefined name 'cmp'
Expand All @@ -142,18 +130,15 @@ per-file-ignores =
python/grass/pygrass/rpc/__init__.py: F401, F403
python/grass/pygrass/utils.py: E402
python/grass/script/task.py: W605
python/grass/temporal/abstract_space_time_dataset.py: W605, F841, E722
python/grass/temporal/c_libraries_interface.py: F841, E722
python/grass/temporal/core.py: E722
python/grass/temporal/datetime_math.py: F841, E722
python/grass/temporal/abstract_space_time_dataset.py: W605, F841
python/grass/temporal/c_libraries_interface.py: F841
python/grass/temporal/datetime_math.py: F841
python/grass/temporal/open_stds.py: F841
python/grass/temporal/spatial_topology_dataset_connector.py: E722
python/grass/temporal/temporal_algebra.py: E741, F841, E722
python/grass/temporal/temporal_granularity.py: F841, E722
python/grass/temporal/temporal_algebra.py: E741, F841
python/grass/temporal/temporal_granularity.py: F841
python/grass/temporal/temporal_raster_algebra.py: E741
python/grass/temporal/temporal_raster_base_algebra.py: F841, E722
python/grass/temporal/temporal_raster_base_algebra.py: F841
python/grass/temporal/temporal_raster3d_algebra.py: E741
python/grass/temporal/temporal_topology_dataset_connector.py: E722
python/grass/temporal/temporal_vector_algebra.py: E741, F841
python/grass/temporal/univar_statistics.py: E231
# Current benchmarks/tests are changing sys.path before import.
Expand All @@ -172,24 +157,19 @@ per-file-ignores =
python/grass/*/*/*/__init__.py: F401, F403
# E402 module level import not at top of file
scripts/d.polar/d.polar.py: F841
scripts/r.in.wms/wms_gdal_drv.py: F841, E722
scripts/r.in.wms/wms_gdal_drv.py: F841
scripts/r.in.wms/wms_cap_parsers.py: F841, E741
scripts/r.in.wms/wms_drv.py: E402, E722
scripts/r.in.wms/srs.py: E722
scripts/r.in.wms/wms_drv.py: E402
scripts/r.semantic.label/r.semantic.label.py: F841, E501
scripts/v.report/v.report.py: F841, E721
scripts/db.out.ogr/db.out.ogr.py: F841
scripts/g.extension/g.extension.py: F841, E722, E501
scripts/v.unpack/v.unpack.py: F841, E722, E501
scripts/v.import/v.import.py: F841, E722, E501
scripts/g.extension/g.extension.py: F841, E501
scripts/v.unpack/v.unpack.py: F841, E501
scripts/v.import/v.import.py: F841, E501
scripts/db.univar/db.univar.py: E741, E501
scripts/d.rast.leg/d.rast.leg.py: E741
scripts/d.frame/d.frame.py: E722
scripts/i.pansharpen/i.pansharpen.py: E722, E501
scripts/r.in.srtm/r.in.srtm.py: E722
scripts/r.fillnulls/r.fillnulls.py: E722
scripts/d.rast.edit/d.rast.edit.py: E722
scripts/v.what.strds/v.what.strds.py: E722, E501
scripts/i.pansharpen/i.pansharpen.py: E501
scripts/v.what.strds/v.what.strds.py: E501
# Line too long (esp. module interface definitions)
scripts/*/*.py: E501
# local variable 'column' is assigned to but never used
Expand Down
3 changes: 2 additions & 1 deletion gui/wxpython/core/gconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import grass.script as grass
from grass.script import task as gtask
from grass.exceptions import ScriptError

from grass.pydispatch.signal import Signal

Expand Down Expand Up @@ -676,7 +677,7 @@ def RunCmd(
if len(command) == 1 and not skipInterface:
try:
task = gtask.parse_interface(command[0])
except:
except ScriptError:
task = None
else:
task = None
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ def SetRegion(self, windres=False, windres3=False):

return grass_region

except:
except (KeyError, TypeError):
return None

def GetListOfLayers(
Expand Down
10 changes: 8 additions & 2 deletions gui/wxpython/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ def _generateLocale(self):
self.locs.sort()
# Add a default choice to not override system locale
self.locs.insert(0, "system")
except:
except (
KeyError,
FileNotFoundError,
PermissionError,
NotADirectoryError,
OSError,
):
# No NLS
self.locs = ["system"]

Expand Down Expand Up @@ -992,7 +998,7 @@ def SaveToFile(self, settings=None):
if not os.path.exists(dirPath):
try:
os.mkdir(dirPath)
except:
except OSError:
GError(_("Unable to create settings directory"))
return
try:
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/toolboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def getMenudataFile(userRootFile, newFile, fallback):
fh.write(xml)
fh.close()
return menudataFile
except:
except Exception:
mshukuno marked this conversation as resolved.
Show resolved Hide resolved
_debug(
2,
(
Expand Down
11 changes: 6 additions & 5 deletions gui/wxpython/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from grass.script import core as grass
from grass.script import task as gtask
from grass.exceptions import CalledModuleError

from core.gcmd import RunCommand
from core.debug import Debug
Expand Down Expand Up @@ -76,7 +77,7 @@ def GetTempfile(pref=None):
return os.path.join(pref, file)
else:
return tempfile
except:
except TypeError:
return None


Expand Down Expand Up @@ -254,7 +255,7 @@ def ListOfCatsToRange(cats):

try:
cats = list(map(int, cats))
except:
except (TypeError, ValueError):
return catstr

i = 0
Expand Down Expand Up @@ -579,7 +580,7 @@ def GetListOfLocations(dbase):
os.path.join(location, "*")
):
listOfLocations.append(os.path.basename(location))
except:
except (TypeError, FileNotFoundError, PermissionError, NotADirectoryError):
pass
except (UnicodeEncodeError, UnicodeDecodeError) as e:
raise e
Expand Down Expand Up @@ -634,7 +635,7 @@ def _getGDALFormats():
"""Get dictionary of available GDAL drivers"""
try:
ret = grass.read_command("r.in.gdal", quiet=True, flags="f")
except:
except CalledModuleError:
ret = None

return _parseFormats(ret), _parseFormats(ret, writableOnly=True)
Expand All @@ -644,7 +645,7 @@ def _getOGRFormats():
"""Get dictionary of available OGR drivers"""
try:
ret = grass.read_command("v.in.ogr", quiet=True, flags="f")
except:
except CalledModuleError:
ret = None

return _parseFormats(ret), _parseFormats(ret, writableOnly=True)
Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __processFile(self):
try:
self.layerManager["pos"] = (posVal[0], posVal[1])
self.layerManager["size"] = (posVal[2], posVal[3])
except:
except IndexError:
pass
# current working directory
cwdPath = self.__getNodeText(node_lm, "cwd")
Expand Down Expand Up @@ -157,7 +157,7 @@ def __processFile(self):
try:
pos = (posVal[0], posVal[1])
size = (posVal[2], posVal[3])
except:
except IndexError:
pos = None
size = None
# this happens on Windows when mapwindow is minimized when
Expand Down Expand Up @@ -2023,7 +2023,7 @@ def _get_value(self, line):
"""Get value of element"""
try:
return line.strip(" ").split(" ")[1].strip(" ")
except:
except (IndexError, AttributeError):
return ""

def _get_element(self, line):
Expand Down
17 changes: 9 additions & 8 deletions gui/wxpython/dbmgr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def LoadData(self, layer, columns=None, where=None, sql=None):
try:
# for maps connected via v.external
keyId = columns.index(keyColumn)
except:
except ValueError:
keyId = -1

# read data
Expand Down Expand Up @@ -963,7 +963,7 @@ def OnLayerPageChanged(self, event):
self.layerPage[self.selLayer]["data"]
).GetItemCount()
)
except:
except Exception:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Log class has self.parent.SetStatusText(text_string.strip()) and I think this is wx.Frame method. Since I have no idea what exceptions to expect from WxPython method calls, I will just use Exception.

except Exception:

pass

if idCol:
Expand Down Expand Up @@ -1645,7 +1645,7 @@ def OnDataItemAdd(self, event):
if dlg.ShowModal() == wx.ID_OK:
try: # get category number
cat = int(dlg.GetValues(columns=[keyColumn])[0])
except:
except Exception:
cat = -1

try:
Expand Down Expand Up @@ -1678,7 +1678,7 @@ def OnDataItemAdd(self, event):
values[i] = int(float(values[i]))
elif tlist.columns[columnName[i]]["ctype"] == float:
values[i] = float(values[i])
except:
except Exception:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be pylint warning raise broad-exception-raised / W0719. Is there way to avoid this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean here. The exception Exception is not raised here. How this is different from the other cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is not flake8 W605, but pylint W0716 (broad-exception-raised). Just wondering if you have any suggestions for something different. Keeping except Exception: here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, except Exception causes Pylint's broad-exception-caught, but that's the issue everywhere, not just here, that was my point (and that its broad-exception-caught not broad-exception-raised).

Anyway, I agree that we should be addressing Pylint exceptions as well. There is no point in replacing all except: by except Exception: because we just exchange one issue for another, but we can make use of the fact that we can resolve the Pylint issue later and get the more clear cases now and leave the difficult cases for later.

For this specific line, my whole issue was just if you point out this as something special, just an example, or something you want to address sooner than the other cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a sample. You mentioned previously that "except Exception" will silence flake8, but Pylint would have a problem with it, so fixing simple ones has been objective. Just wondering if we should deal with this now or later. I guess we'll address this when we try to fix Pylint problems.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is difficult to figure out, let's deal with that later.

raise ValueError(
_("Value '%(value)s' needs to be entered as %(type)s.")
% {
Expand Down Expand Up @@ -3101,6 +3101,7 @@ class LayerBook(wx.Notebook):
def __init__(self, parent, id, parentDialog, style=wx.BK_DEFAULT):
wx.Notebook.__init__(self, parent, id, style=style)

self.delet6yeLayer = None
self.parent = parent
self.parentDialog = parentDialog
self.mapDBInfo = self.parentDialog.dbMgrData["mapDBInfo"]
Expand Down Expand Up @@ -3817,8 +3818,8 @@ def OnAddLayer(self, event):
def OnDeleteLayer(self, event):
"""Delete layer"""
try:
layer = int(self.deleteLayer.GetValue())
except:
layer = int(self.delet6yeLayer.GetValue())
except Exception:
return

RunCommand(
Expand Down Expand Up @@ -3865,10 +3866,10 @@ def OnChangeLayer(self, event):
"""Layer number of layer to be deleted is changed"""
try:
layer = int(event.GetString())
except:
except Exception:
try:
layer = self.mapDBInfo.layers.keys()[0]
except:
except (TypeError, IndexError, AttributeError):
return

if self.GetCurrentPage() == self.modifyPanel:
Expand Down
Loading
Loading