html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue https://github.com/pydata/xarray/issues/4571#issuecomment-725455918,https://api.github.com/repos/pydata/xarray/issues/4571,725455918,MDEyOklzc3VlQ29tbWVudDcyNTQ1NTkxOA==,10194086,2020-11-11T14:32:08Z,2020-11-11T14:32:08Z,MEMBER,"Jup, given the effort required for 1. I also vote for 2. I learned a lot about typing/ mypy, though...","{""total_count"": 1, ""+1"": 0, ""-1"": 0, ""laugh"": 1, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,739281259 https://github.com/pydata/xarray/issues/4571#issuecomment-724768430,https://api.github.com/repos/pydata/xarray/issues/4571,724768430,MDEyOklzc3VlQ29tbWVudDcyNDc2ODQzMA==,10194086,2020-11-10T15:16:49Z,2020-11-11T14:30:51Z,MEMBER,"For context, mypy 0.790 now requires `__lt__` if something should be sorted. Thus the following fails (used to work): ```python from typing import TypeVar T = TypeVar(""T"") def sort(lst: List[T]) -> List[T]): return sorted(lst) ``` which returns the error message above (`_LT` stands for `_LowerThan`). The workaround is to define a `TypeVar` that is `Sortable`:
```python from typing import TypeVar, List, Protocol class Sortable(Protocol): def __lt__(self, __other: Any) -> bool: ... SortableT = TypeVar(""SortableT"", bound=Sortable) def sort(lst: List[SortableT]) -> List[SortableT]): return sorted(lst) ```
What we want is a [sortable dict key](https://github.com/pydata/xarray/blob/7ce0110f727b37a776d509174365cf0905163234/xarray/core/utils.py#L465-L466). However, we define dict keys as `Hashable` (e.g. [dims](https://github.com/pydata/xarray/blob/f10a6fe273402015a7caab26bf66d6923b35169b/xarray/core/dataset.py#L688-L697)) which is not sortable (does not have `__lt__`). I see two possibilities: 1. type all keys of a `SortedKeysDict` as `SortableHashable` instead of `Hashable` (that would be a lot) 2. ignore this and add `# type: ignore` to [core/utils.py#L466](https://github.com/pydata/xarray/blob/7ce0110f727b37a776d509174365cf0905163234/xarray/core/utils.py#L466) But maybe there is a better way? Here is how 1. could look like (simplified):
```python import collections from typing import Any, Dict, Iterator, Mapping, Protocol, TypeVar class HashableSortable(Protocol): def __lt__(self, __other: Any) -> bool: ... def __hash__(self) -> int: ... HashableSortableK = TypeVar(""HashableSortableK"", bound=HashableSortable) V = TypeVar(""V"") # use UserDict for brevity class SortedKeysDict(collections.UserDict[HashableSortableK, V]): # used to be [K, V] def __iter__(self) -> Iterator[HashableSortableK]: return iter(sorted(self.data)) # now we need to update the typing of all uses of `SortedKeysDict`, e.g. dims class Dataset: _dims: Dict[HashableSortable, int] # used to be [Hashable, int] def __init__(self, dims) -> None: self._dims = dims @property def dims(self) -> Mapping[HashableSortable, int]: return SortedKeysDict(self._dims) ```
","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,739281259