Skip to content

types(unknown_sort): constrain items to Comparable - #15411

Closed
AuroraAeon wants to merge 1 commit into
TheAlgorithms:masterfrom
AuroraAeon:types/unknown-sort-comparable
Closed

AuroraAeon wants to merge 1 commit into
TheAlgorithms:masterfrom
AuroraAeon:types/unknown-sort-comparable

Conversation

@AuroraAeon

Copy link
Copy Markdown
Contributor

Part of #15234

What

sorts/unknown_sort.py — bind merge_sort's element type to a Comparable Protocol so the signature states "a list of items that can be compared with each other" instead of a bare list, and keeps the element type in the return:

def merge_sort[T: Comparable](collection: list[T]) -> list[T]:

start/end are annotated list[T] so the element type survives the local bindings instead of degrading to list[Any].

Tests

Doctests added for:

  • a comparable non-int type — ["banana", "apple", "cherry"] and [3.14, 1.5, 2.7]
  • the failure mode — merge_sort([1, "a"]) raises TypeError rather than silently mis-sorting

tests/test_sorts.py:

  • added to the shared SORTS battery (all 11 cases, including the Person and Dog dataclass cases)
  • added to test_sort_rejects_non_comparable_items

Run with:

python -m pytest tests/test_sorts.py sorts/unknown_sort.py -q

→ 321 passed, 12 of them new. ruff check and ruff format --check are clean, and the wider sorts/ tier is unaffected (90 passed).

Note on the import alias

sorts/merge_sort.py already exports a merge_sort, so the import is aliased as unknown_merge_sort. The parametrize id therefore reads merge_sort0/merge_sort1 rather than the alias name — the same way shrink_shell_sort already shares the shell_sort id in this battery. No behaviour change; only annotations and doctests were added.

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.
Copilot AI lite review requested due to automatic review settings September 23, 2026 09:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@AuroraAeon

Copy link
Copy Markdown
Contributor Author

Closing this one myself, for two reasons:

  1. @cclauss raised the keep-vs-drop question upthread in sorts: make algorithms sort any comparable items, not just ints #15234. sorts/unknown_sort.py exports a function literally named merge_sort that is actually a double-ended min/max selection sort — it leans on min(), max() and list.remove() — so it duplicates selection_sort's idea under a misleading name.
  2. That is a repo-hygiene decision that should not ride along inside a typing PR.

Happy to re-open this if the verdict is "keep the file" — the typing change itself is mechanical.

@AuroraAeon AuroraAeon closed this Sep 23, 2026
@AuroraAeon
AuroraAeon deleted the types/unknown-sort-comparable branch September 23, 2026 11:08
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.

3 participants