-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
sorted() does not enforce comparable items in an iterable of tuples #13297
Comments
Hi @jcmacdon , |
In my example I think the main issue here is actually not This code typechecks ok: from _typeshed import SupportsRichComparison
tuple1: SupportsRichComparison = (1, object())
tuple2: SupportsRichComparison = (1, object())
items = [tuple1, tuple2]
sorted(items) but if you run it, you will get a runtime error: TypeError: '<' not supported between instances of 'object' and 'object' |
Hi @jcmacdon, Thanks for the clarification! I see the issue now. The sorted() function was allowing tuples containing non-comparable types to be sorted, which caused the TypeError at runtime. |
This is a problem with the You'd need something like this recursive type definition with type intersection and type inversion to handle this case properly type Comparable = (SupportsRichComparison & Not[tuple[Any, ...]) | tuple[Comparable, ...] |
I agree that type checkers should complain about the example and the example given by @KotlinIsland in #13499: sorted(["a", 1]) But per @Daverball, I don't think there is anything that we can do to improve the situation without changes to type system. So I'm closing this as inexpressible for now. If you have any ideas that we have overlooked, please send an exploratory PR. |
sorted()
does not enforce that every item in the tuple must be comparable:typeshed/stdlib/builtins.pyi
Lines 1762 to 1767 in e5c5318
The text was updated successfully, but these errors were encountered: