From cb5cf498f0cf8dd93cc8c04c4f727103e7a4c96f Mon Sep 17 00:00:00 2001 From: Kimoon Han Date: Wed, 11 Dec 2024 13:32:55 +0900 Subject: [PATCH] style: cleanup unreachable code --- .pre-commit-config.yaml | 2 +- pyproject.toml | 1 + src/erlab/accessors/fit.py | 3 --- src/erlab/interactive/imagetool/core.py | 3 --- src/erlab/interactive/imagetool/slicer.py | 6 +++--- src/erlab/interactive/utils.py | 9 +++++---- src/erlab/plotting/annotations.py | 5 +---- src/erlab/plotting/general.py | 8 -------- src/erlab/utils/misc.py | 14 +++++++++----- 9 files changed, 20 insertions(+), 31 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9f273b50..17805407 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -37,7 +37,7 @@ repos: hooks: # Run the linter. - id: ruff - args: [ --fix ] + args: [ --fix, --show-fixes ] # Run the formatter. - id: ruff-format diff --git a/pyproject.toml b/pyproject.toml index 091cd6e0..553ccb4c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -250,6 +250,7 @@ plugins = ["numpy.typing.mypy_plugin"] warn_unused_configs = true warn_redundant_casts = true warn_unused_ignores = true +warn_unreachable = true allow_redefinition = true check_untyped_defs = false exclude = [ diff --git a/src/erlab/accessors/fit.py b/src/erlab/accessors/fit.py index f8f4d940..5618fd38 100644 --- a/src/erlab/accessors/fit.py +++ b/src/erlab/accessors/fit.py @@ -267,9 +267,6 @@ def __call__( if parallel_kw is None: parallel_kw = {} - if kwargs is None: - kwargs = {} - is_dask: bool = not ( self._obj.chunksizes is None or len(self._obj.chunksizes) == 0 ) diff --git a/src/erlab/interactive/imagetool/core.py b/src/erlab/interactive/imagetool/core.py index 41c01c76..9b78673d 100644 --- a/src/erlab/interactive/imagetool/core.py +++ b/src/erlab/interactive/imagetool/core.py @@ -1013,9 +1013,6 @@ def apply_func(self, func: Callable[[xr.DataArray], xr.DataArray] | None) -> Non """ # self._data is original data passed to `set_data` # self.data is the current data transformed by ArraySlicer - if self._data is None: - return - self._applied_func = func if self._applied_func is None: diff --git a/src/erlab/interactive/imagetool/slicer.py b/src/erlab/interactive/imagetool/slicer.py index 09d8467d..7bc2602b 100644 --- a/src/erlab/interactive/imagetool/slicer.py +++ b/src/erlab/interactive/imagetool/slicer.py @@ -602,7 +602,7 @@ def set_bins( @QtCore.Slot(int, int, int, bool, result=list) def set_bin( - self, cursor: int, axis: int, value: int, update: bool = True + self, cursor: int, axis: int, value: int | None, update: bool = True ) -> list[int]: if value is None: return [] @@ -639,7 +639,7 @@ def set_indices(self, cursor: int, value: list[int], update: bool = True) -> Non @QtCore.Slot(int, int, int, bool, result=list) def set_index( - self, cursor: int, axis: int, value: int, update: bool = True + self, cursor: int, axis: int, value: int | None, update: bool = True ) -> list[int | None]: if value is None: return [] @@ -696,7 +696,7 @@ def set_value( self, cursor: int, axis: int, - value: float, + value: float | None, update: bool = True, uniform: bool = False, ) -> list[int | None]: diff --git a/src/erlab/interactive/utils.py b/src/erlab/interactive/utils.py index c6e478cb..6a27f1b6 100644 --- a/src/erlab/interactive/utils.py +++ b/src/erlab/interactive/utils.py @@ -934,7 +934,7 @@ def fixed(self): return self.check.isChecked() return False - def setFixed(self, value: bool) -> None: + def setFixed(self, value: bool | QtCore.Qt.CheckState) -> None: if isinstance(value, QtCore.Qt.CheckState): if value == QtCore.Qt.CheckState.Unchecked: value = False @@ -1064,10 +1064,11 @@ def getMenu(self): return self.menu def getPlotItem(self) -> pg.PlotItem | None: - p = self + p: Self | None = self while True: try: - p = p.parentItem() + if p is not None: + p = p.parentItem() except RuntimeError: return None if p is None: @@ -1685,7 +1686,7 @@ def __init__( raise ValueError("Orientation must be 'vertical' or 'horizontal'.") self.cut_to_data = cut_to_data - self.input: None | xr.DataArray = None + self.input: None | xr.DataArray | npt.NDArray = None self.initialize_layout(num_ax) diff --git a/src/erlab/plotting/annotations.py b/src/erlab/plotting/annotations.py index bec447e9..8ef7767d 100644 --- a/src/erlab/plotting/annotations.py +++ b/src/erlab/plotting/annotations.py @@ -606,7 +606,7 @@ def label_subplots_nature( | Literal[ "xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large" ] - ) = 8, + ) = "medium", **kwargs, ) -> None: r"""Labels subplots with automatically generated labels. @@ -672,9 +672,6 @@ def label_subplots_nature( trans = matplotlib.transforms.ScaledTranslation( offset[0] / 72, offset[1] / 72, axlist[i].get_figure().dpi_scale_trans ) - - if fontsize is None: - fontsize = "medium" axlist[i].figure.text( # axlist[i].text( 0.0, diff --git a/src/erlab/plotting/general.py b/src/erlab/plotting/general.py index 584d7e37..fec70330 100644 --- a/src/erlab/plotting/general.py +++ b/src/erlab/plotting/general.py @@ -304,8 +304,6 @@ def plot_array( if func_args is None: func_args = {} - if isinstance(arr, np.ndarray): - arr = xr.DataArray(arr) if arr.ndim != 2: raise ValueError("Input array must be 2D") @@ -516,12 +514,6 @@ def plot_array_2d( if normalize_with_larr: carr = carr / larr - if lnorm is None: - lnorm = plt.Normalize() - - if cnorm is None: - cnorm = plt.Normalize() - cmap_img, img = gen_2d_colormap( larr.values, carr.values, diff --git a/src/erlab/utils/misc.py b/src/erlab/utils/misc.py index 3f6e5e2d..d6fbed07 100644 --- a/src/erlab/utils/misc.py +++ b/src/erlab/utils/misc.py @@ -12,11 +12,15 @@ def _convert_to_native(obj: list[Any]) -> list[Any]: """Convert a nested list of numpy objects to native types.""" - if isinstance(obj, np.generic): - return obj.item() - if isinstance(obj, list): - return [_convert_to_native(item) for item in obj] - return obj + + def _convert(obj: Any) -> Any: + if isinstance(obj, np.generic): + return obj.item() + if isinstance(obj, list): + return [_convert(item) for item in obj] + return obj + + return _convert(obj) def _find_stack_level() -> int: