From 22bbfd6d7882050f92accd7cc99d8ebceacc7bd5 Mon Sep 17 00:00:00 2001 From: Richard Kleijn Date: Thu, 7 Jan 2021 01:46:33 +0100 Subject: [PATCH 1/2] improve typing of OrderedSet --- xarray/core/missing.py | 2 +- xarray/core/utils.py | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/xarray/core/missing.py b/xarray/core/missing.py index f608468ed9f..a2cdae55cb1 100644 --- a/xarray/core/missing.py +++ b/xarray/core/missing.py @@ -651,7 +651,7 @@ def interp(var, indexes_coords, method, **kwargs): out_dims.update(indexes_coords[d][1].dims) else: out_dims.add(d) - result = result.transpose(*tuple(out_dims)) + result = result.transpose(*out_dims) return result diff --git a/xarray/core/utils.py b/xarray/core/utils.py index c0e2635d084..d6e85187f04 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -9,7 +9,6 @@ import warnings from enum import Enum from typing import ( - AbstractSet, Any, Callable, Collection, @@ -509,17 +508,14 @@ class OrderedSet(MutableSet[T]): __slots__ = ("_d",) - def __init__(self, values: AbstractSet[T] = None): + def __init__(self, values: Iterable[T] = None): self._d = {} if values is not None: - # Disable type checking - both mypy and PyCharm believe that - # we're altering the type of self in place (see signature of - # MutableSet.__ior__) - self |= values # type: ignore + self.update(values) # Required methods for MutableSet - def __contains__(self, value: object) -> bool: + def __contains__(self, value: Any) -> bool: return value in self._d def __iter__(self) -> Iterator[T]: @@ -536,9 +532,9 @@ def discard(self, value: T) -> None: # Additional methods - def update(self, values: AbstractSet[T]) -> None: - # See comment on __init__ re. type checking - self |= values # type: ignore + def update(self, values: Iterable[T]) -> None: + for v in values: + self._d[v] = None def __repr__(self) -> str: return "{}({!r})".format(type(self).__name__, list(self)) From 0cc4e6ec58db285f28508c256be3cc380763ba29 Mon Sep 17 00:00:00 2001 From: Richard Kleijn Date: Thu, 7 Jan 2021 09:57:07 +0100 Subject: [PATCH 2/2] change Any to Hashable --- xarray/core/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/utils.py b/xarray/core/utils.py index d6e85187f04..1eff91e3608 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -515,7 +515,7 @@ def __init__(self, values: Iterable[T] = None): # Required methods for MutableSet - def __contains__(self, value: Any) -> bool: + def __contains__(self, value: Hashable) -> bool: return value in self._d def __iter__(self) -> Iterator[T]: