Add missing type hints to hill_climbing.py#14260
Add missing type hints to hill_climbing.py#14260anandvenugopal-tech wants to merge 5 commits intoTheAlgorithms:masterfrom
Conversation
This commit adds the missing type annotations to searches/hill_climbing.py. - Added type annotation for function_to_optimize using Callable [[int, int], int] - Added return type hints to get_neighbors, __hash__, __eq__, and __str__ - Added missing type hint for search_prob in hill_climbing() - Improved type clarity while preserving existing logic - Used modern Python type hints (PEP 585) This improves readability and typing consistency across the repository.
for more information, see https://pre-commit.ci
llukito
left a comment
There was a problem hiding this comment.
Left a suggestion regarding the type hints for the objective function.
searches/hill_climbing.py
Outdated
| x: int, | ||
| y: int, | ||
| step_size: int, | ||
| function_to_optimize: Callable[[int, int], int], |
There was a problem hiding this comment.
Nice work adding these hints!
I have one suggestion regarding function_to_optimize: Currently, it is typed to return an int (Callable[[int, int], int]). However, in optimization problems, objective functions very often return float values (costs, fitness scores, etc.).
Restricting it to int might flag valid use cases as errors. Would it be safer to type it as Callable[[int, int], float] or Callable[[int, int], int | float]?
|
The CI failure confirms that the int | float change is correct, but we missed the return type hint for the score() method (or whichever method is at line 42). You just need to update that method's return annotation to -> int | float so it matches the input function. |
Mypy reported an incompatible return type because function_to_optimize may return float values. Updated score() return type from int to int | float for full compatibility.
|
Thanks for the suggestion! |
This PR adds missing type hints to searches/hill_climbing.py.
Improves readability and typing consistency across the repository.
Describe your change:
Checklist: