-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with sorted() and _LT #9582
Comments
Actually adding items fails at runtime: from typing import Sequence
class A:
pass
d: Sequence[A] = (A(), A())
s = sorted(d)
print("OK")
|
I saw the same |
This is a new trick mypy has learned. The error message is a little inscrutable, but has already been improved on master (python/typeshed#4579). As Sebastian mentions, in this case the error is valid, so I'm closing. |
When using generics, you can get around this by bounding your C = TypeVar("C", bound="Comparable")
class Comparable(Protocol):
@abstractmethod
def __eq__(self, other: Any) -> bool:
...
@abstractmethod
def __lt__(self: C, other: C) -> bool:
...
def __gt__(self: C, other: C) -> bool:
return (not self < other) and self != other
def __le__(self: C, other: C) -> bool:
return self < other or self == other
def __ge__(self: C, other: C) -> bool:
return not (self < other) This idea comes from another mypy issue. |
Yup. The ingredients you need for many fixes are:
and as mentioned^, you can use Note the error message on master is also improved: Finally, the issue for adding this protocol to typing / typing_extensions is here: python/typing_extensions#9 |
The following code:
produces the following output:
The code seems perfectly valid, so
mypy
reaction looks a false positive.The text was updated successfully, but these errors were encountered: