From 65a807f7262a1b28c4142bdc9a74309718b7c79f Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Fri, 9 Aug 2024 11:49:57 +0200 Subject: [PATCH] Satisfy mypy 1.11.1 Temporarily ignore safe-super errors (may be python/mypy#14757). --- genno/core/attrseries.py | 4 ++-- genno/core/operator.py | 2 ++ genno/operator.py | 15 +++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/genno/core/attrseries.py b/genno/core/attrseries.py index fba89fdf..bb63add9 100644 --- a/genno/core/attrseries.py +++ b/genno/core/attrseries.py @@ -280,7 +280,7 @@ def clip( if pandas_version() < Version("2.1.0"): return self._replace(pd.Series(self).clip(min, max)) else: - return super(pd.Series, self).clip(min, max) + return super().clip(min, max) # type: ignore [safe-super] def drop(self, label): """Like :meth:`xarray.DataArray.drop`.""" @@ -610,7 +610,7 @@ def where( raise NotImplementedError("where(…, drop=True)") elif axis is not None or inplace is not False: raise NotImplementedError("where(…, axis=…) or where(…, inplace=…)") - return super().where(cond, other) + return super().where(cond, other) # type: ignore [safe-super] @property def xindexes(self): # pragma: no cover diff --git a/genno/core/operator.py b/genno/core/operator.py index f999adf0..7340ef83 100644 --- a/genno/core/operator.py +++ b/genno/core/operator.py @@ -93,8 +93,10 @@ def decorator(func: Callable) -> "Operator": # which will skip documenting items that have the same __doc__ as their # class result = update_wrapper(klass(), func, updated=()) + assert isinstance(result, Operator) # For mypy if helper: + # Register the provided `helper` method for the class result.helper(helper) return result diff --git a/genno/operator.py b/genno/operator.py index 032a34d9..54ac1c9a 100644 --- a/genno/operator.py +++ b/genno/operator.py @@ -946,15 +946,18 @@ def select( coords = qty.coords idx = dict() for dim, labels in indexers.items(): - s = is_scalar(labels) - # Check coords equal to (scalar) label or contained in (iterable of) labels - op1 = partial(operator.eq if s else operator.contains, labels) - # Take either 1 item (scalar label) or all (collection of labels) - ig = operator.itemgetter(0 if s else slice(None)) + if is_scalar(labels): + # Check coords equal to scalar label + op1 = partial(operator.eq, labels) + # Take 1 item + item: Union[int, slice] = 0 + else: + # Check coords contained in collection of labels; take all + op1, item = partial(operator.contains, set(labels)), slice(None) try: # Use only the values from `indexers` (not) appearing in `qty.coords` - idx[dim] = ig(list(filter(lambda x: op2(op1(x)), coords[dim].data))) + idx[dim] = list(filter(lambda x: op2(op1(x)), coords[dim].data))[item] except IndexError: raise KeyError(f"value {labels!r} not found in index {dim!r}")