Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd SupportsLessThan to typing and typing_extensions #760
Comments
|
Sounds like a good idea. In fact, adding protocols for other dunder protocols makes sense, too. See for example the dunder protocols in builtins. |
|
Adding it to typing(_extensions) is relatively painful though because it ties us to Python's release cycle. An alternative could be to release our |
|
Oops, I was misreading this. I was talking about |
|
The Example given above was this Item = TypeVar("Item")
def try_sort(iterable: Iterable[Item]) -> Iterable[Item]:
# Pulled from pandas.core.common since
# it was deprecated and removed in 1.1
listed = list(iterable)
try:
return sorted(listed)
except TypeError:
return listed
So, I changed it to str and it worked def try_sort(iterable: Iterable[str]) -> Iterable[str]:
# Pulled from pandas.core.common since
# it was deprecated and removed in 1.1
listed = list(iterable)
try:
return sorted(listed)
except TypeError:
return listedI am trying to figure out what was the general solution to this issue? |
Note that typing_extensions is not tied to the CPython release cycle. |
|
The general solution if we add this to
If this isn't added to But now that I'm looking closely at your code, some more comments:
@everyoneelse It sounds like no one is opposed, but no one feels strongly either? In that case, maybe we hold off and see if people complain, rather than presumptively making a decision based on mypy_primer. |
|
@hauntsaninja, I am facing the same problem with
The lines involved are the ones below |
|
@guilhermeleobas Hmm, your false positive is a little tricky. The problem is that there isn't a good way to type the return type of Some possibilities:
|
|
Thanks @hauntsaninja, I will give it a try. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

The latest version of mypy / typeshed uses a protocol for things passed to sorted / sort. As measured by mypy_primer, this was a fairly disruptive change. To correctly type some usage of sort / sorted, you need the protocol, and it's pretty hard for users to figure this out, eg: elastic/eland#283 (comment)
Adding the protocol to typing / typing_extensions would make this solution more discoverable and involve less end user protocol redefinition boilerplate when used.