types(unknown_sort): constrain items to Comparable - #15411
Closed
AuroraAeon wants to merge 1 commit into
Closed
AuroraAeon wants to merge 1 commit into
AuroraAeon wants to merge 1 commit into
Conversation
Bind merge_sort's element type to a Comparable Protocol so the signature says "a list of items that can be compared with each other" instead of a bare list, and keeps the element type in the return. start/end are now annotated list[T] so the generic survives the local bindings. Adds doctests for a comparable non-int type (strings, floats) and for the failure mode: mixing non-comparable items must raise TypeError rather than silently mis-sort. The test battery picks the sort up for the shared cases and for the rejection check. It is imported under an alias because sorts/merge_sort.py already exports a merge_sort; the parametrize id therefore shows up as merge_sort0/merge_sort1, the same way shrink_shell_sort already shares the shell_sort id.
37 of 41 tasks
Contributor
Author
|
Closing this one myself, for two reasons:
Happy to re-open this if the verdict is "keep the file" — the typing change itself is mechanical. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #15234
What
sorts/unknown_sort.py— bindmerge_sort's element type to aComparableProtocolso the signature states "a list of items that can be compared with each other" instead of a barelist, and keeps the element type in the return:start/endare annotatedlist[T]so the element type survives the local bindings instead of degrading tolist[Any].Tests
Doctests added for:
["banana", "apple", "cherry"]and[3.14, 1.5, 2.7]merge_sort([1, "a"])raisesTypeErrorrather than silently mis-sortingtests/test_sorts.py:SORTSbattery (all 11 cases, including thePersonandDogdataclass cases)test_sort_rejects_non_comparable_itemsRun with:
→ 321 passed, 12 of them new.
ruff checkandruff format --checkare clean, and the widersorts/tier is unaffected (90 passed).Note on the import alias
sorts/merge_sort.pyalready exports amerge_sort, so the import is aliased asunknown_merge_sort. The parametrize id therefore readsmerge_sort0/merge_sort1rather than the alias name — the same wayshrink_shell_sortalready shares theshell_sortid in this battery. No behaviour change; only annotations and doctests were added.