From 25ea71cc61346489412dc46eff60cdd8df5b958a Mon Sep 17 00:00:00 2001 From: Saeed Rasooli Date: Wed, 6 Mar 2024 03:14:16 +0330 Subject: [PATCH] ruff: fix / ignore TRY errors --- pyproject.toml | 12 +++++++----- scal3/event_lib.py | 2 +- scal3/locale_man.py | 4 ++-- scal3/os_utils.py | 4 ++-- scal3/s_object.py | 8 ++++---- scal3/ui_gtk/event/manager.py | 6 +++--- scal3/ui_gtk/layout.py | 2 +- scal3/ui_gtk/mywidgets/dialog.py | 2 -- scal3/ui_gtk/stack.py | 2 +- scal3/ui_gtk/starcal.py | 8 ++++---- scal3/utils.py | 4 ++-- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7e0be5a0c..eb904c24c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,6 +2,7 @@ target-version = "py310" line-length = 88 +# see https://docs.astral.sh/ruff/rules/ lint.select = [ "F", # Pyflakes "E", # pycodestyle Error @@ -14,7 +15,7 @@ lint.select = [ "UP", # pyupgrade "YTT", # flake8-2020 # "ANN", # flake8-annotationsq, still gives errors for `self` - # ASYNC, # flake8-async + # "ASYNC", # flake8-async "TRIO", # flake8-trio # "S", # flake8-bandit "BLE", # flake8-blind-except @@ -54,12 +55,12 @@ lint.select = [ # "PD", # pandas-vet: full of false positives "PGH", # pygrep-hooks # "PL", # Pylint - # "TRY", # tryceratops, they all sound BS + "TRY", # tryceratops, some are BS # "FLY", # flynt "NPY", # NumPy-specific rules "AIR", # Airflow "PERF", # Perflint - # "FURB", # refurb --preview + # "FURB", # refurb, ruff --preview "LOG", # flake8-logging "RUF", # Ruff-specific rules ] @@ -99,9 +100,9 @@ lint.ignore = [ "RUF005", # Consider `[*_list, x]` instead of concatenation "RUF012", # Mutable class attributes should be annotated with `typing.ClassVar` "RUF100", # Unused `noqa` directive (non-enabled: ...) - "TRY003", # Avoid specifying long messages outside the exception class, ??? - "TRY400", # Use `logging.exception` instead of `logging.error` + "TRY003", # BS: Avoid specifying long messages outside the exception class + "TRY400", # BS: Use `logging.exception` instead of `logging.error` "SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass "SIM108", # Use ternary operator {contents} instead of if-else-block "SIM117", # Use a single with statement with multiple contexts... @@ -120,6 +121,7 @@ lint.ignore = [ # Allow autofix for all enabled rules (when `--fix`) is provided. lint.fixable = [ + "TRY", # tryceratops "ANN", "C408", # Unnecessary `dict` call (rewrite as a literal) "COM", diff --git a/scal3/event_lib.py b/scal3/event_lib.py index 460ee2b3b..583df9da4 100644 --- a/scal3/event_lib.py +++ b/scal3/event_lib.py @@ -4614,7 +4614,7 @@ def _exportToIcsFpEvent( vevent += "\n".join(parts) fp.write(vevent) else: - raise RuntimeError + raise TypeError(f"invalid type {type(occur)} for occur") def exportToIcsFp(self, fp: "io.TextIOBase") -> None: currentTimeStamp = ics.getIcsTimeByEpoch(now()) diff --git a/scal3/locale_man.py b/scal3/locale_man.py index 72a8278fc..30503339e 100644 --- a/scal3/locale_man.py +++ b/scal3/locale_man.py @@ -241,9 +241,9 @@ def setData(self, data: dict): try: with open(fpath, encoding="utf-8") as fp: data = jsonToData(fp.read()) - except Exception as e: + except Exception: log.error(f"failed to load json file {fpath}") - raise e + raise langObj = LangData(fpath) langObj.setData(data) langDict[langObj.code] = langObj diff --git a/scal3/os_utils.py b/scal3/os_utils.py index 5ad7df02b..1e543eaf7 100644 --- a/scal3/os_utils.py +++ b/scal3/os_utils.py @@ -102,7 +102,7 @@ def kill(pid, signal=0): # no permissions if e.errno == 1: return False - raise e + raise def dead(pid): @@ -118,7 +118,7 @@ def dead(pid): # pid is not a child if e.errno == 10: return False - raise e + raise return dead diff --git a/scal3/s_object.py b/scal3/s_object.py index a6958e273..5d2557283 100644 --- a/scal3/s_object.py +++ b/scal3/s_object.py @@ -211,9 +211,9 @@ def load(cls, fs: FileSystem, *args): with fs.open(fpath) as fp: jsonStr = fp.read() data = jsonToData(jsonStr) - except Exception as e: + except Exception: if not cls.skipLoadExceptions: - raise e + raise else: if not cls.skipLoadNoFile: raise FileNotFoundError(f"{fpath} : file not found") @@ -369,10 +369,10 @@ def load(cls, fs: FileSystem, *args): except FileNotFoundError: if not cls.skipLoadNoFile: raise FileNotFoundError(f"{_file} : file not found") from None - except Exception as e: + except Exception: if not cls.skipLoadExceptions: log.error(f'error while opening json file "{_file}"') - raise e + raise else: lastEpoch, lastHash = updateBasicDataFromBson(data, _file, cls.name, fs) diff --git a/scal3/ui_gtk/event/manager.py b/scal3/ui_gtk/event/manager.py index e8f83b8f3..1ede679d9 100644 --- a/scal3/ui_gtk/event/manager.py +++ b/scal3/ui_gtk/event/manager.py @@ -2292,7 +2292,7 @@ def editTrash(self, _menuItem: "gtk.MenuItem | None" = None) -> None: def moveUp(self, path: list[int]) -> None: srcIter = self.treeModel.get_iter(path) if not isinstance(path, list): - raise RuntimeError(f"invalid {path = }") + raise TypeError(f"invalid {path = }") if len(path) == 1: if path[0] == 0: return @@ -2336,7 +2336,7 @@ def moveUp(self, path: list[int]) -> None: newGroup.save() ui.eventUpdateQueue.put("r", parentObj, self) else: - raise RuntimeError(f"invalid tree path {path}") + raise ValueError(f"invalid tree path {path}") newPath = self.treeModel.get_path(srcIter) if len(path) == 2: self.treev.expand_to_path(newPath) @@ -2345,7 +2345,7 @@ def moveUp(self, path: list[int]) -> None: def moveDown(self, path: list[int]) -> None: if not isinstance(path, list): - raise RuntimeError(f"invalid {path = }") + raise TypeError(f"invalid {path = }") srcIter = self.treeModel.get_iter(path) if len(path) == 1: if self.getRowId(srcIter) == -1: diff --git a/scal3/ui_gtk/layout.py b/scal3/ui_gtk/layout.py index 0b980e998..2e49257c2 100644 --- a/scal3/ui_gtk/layout.py +++ b/scal3/ui_gtk/layout.py @@ -101,7 +101,7 @@ def getWidget(self): return self._item item = self.initializer() if not isinstance(item, gtk.Widget): - raise ValueError(f"initializer returned non-widget: {type(item)}") + raise TypeError(f"initializer returned non-widget: {type(item)}") item.enableParam = self.enableParam if item.enableParam: item.enable = getattr(ui, item.enableParam) diff --git a/scal3/ui_gtk/mywidgets/dialog.py b/scal3/ui_gtk/mywidgets/dialog.py index 7e826a545..8ff15f1af 100644 --- a/scal3/ui_gtk/mywidgets/dialog.py +++ b/scal3/ui_gtk/mywidgets/dialog.py @@ -38,8 +38,6 @@ def waitingDo(self, func, *args, **kwargs): return result try: result = func(*args, **kwargs) - except Exception as e: - raise e finally: self.endWaiting() return result diff --git a/scal3/ui_gtk/stack.py b/scal3/ui_gtk/stack.py index c19b9d6b6..c51b0099a 100644 --- a/scal3/ui_gtk/stack.py +++ b/scal3/ui_gtk/stack.py @@ -183,7 +183,7 @@ def addPage(self, page: StackPage): raise ValueError(f"addPage: duplicate {pagePath=}") if not isinstance(widget, gtk.Widget): - raise ValueError(f"invalid {widget=}, {pagePath=}") + raise TypeError(f"invalid {widget=}, {pagePath=}") widget.show() if self._header and parentName: diff --git a/scal3/ui_gtk/starcal.py b/scal3/ui_gtk/starcal.py index e3c742220..65fdccc20 100755 --- a/scal3/ui_gtk/starcal.py +++ b/scal3/ui_gtk/starcal.py @@ -163,8 +163,8 @@ def createItems(self): fromlist=["CalObj"], ) CalObj = module.CalObj - except RuntimeError as e: - raise e + except RuntimeError: + raise except Exception: log.error(f"error importing mainWinItem {name}") log.exception("") @@ -172,9 +172,9 @@ def createItems(self): continue try: item = CalObj(win) - except Exception as e: + except Exception: log.error(f"{name=}, {module=}") - raise e + raise item.enable = enable # modify_bg_all( # item, diff --git a/scal3/utils.py b/scal3/utils.py index 15c56bbb8..4dc0a14ec 100644 --- a/scal3/utils.py +++ b/scal3/utils.py @@ -161,7 +161,7 @@ def __setitem__(self, arg: "int | str", value) -> None: self.keyList.append(arg) dict.__setitem__(self, arg, value) else: - raise ValueError( + raise TypeError( "Bad type argument given to StrOrderedDict.__setitem__" f": {type(arg)}", ) @@ -178,7 +178,7 @@ def __delitem__(self, arg: "int | str | slice") -> None: dict.__delitem__(self, key) self.keyList.__delitem__(arg) else: - raise ValueError( + raise TypeError( "Bad type argument given to StrOrderedDict.__delitem__" f": {type(arg)}", )