Skip to content

typing: bind recursive insertion sort to Comparable protocol - #15472

Merged
cclauss merged 4 commits into
TheAlgorithms:masterfrom
Silverbullets1:typing/recursive-insertion-15234
Oct 1, 2026
Merged

cclauss merged 4 commits into
TheAlgorithms:masterfrom
Silverbullets1:typing/recursive-insertion-15234

Conversation

@Silverbullets1

Copy link
Copy Markdown
Contributor

Describe your change

  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Part of #15234 (the recursive_insertion_sort.py checkbox).

This PR types the recursive insertion sort for any comparable items:

  • insert_next was missing its TypeVar bound, so Pyright flagged the < operator on T. Both rec_insertion_sort and insert_next are now [T: Comparable] with the Comparable Protocol (__lt__(self, other: object, /) -> bool), matching the pattern already checked in for other sorts in this issue.
  • The adjacent-order check switched from collection[index - 1] <= collection[index] (which requires __le__) to not collection[index] < collection[index - 1], so the algorithm relies on __lt__ alone — same behavior, weaker protocol requirement.
  • New doctests: floats and ints sort in place together, and rec_insertion_sort([1, "a"], 2) raises TypeError (a silent mis-sort would be a correctness bug).

Verification:

$ uvx ruff check sorts/recursive_insertion_sort.py
All checks passed!

$ python -m pytest tests/test_sorts.py -q --no-header
444 passed in 0.37s

$ python -m pytest --doctest-modules sorts/recursive_insertion_sort.py -q --no-header
1 passed in 0.02s

The [3, 2, 3] and [3, 2, 4, 2] doctests are unchanged, so equal-element behavior is preserved.

Best,
Sam | Lead Engineer
DevilX Digital Labs
https://devilx-solution.vercel.app/

- insert_next was missing its TypeVar bound (Pyright: '<' operator invalid on T)
- Protocol __lt__ takes object, not Any; order check now relies on __lt__ only
- doctests: floats+ints mix, mixed [1, 'a'] raises TypeError

Part of TheAlgorithms#15234
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 1, 2026
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Oct 1, 2026
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Oct 1, 2026
@cclauss
cclauss merged commit ae429ad into TheAlgorithms:master Oct 1, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants