Repository: TheAlgorithms/Python

Issue:
[mypy] Need help to fix all `mypy` errors in the codebase
# Just one left to fix...
https://github.com/TheAlgorithms/Python/blob/master/mypy.ini#L5
* [x] other/least_recently_used.py
* [x] other/lfu_cache.py #5755
* [x] other/lru_cache.py #5755

---

__UPDATE:__ Our GitHub Actions now run `mypy --ignore-missing-imports` excluding those directories that fail that test.
* https://github.com/TheAlgorithms/Python/blob/master/mypy.ini#L5

Currently, we are not running `mypy` in our regular CI tests as there are a lot of errors in the entire codebase, which needs to be fixed. This won't be a one-person job, so we are asking for help from you. I cannot paste the entire message in here as there are around 600 of them, so here's just a gist of it:

```console
$ mypy --ignore-missing-imports .
strings/word_occurrence.py:17: error: Need type annotation for 'occurrence'
strings/min_cost_string_conversion.py:36: error: No overload variant of "__setitem__" of "list" matches argument types "int", "str"
strings/min_cost_string_conversion.py:36: note: Possible overload variants:
strings/min_cost_string_conversion.py:36: note:     def __setitem__(self, int, int) -> None
strings/min_cost_string_conversion.py:36: note:     def __setitem__(self, slice, Iterable[int]) -> None
strings/min_cost_string_conversion.py:40: error: No overload variant of "__setitem__" of "list" matches argument types "int", "str"
strings/min_cost_string_conversion.py:40: note: Possible overload variants:
strings/min_cost_string_conversion.py:40: note:     def __setitem__(self, int, int) -> None
strings/min_cost_string_conversion.py:40: note:     def __setitem__(self, slice, Iterable[int]) -> None
...
backtracking/n_queens_math.py:109: error: List comprehension has incompatible type List[str]; expected List[int]
backtracking/n_queens_math.py:110: error: Argument 1 to "append" of "list" has incompatible type "List[int]"; expected "List[str]"
backtracking/n_queens_math.py:149: error: Need type annotation for 'boards' (hint: "boards: List[<type>] = ...")
backtracking/minimax.py:15: error: "list" is not subscriptable, use "typing.List" instead
backtracking/knight_tour.py:6: error: "tuple" is not subscriptable, use "typing.Tuple" instead
backtracking/knight_tour.py:6: error: "list" is not subscriptable, use "typing.List" instead
...
```

# Guidelines to follow:

- Please make sure you read the [Contributing Guidelines](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md) first.
- Please submit a fix for a maximum of 3 files at a time (1 file is also acceptable).
- As we are not running `mypy` in our CI tests, the user who is submitting a pull request should run it on their local machine and ensure there are no errors in their submission.
- Please ensure your pull request title contains the word `mypy` in it. If possible use this template for your pull request title:
```
[mypy] Fix type annotations for <filenames>
```

### Which errors to fix?

Please follow the below steps to produce all the errors in this library:
- Fork this repository if you haven't already.
- Clone the forked repository on your local machine using the command:

```
git clone --depth 1 https://github.com/TheAlgorithms/Python.git
```
Then you need to install all the necessary requirements:
```
cd python/
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install mypy
```
Then run either of the two commands:
- `mypy --ignore-missing-imports .` -> To produce all the error messages for the entire codebase.
- `mypy --ignore-missing-imports <filepath1> <filepath2> ...` -> To produce error messages for the mentioned file.

### How to fix the errors?

- Make a separate branch for your fix with the command: 
```
git checkout -b mypy-fix
```
- Make changes to the selected files.
- Push it to your forked copy and open a pull request with the appropriate title as mentioned above.

### Focus on one directory at a time:

```
.
├── [x] arithmetic_analysis
├── [x] backtracking
├── [x] bit_manipulation
├── [x] blockchain
├── [x] boolean_algebra
├── [x] cellular_automata
├── [x] ciphers
├── [x] compression
├── [x] computer_vision
├── [x] conversions
├── [ ] data_structures
├── [x] digital_image_processing
├── [x] divide_and_conquer
├── [ ] dynamic_programming
├── [x] electronics
├── [x] file_transfer
├── [x] fractals
├── [x] fuzzy_logic
├── [x] genetic_algorithm
├── [x] geodesy
├── [x] graphics
├── [ ] graphs
├── [x] hashes
├── [x] knapsack
├── [x] linear_algebra
├── [x] machine_learning
├── [ ] maths
├── [ ] matrix
├── [x] networking_flow
├── [x] neural_network
├── [ ] other
├── [ ] project_euler
├── [x] quantum
├── [x] scheduling
├── [x] scripts
├── [ ] searches
├── [x] sorts
├── [ ] strings
└── [x] web_programming
```

### Pre-requisites:
- You should be familiar with `mypy`: https://mypy.readthedocs.io
- You should be familiar with Python type hints: https://docs.python.org/3/library/typing.html


Discussion:
@dhruvmanila I am beginner programmer in python. Is there any help I can do regarding this? 
wrking

# Update:

With the latest update for `mypy` http://mypy-lang.org/, it supports [PEP 585](https://www.python.org/dev/peps/pep-0585/) which lets us use `list[int]` instead of `List[int]`. We always run on the latest Python version so we will adopt the built in generic types instead of importing it from the `typing` module.
I updated the list above to reflect the directories that pass in our build GitHub Action.  As contributors fix directories, please add them to the mypy tests in `.github/workflows/build.yml` so that they get tested and we do not have regressions.
So, here's the crux of the issue: mypy does not try type-checking every single module you've imported. Instead, it only attempts to type-check modules that have explicitly opted-in to the typing ecosystem.

Modules can opt-in to the typing ecosystem via two key mechanisms:

Add type hints or stubs to their code, and include a file named py.typed within the package they distribute to PyPi (or any other package repository). The presence of this marker makes the package PEP-561-aware. The mypy docs also have more info about PEP-561-aware packages.
Alternatively, add stubs to typeshed, the repository of type hints for the standard library and select 3rd party libraries.
The aws_xray_sdk package has done neither of these things, so will be ignored by mypy.
@ayushigoyal2840 The next step is to [remove the `exclude` statement](https://github.com/TheAlgorithms/Python/blob/master/mypy.ini#L5) while keeping the tests green.  Once that is done, we can look into the missing imports.  Once that is done, we can try adding `--strict`
Great work team
@ikobangs There is [still work to do](https://github.com/TheAlgorithms/Python/blob/master/mypy.ini#L5) so please consider attacking one of the following directories:
* [ ] data_structures #4362 
* [ ]  dynamic_programming #4332
* [ ]  graphs
* [ ]  maths #4362 #4358
* [ ]  matrix
* [ ]  other
* [ ]  project_euler
* [ ]  searches #4323 #4352
* [ ]  strings
Where to start
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I'm a beginner in Python, is there any help I can do?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Please reopen this issue once you add more information and updates here. If this is not the case and you need some help, feel free to seek help from our [Gitter](https://gitter.im/TheAlgorithms) or ping one of the reviewers. Thank you for your contributions!

Hello... Can I help with any of the issues here? ..i want to learn the ways ...so i also might need some guidance
Hello .. Can I help with any issues here? Like if someone assigns me some work I would be grateful to work on that 
 No one assigns work.  We just review pull requests so please go ahead.
Now how to solve it 
@AminVerve14 Read all the instructions given in the guidelines of this issue, you will get to know
I have done fixes for the entire proj euler repo

its amazing project

Resolving patch:
diff --git a/graphs/boruvka.py b/graphs/boruvka.py
--- a/graphs/boruvka.py
+++ b/graphs/boruvka.py
@@ -24,6 +24,7 @@
 
     Details: https://en.wikipedia.org/wiki/Bor%C5%AFvka%27s_algorithm
 """
+from __future__ import annotations
 
 
 class Graph:
@@ -39,8 +40,8 @@ def __init__(self, num_of_nodes: int) -> None:
         """
 
         self.m_num_of_nodes = num_of_nodes
-        self.m_edges = []
-        self.m_component = {}
+        self.m_edges: list[list[int]] = []
+        self.m_component: dict[int, int] = {}
 
     def add_edge(self, u_node: int, v_node: int, weight: int) -> None:
         """Adds an edge in the format [first, second, edge weight] to graph."""
@@ -83,7 +84,7 @@ def boruvka(self) -> None:
         component_size = []
         mst_weight = 0
 
-        minimum_weight_edge = [-1] * self.m_num_of_nodes
+        minimum_weight_edge: list[int] = [-1] * self.m_num_of_nodes
 
         # A list of components (initialized to all of the nodes)
         for node in range(self.m_num_of_nodes):


Test patch: