issue_comments: 724768430
This data as json
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-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 ```python from typing import TypeVar T = TypeVar("T") def sort(lst: List[T]) -> List[T]):
return sorted(lst)
```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. However, we define dict keys as
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 |