Skip to content

Conversation

@keewis
Copy link
Collaborator

@keewis keewis commented Nov 7, 2020

pre-commit autoupdate also wants to update mypy to 0.790, but running that prints a error:

xarray/core/utils.py:466: error: Value of type variable "_LT" of "sorted" cannot be "K"

I'm not sure how to fix that, so I'll leave the upgrade to someone else.

@mathause
Copy link
Collaborator

mathause commented Nov 8, 2020

See: python/mypy#9582

Maybe K should be replaced with Hashable or K = TypeVar("K", bound=Hashable) (or maybe it has to be "Comparable" as suggested in the issue).

edit: see also https://github.com/python/typeshed/blob/f35e79fd24d896678abd1b682c2b2429a546500b/stdlib/2and3/builtins.pyi#L88

@mathause
Copy link
Collaborator

mathause commented Nov 9, 2020

I am not entirely sure what I say below is correct. So would be good if someone with more experience with python typing could have a look.


I think the solution is to add # type: ignore to

return iter(sorted(self.mapping))


The alternative would be to define:

class HashableSortable(Protocol):
    def __lt__(self, __other: Any) -> bool: ...
    def __hash__(self) -> int: ...

SortableHashableT = TypeVar("SortableHashableT", bound=HashableSortable)

but then we would probably need to replace all instances of "Dict[Hashable, int]" with "Dict[SortableHashable, int]".

Unfortunately we cannot do

class Sortable(Protocol):
    def __lt__(self, __other: Any) -> bool: ...

SortableT = TypeVar("SortableT", bound=HashableSortable, covariant=True)

because covariant type variable cannot be used as a parameter.

@keewis
Copy link
Collaborator Author

keewis commented Nov 9, 2020

let's continue this in #4571

@keewis keewis merged commit e71c7b4 into pydata:master Nov 10, 2020
@keewis keewis deleted the update-pre-commit branch November 10, 2020 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants