Skip to content
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 is fixed #13297 #13303

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ajaya0
Copy link

@ajaya0 ajaya0 commented Dec 26, 2024

I’ve added an overload for the sorted() function that explicitly handles tuples with elements that support SupportsRichComparison. This ensures that the tuples only contain comparable elements, and the type checker will catch any issues before runtime.

Explain :
Added a Specialized Overload for Tuples:

The third overload explicitly specifies that when sorting tuples, all elements in the tuple must satisfy SupportsRichComparison.
This ensures that sorted only works for tuples where every element is fully comparable.If a key function is provided, the burden of ensuring comparability is transferred to the result of the key function.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

scipy (https://github.com/scipy/scipy)
- scipy/spatial/tests/test_spherical_voronoi.py:194: error: Value of type variable "SupportsRichComparisonT" of "sorted" cannot be "floating[Any]"  [type-var]
+ scipy/spatial/tests/test_spherical_voronoi.py:194: error: Need type annotation for "closest"  [var-annotated]

jax (https://github.com/google/jax)
+ jax/_src/interpreters/mlir.py:1168: error: Argument 1 to "tuple" has incompatible type "list[tuple[Never, ...]]"; expected "Iterable[str]"  [arg-type]

spack (https://github.com/spack/spack)
+ lib/spack/spack/binary_distribution.py:906: error: Need type annotation for "index"  [var-annotated]

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.

1 participant