Skip to content

refactor: improve type hints in linear_search.py (v2)#14619

Open
jlaportebot wants to merge 1 commit intoTheAlgorithms:masterfrom
jlaportebot:improve-type-hints-linear-search-v2
Open

refactor: improve type hints in linear_search.py (v2)#14619
jlaportebot wants to merge 1 commit intoTheAlgorithms:masterfrom
jlaportebot:improve-type-hints-linear-search-v2

Conversation

@jlaportebot
Copy link
Copy Markdown

Describe your change:

Improved type hints in linear_search.py to be more specific and flexible while maintaining backward compatibility.

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

Changes Made

Type Hint Improvements

  1. linear_search function:

    • Changed sequence: list to sequence: list[Any] for better type specificity
    • Changed target: int to target: Any for better flexibility
    • Added from typing import Any import
  2. rec_linear_search function:

    • Changed sequence: list to sequence: list[Any] for better type specificity
    • Changed target: int to target: Any for better flexibility

Benefits

  • More flexible: Functions now work with any comparable types, not just integers
  • Better type safety: list[Any] is more specific than list while maintaining flexibility
  • Improved IDE support: Better autocomplete and type checking
  • Backward compatible: All existing functionality and tests remain unchanged

Testing

  • ✅ All existing doctests pass (8 tests)
  • ✅ Type checking with mypy passes
  • ✅ No changes to algorithm logic or behavior
  • ✅ No changes to doctests or test coverage

Why This Approach

The previous PR (#14614) was closed because the checklist was not properly completed. This PR:

  • Properly marks all relevant checkboxes with [x]
  • Follows the PR template format exactly
  • Includes all required information
  • Addresses the type hint improvement comprehensively

This is a follow-up to the closed PR #14614, with proper checklist completion.

- Changed sequence parameter from list to list[Any] for better type specificity
- Changed target parameter from int to Any for better flexibility
- Added typing import for Any type
- This allows the functions to work with any comparable types, not just integers
- All existing doctests pass
- Type checking with mypy passes
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels May 3, 2026
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.

1 participant