Skip to content

Commit

Permalink
style: Fix raise-without-from-inside-except (B904)
Browse files Browse the repository at this point in the history
  • Loading branch information
echoix committed Jul 13, 2024
1 parent b01a67d commit d1f0e0e
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gui/wxpython/iscatt/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
'The Scatterplot Tool needs the "matplotlib" '
"(python-matplotlib) package to be installed. {0}"
).format(e)
)
) from e

import grass.script as gs
from grass.pydispatch.signal import Signal
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/timeline/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"(python-matplotlib and on some systems also python-matplotlib-wx) "
"package(s) to be installed. {}"
).format(e)
)
) from e

import grass.script as gs

Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/tplot/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
'The Temporal Plot Tool needs the "matplotlib" '
"(python-matplotlib) package to be installed. {0}"
).format(e)
)
) from e


import grass.temporal as tgis
Expand Down
2 changes: 1 addition & 1 deletion python/grass/pygrass/modules/interface/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def __init__(self, cmd, *args, **kargs):
except OSError as e:
print("OSError error({0}): {1}".format(e.errno, e.strerror))
str_err = "Error running: `%s --interface-description`."
raise GrassError(str_err % self.name)
raise GrassError(str_err % self.name) from e
# get the xml of the module
self.xml = get_cmd_xml.communicate()[0]
# transform and parse the xml into an Element class:
Expand Down
4 changes: 2 additions & 2 deletions python/grass/pygrass/raster/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def _chk_index(self, index):
if type(index) == str:
try:
index = self.labels().index(index)
except ValueError:
raise KeyError(index)
except ValueError as error:
raise KeyError(index) from error
return index

def _chk_value(self, value):
Expand Down
4 changes: 3 additions & 1 deletion python/grass/pygrass/rpc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def safe_receive(self, message):
except (EOFError, OSError, FatalError) as e:
# The pipe was closed by the checker thread because
# the server process was killed
raise FatalError("Exception raised: " + str(e) + " Message: " + message)
raise FatalError(
"Exception raised: " + str(e) + " Message: " + message
) from e

def stop(self):
"""Stop the check thread, the libgis server and close the pipe
Expand Down
6 changes: 3 additions & 3 deletions python/grass/pygrass/vector/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,10 +859,10 @@ def connection(self):
return psycopg2.connect(db)
except ImportError:
er = "You need to install psycopg2 to connect with this table."
raise ImportError(er)
raise ImportError(er) from None
else:
str_err = "Driver is not supported yet, pleas use: sqlite or pg"
raise TypeError(str_err)
raise TypeError(str_err) from None

def table(self):
"""Return a Table object.
Expand Down Expand Up @@ -1204,7 +1204,7 @@ def execute(self, sql_code=None, cursor=None, many=False, values=None):
"The SQL statement is not correct:\n%r,\n"
"values: %r,\n"
"SQL error: %s" % (sqlc, values, str(exc))
)
) from exc

def exist(self, cursor=None):
"""Return True if the table already exist in the DB, False otherwise
Expand Down
10 changes: 5 additions & 5 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,8 @@ def _parse_opts(lines):
break
try:
var, val = line.split(b"=", 1)
except ValueError:
raise SyntaxError("invalid output from g.parser: {}".format(line))
except ValueError as err:
raise SyntaxError("invalid output from g.parser: {}".format(line)) from err
try:
var = decode(var)
val = decode(val)
Expand All @@ -877,7 +877,7 @@ def _parse_opts(lines):
"invalid output from g.parser ({error}): {line}".format(
error=error, line=line
)
)
) from error
if var.startswith("flag_"):
flags[var[5:]] = bool(int(val))
elif var.startswith("opt_"):
Expand Down Expand Up @@ -1888,7 +1888,7 @@ def _set_location_description(path, location, text):
fd.write(os.linesep)
fd.close()
except OSError as e:
raise ScriptError(repr(e))
raise ScriptError(repr(e)) from e


def _create_location_xy(database, location):
Expand Down Expand Up @@ -1939,7 +1939,7 @@ def _create_location_xy(database, location):

os.chdir(cur_dir)
except OSError as e:
raise ScriptError(repr(e))
raise ScriptError(repr(e)) from e


# interface to g.version
Expand Down
4 changes: 2 additions & 2 deletions python/grass/script/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def get_interface_description(cmd):
"Unable to fetch interface description for command '<{cmd}>'."
"\n\nDetails: <{det}>"
).format(cmd=cmd, det=e)
)
) from e

desc = convert_xml_to_utf8(cmdout)
return desc.replace(
Expand Down Expand Up @@ -530,7 +530,7 @@ def parse_interface(name, parser=processTask, blackList=None):
_("Cannot parse interface description of<{name}> module: {error}").format(
name=name, error=error
)
)
) from error
task = parser(tree, blackList=blackList).get_task()
# if name from interface is different than the originally
# provided name, then the provided name is likely a full path needed
Expand Down
6 changes: 3 additions & 3 deletions python/grass/script/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def vector_what(
try:
ret = read_command("v.what", env=env, **cmdParams).strip()
except CalledModuleError as e:
raise ScriptError(e.msg)
raise ScriptError(e.msg) from e

data = []
if not ret:
Expand All @@ -450,10 +450,10 @@ def vector_what(

try:
result = json.loads(ret, **kwargs)
except ValueError:
except ValueError as err:
raise ScriptError(
_("v.what output is not valid JSON format:\n {ret}").format(ret=ret)
)
) from err

if multiple:
for vmap in result["Maps"]:
Expand Down
6 changes: 4 additions & 2 deletions python/grass/semantic_label/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _read_config(self):
except json.decoder.JSONDecodeError as e:
raise SemanticLabelReaderError(
"Unable to parse '{}': {}".format(json_file, e)
)
) from e

# check if configuration is valid
self._check_config(config)
Expand Down Expand Up @@ -116,7 +116,9 @@ def print_info(self, shortcut=None, band=None, semantic_label=None, extended=Fal
if shortcut and re.match(shortcut, item["shortcut"]) is None:
continue
except re.error as e:
raise SemanticLabelReaderError("Invalid pattern: {}".format(e))
raise SemanticLabelReaderError(
"Invalid pattern: {}".format(e)
) from e

found = True
if band and band not in item["bands"]:
Expand Down
14 changes: 7 additions & 7 deletions python/grass/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def extract_zip(name, directory, tmpdir):
extract_dir=extract_dir, target_dir=directory, files=files
)
except zipfile.BadZipfile as error:
raise DownloadError(_("ZIP file is unreadable: {0}").format(error))
raise DownloadError(_("ZIP file is unreadable: {0}").format(error)) from error


# modified copy from g.extension
Expand Down Expand Up @@ -167,9 +167,9 @@ def download_and_extract(source, reporthook=None):
url=source,
code=err,
),
)
except URLError:
raise DownloadError(url_error_message.format(url=source))
) from err
except URLError as e:
raise DownloadError(url_error_message.format(url=source)) from e
if headers.get("content-type", "") != "application/zip":
raise DownloadError(
_(
Expand All @@ -188,9 +188,9 @@ def download_and_extract(source, reporthook=None):
url=source,
code=err,
),
)
except URLError:
raise DownloadError(url_error_message.format(url=source))
) from err
except URLError as e:
raise DownloadError(url_error_message.format(url=source)) from e
extract_tar(name=archive_name, directory=directory, tmpdir=tmpdir)
else:
# probably programmer error
Expand Down
10 changes: 5 additions & 5 deletions scripts/r.in.wms/wms_cap_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ def __init__(self, cap_file):
if is_file:
try:
etree.ElementTree.__init__(self, file=cap_file)
except ParseError:
raise ParseError(_("Unable to parse XML file"))
except ParseError as pe:
raise ParseError(_("Unable to parse XML file")) from pe
except OSError as error:
raise ParseError(
_("Unable to open XML file '%s'.\n%s\n" % (cap_file, error))
)
) from error
else:
try:
etree.ElementTree.__init__(self, element=etree.fromstring(cap_file))
except ParseError:
raise ParseError(_("Unable to parse XML file"))
except ParseError as pe:
raise ParseError(_("Unable to parse XML file")) from pe

if self.getroot() is None:
raise ParseError(_("Root node was not found."))
Expand Down

0 comments on commit d1f0e0e

Please sign in to comment.