Skip to content

sorts: make odd_even_sort generic over any comparable type (Part of #15234) - #15378

Open
aayushgupta7725 wants to merge 7 commits into
TheAlgorithms:masterfrom
aayushgupta7725:odd-even-sort-comparable
Open

aayushgupta7725 wants to merge 7 commits into
TheAlgorithms:masterfrom
aayushgupta7725:odd-even-sort-comparable

Conversation

@aayushgupta7725

Copy link
Copy Markdown

Adds a Comparable-bound TypeVar (matching the pattern used in insertion_sort.py), doctests covering strings, floats, and the non-comparable TypeError case, and registers odd_even_sort in the shared test_sort_rejects_non_comparable_items test.

Part of #15234

@priya-sundaram-dev, this is ready whenever you get a chance to review.

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

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".

…heAlgorithms#15234)

Adds a Comparable-bound TypeVar (matching the pattern used in insertion_sort.py), doctests covering strings, floats, and the non-comparable TypeError case, and registers odd_even_sort in the shared test_sort_rejects_non_comparable_items test.

Part of TheAlgorithms#15234
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed merge conflicts Open a new PR or rebase on the latest commit enhancement This PR modified some existing files labels Sep 18, 2026
@algorithms-keeper algorithms-keeper Bot removed the merge conflicts Open a new PR or rebase on the latest commit label Sep 18, 2026
@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Sep 18, 2026

@priya-sundaram-dev priya-sundaram-dev 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.

Thanks @aayushgupta7725 — this reads cleanly and the added doctests (str, float, and the mixed-type TypeError) are a nice touch. One small simplification:

The module-level Comparable/TypeVar boilerplate is now redundant with the PEP 695 syntax you used on the signature. def odd_even_sort[T: Comparable](...) introduces its own scoped T, so the top-level T = TypeVar("T", bound="Comparable") is dead — nothing references it. You can drop that line and the TypeVar import (keep Protocol, and Any is still used inside the protocol):

from collections.abc import MutableSequence
from typing import Any, Protocol


class Comparable(Protocol):
    def __gt__(self, other: Any, /) -> bool: ...


def odd_even_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequence[T]:

Tiny consistency nit while you're there: the algorithm branches on collection[i] > collection[i + 1], so the protocol bound is more accurate as __gt__ than __lt__ (matches the operator the function actually relies on). Not a CI blocker — ty is happy either way — just makes the bound say what the code does.

Otherwise LGTM. 👍

@priya-sundaram-dev priya-sundaram-dev 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.

LGTM — this cleanly mirrors the pattern ( protocol + PEP 695 on a ), and registering it in / the non-comparable rejection test is exactly right. I ran the four new doctests locally and they all pass, including the → case.

One optional nit, no need to block on it: uses (unquoted, since the class is already defined above), whereas here it is . Both work; dropping the quotes would keep it byte-for-byte consistent with the reference file. Nice work.

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

(Reposting my review note — GitHub ate the code spans in the previous one.)

Approving: this mirrors the insertion_sort.py pattern exactly — Comparable protocol + PEP 695 [T: Comparable] over a MutableSequence[T], plus registration in the shared test_sort_matches_builtin / non-comparable rejection tests. I ran the four new doctests locally and all pass, including the [1, "a"]TypeError case.

One optional nit (don't block on it): insertion_sort.py uses bound=Comparable unquoted, whereas this uses bound="Comparable". Both work; dropping the quotes keeps it byte-for-byte consistent with the reference file. Nice work.

@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Sep 19, 2026
@aayushgupta7725

Copy link
Copy Markdown
Author

Thanks for the review! I've pushed both requested changes:

  • Removed the now-redundant module-level T = TypeVar("T", bound=Comparable) and the unused TypeVar import, since [T: Comparable] on the function signature already scopes its own T.
  • Changed the Comparable protocol to require __gt__ instead of __lt__, to match the > operator the algorithm actually uses.

Also fixed a ruff import-formatting check that was failing separately (missing blank line after the imports) — that should be green now too.

Let me know if anything else needs adjusting!

@priya-sundaram-dev priya-sundaram-dev 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.

Both changes look great, thanks for the quick turnaround.

  • Dropping the module-level TypeVar in favor of the inline [T: Comparable] scoping is the right call — matches the insertion_sort.py pattern exactly.
  • Switching the Comparable protocol to __gt__ now correctly mirrors the > the algorithm actually uses.

I re-ran the 7 doctests on the latest commit (5d21113) locally — all pass, including the [1, "a"] → TypeError case. LGTM. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants