Skip to content

fix(sorts): remove shadowed type parameter from bogo_sort is_sorted helper - #15605

Open
hardikkaurani wants to merge 1 commit into
TheAlgorithms:masterfrom
hardikkaurani:fix/bogo-sort-shadowed-typevar
Open

hardikkaurani wants to merge 1 commit into
TheAlgorithms:masterfrom
hardikkaurani:fix/bogo-sort-shadowed-typevar

Conversation

@hardikkaurani

Copy link
Copy Markdown

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?

Problem & Context

In sorts/bogo_sort.py, bogo_sort is generic over T: Comparable using PEP 695 syntax:

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

Inside bogo_sort, the nested helper function is_sorted re-declared [T: Comparable]:

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

Because T is already in scope from the enclosing function, this shadows T and causes ty check to emit:

error[shadowed-type-variable]: Generic function `is_sorted` uses type variable `T` already bound by an enclosing scope
  --> sorts/bogo_sort.py:46:9

Fix

Remove [T: Comparable] from is_sorted so it uses the enclosing type variable T, which matches how other nested helper functions are typed across sorts/ (e.g. merge in sorts/merge_sort.py and circle_sort_util in sorts/circle_sort.py).

Validation

  • uvx ty check sorts/bogo_sort.py -> All checks passed (0 diagnostics).
  • uvx pytest sorts/bogo_sort.py --doctest-modules -> 1 passed.
  • uvx pytest tests/test_sorts.py -k test_bogo_sort -> 1 passed.
  • uvx ruff check sorts/bogo_sort.py -> All checks passed.
  • uvx ruff format --check sorts/bogo_sort.py -> 1 file already formatted.

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.

…elper

In PEP 695 syntax, T is already bound by the outer function bogo_sort[T: Comparable]. Re-declaring [T: Comparable] on the nested is_sorted helper shadows T and causes ty check to emit error[shadowed-type-variable].

Remove [T: Comparable] from is_sorted so it uses the enclosing type variable T, matching other sorting algorithms in sorts/ (e.g. circle_sort, merge_sort).

Validation:
- uvx ty check sorts/bogo_sort.py -> All checks passed
- uvx pytest sorts/bogo_sort.py --doctest-modules -> 1 passed
- uvx pytest tests/test_sorts.py -k test_bogo_sort -> 1 passed
- uvx ruff check / format -> clean
Copilot AI balanced review requested due to automatic review settings October 19, 2026 11:13
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 19, 2026

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.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@algorithms-keeper algorithms-keeper Bot added the enhancement This PR modified some existing files label Oct 19, 2026
@hardikkaurani

Copy link
Copy Markdown
Author

Hi @cclauss,

I've opened this small PR to resolve a shadowed type parameter in sorts/bogo_sort.py.

Overview:

In bogo_sort[T: Comparable], T is already in scope from the outer function via PEP 695 syntax. The nested helper function def is_sorted[T: Comparable](collection: list[T]) re-declared [T: Comparable], shadowing T and triggering error[shadowed-type-variable] during ty check.

Removing [T: Comparable] from is_sorted allows it to use the enclosing type variable T, consistent with how other inner helper functions across sorts/ are structured (e.g. merge in merge_sort.py).

  • uvx ty check sorts/bogo_sort.py: All checks passed (0 diagnostics).
  • uvx pytest sorts/bogo_sort.py --doctest-modules: 1 passed.
  • uvx pytest tests/test_sorts.py -k test_bogo_sort: 1 passed.
  • uvx ruff check / format: Clean.

Whenever you have a chance, could you please take a look? Thank you!

This branch has not been deployed

No deployments
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