Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypy] Need help to fix all mypy errors in the codebase #4052

Open
dhruvmanila opened this issue Dec 23, 2020 · 27 comments
Open

[mypy] Need help to fix all mypy errors in the codebase #4052

dhruvmanila opened this issue Dec 23, 2020 · 27 comments

Comments

@dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Dec 23, 2020

UPDATE: Our GitHub Actions now run mypy --ignore-missing-imports excluding those directories that fail that test.

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:

$ 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 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:

@ManuKashyap01
Copy link

@ManuKashyap01 ManuKashyap01 commented Jan 5, 2021

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

@sky3760000
Copy link

@sky3760000 sky3760000 commented Jan 24, 2021

wrking

@dhruvmanila
Copy link
Member Author

@dhruvmanila dhruvmanila commented Feb 23, 2021

Update:

With the latest update for mypy http://mypy-lang.org/, it supports PEP 585 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.

@cclauss
Copy link
Member

@cclauss cclauss commented Mar 19, 2021

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.

@ErwinJunge
Copy link
Contributor

@ErwinJunge ErwinJunge commented Oct 20, 2021

Whle happily plugging away fixing mypy issues, I inadvertently triggered the bot by opening too many MRs 😄 See #5492. I understand the need for the bot, but I was really just following the stated protocol of opening 1 MR per file.

Shall I work by directory instead @dhruvmanila ? It'd save me considerable overhead in creating potentially many MRs, but would lead to probably considerably bigger MRs. I see that @pikulet ran into the same problem fixing the proj euler files and then switched to 1 MR for all the proj euler files.

@cclauss
Copy link
Member

@cclauss cclauss commented Oct 20, 2021

I would be willing to live with mypy fixes on a directory-by-directory basis.

I doubt that we need to go all the way to —strict. Having type hints for all function parameters and return types is super helpful but we do not want mypy wound up so tight that beginning developers cannot contribute.

poyea pushed a commit that referenced this issue Oct 21, 2021
* Update annotations to Python 3.10 #4052

* Add floats doctest

* Copy list to avoid changing input unpredictably

* Refactor code to make it readable

* updating DIRECTORY.md

* Improve raised ValueErrors and add doctest

* Split doctest in multiples lines

* Change ValueError to Monogons and Digons are not poly

* Correct doctest refering number of sides

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
shermanhui added a commit to shermanhui/Python that referenced this issue Oct 22, 2021
shermanhui added a commit to shermanhui/Python that referenced this issue Oct 22, 2021
* fix: type annotations for pypi 🏷️

Fixes TheAlgorithms#4052

* updating DIRECTORY.md

* apply suggestions from code review

Co-authored-by: Christian Clauss <cclauss@me.com>

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
shermanhui added a commit to shermanhui/Python that referenced this issue Oct 22, 2021
* Update annotations to Python 3.10 TheAlgorithms#4052

* Add floats doctest

* Copy list to avoid changing input unpredictably

* Refactor code to make it readable

* updating DIRECTORY.md

* Improve raised ValueErrors and add doctest

* Split doctest in multiples lines

* Change ValueError to Monogons and Digons are not poly

* Correct doctest refering number of sides

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
poyea added a commit that referenced this issue Oct 22, 2021
* [mypy] fix type annotations for graphs/a_star.py #4052

* updating DIRECTORY.md

* Add from __future__ import anotations

* rename delta by DIRECTIONS

Co-authored-by: John Law <johnlaw.po@gmail.com>

* Rename delta by DIRECTIONS in all code

* Enclose script in __main__ code block

* Refactor DIRECTIONS with comments for readibility

* Delete heuristic example comment

* Do not print, return all values

* Fix multilines

* fix black

* Update a_star.py

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: John Law <johnlaw.po@gmail.com>
@ErwinJunge
Copy link
Contributor

@ErwinJunge ErwinJunge commented Oct 23, 2021

Going directory by directory now: 3 additional directories done. Back into timeout, because the next PR will make the bot angry ;)

I'm personally aiming for --strict compliance with my contributions (to set a good example), but I agree with @cclaus that we probably don't want to enforce that level due to the chance of scaring off beginning developers. I do think we should (eventually) enforce --disallow-untyped-defs --check-untyped-defs --disallow-untyped-decorators --strict-equality because the first 3 in that list are the useful ones from a documentation/editor integration point of view and the last one often triggers on bugs.

spazm added a commit to spazm/Python that referenced this issue Oct 25, 2021
+ Prefer tuple to list for point x,y pairs
spazm added a commit to spazm/Python that referenced this issue Oct 25, 2021
+ Prefer tuple to list for point x,y pairs
spazm added a commit to spazm/Python that referenced this issue Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment