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

Add mypy #1639

Merged
merged 11 commits into from
Jul 28, 2022
Merged

Add mypy #1639

merged 11 commits into from
Jul 28, 2022

Conversation

dongkyunc
Copy link
Contributor

@dongkyunc dongkyunc commented Jul 18, 2022

What

Adds mypy and django-stubs

Why

Static type checking is nice

How

  • Add package to `requirements.{in,txt}
  • Add settings to `web/main/config.
  • Add .github/workflows/mypy.yml
  • Change some lines that were making mypy mad

Notes to Reviewers

  • web/main/admin.py is ignored, along with any imports that may affect
  • Originally, check_untyped_defs was passed in as an option, there were errors I did not know how to fix due to an unfamiliarity with the codebase
  • typing.Sequence is unused, but it's making mypy mad if it isn't there for some reason.
    image

@dongkyunc dongkyunc requested a review from a team as a code owner July 18, 2022 17:04
@dongkyunc dongkyunc requested review from sabzo and removed request for a team July 18, 2022 17:04
@codecov-commenter
Copy link

codecov-commenter commented Jul 18, 2022

Codecov Report

Merging #1639 (e4c3382) into develop (c489b8a) will increase coverage by 0.01%.
The diff coverage is 93.93%.

@@             Coverage Diff             @@
##           develop    #1639      +/-   ##
===========================================
+ Coverage    75.77%   75.78%   +0.01%     
===========================================
  Files           43       43              
  Lines         5986     5997      +11     
===========================================
+ Hits          4536     4545       +9     
- Misses        1450     1452       +2     
Impacted Files Coverage Δ
web/main/admin.py 70.07% <ø> (ø)
web/main/models.py 74.63% <92.59%> (+0.03%) ⬆️
web/config/urls.py 83.33% <100.00%> (ø)
web/config/wsgi.py 80.00% <100.00%> (ø)
web/reporting/admin/__init__.py 100.00% <100.00%> (ø)
web/reporting/admin/views.py 98.08% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c489b8a...e4c3382. Read the comment docs.

@lizadaly
Copy link
Contributor

Thanks for taking this on!

When I looked at this in #1615 it seemed valuable to install the type stubs for some of the imported packages too.

I recommend deferring the mypy Github Action for now. We probably want to combine all three of pyflakes, black, and mypy into a single workflow to avoid too much overhead in spinning up containers. (There's a Github Action quota for private repositories but that wouldn't seem to apply here.)

@dongkyunc dongkyunc force-pushed the matcha/add-mypy branch 5 times, most recently from 274071d to 2747628 Compare July 20, 2022 18:07
Copy link
Contributor

@rebeccacremona rebeccacremona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hooray for adding support for type annotation!

I have a bunch of little questions.

web/config/wsgi.py Outdated Show resolved Hide resolved
web/main/models.py Show resolved Hide resolved
web/main/models.py Show resolved Hide resolved
web/main/models.py Show resolved Hide resolved
web/main/models.py Outdated Show resolved Hide resolved
web/main/models.py Show resolved Hide resolved
@rebeccacremona
Copy link
Contributor

Cool, thanks for doing this, and sorry to be so slow with approving the changes!!

I'm going to take the liberty of pushing a few commits.

Stubs

I pulled this code down and ran docker compose exec web mypy . and got the following errors:

rcremona@HLSNHJGWY2X2J h2o % docker compose exec web mypy .
main/sanitize.py:1: error: Library stubs not installed for "bleach" (or incompatible with Python 3.9)
main/sanitize.py:1: note: Hint: "python3 -m pip install types-bleach"
main/utils.py:15: error: Library stubs not installed for "requests" (or incompatible with Python 3.9)
main/models.py:8: error: Library stubs not installed for "dateutil.parser" (or incompatible with Python 3.9)
main/models.py:8: note: Hint: "python3 -m pip install types-python-dateutil"
main/models.py:8: note: (or run "mypy --install-types" to install all missing stub packages)
main/models.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main/models.py:8: error: Library stubs not installed for "dateutil" (or incompatible with Python 3.9)
main/models.py:13: error: Library stubs not installed for "requests" (or incompatible with Python 3.9)
main/models.py:13: note: Hint: "python3 -m pip install types-requests"
reporting/admin/views.py:5: error: Library stubs not installed for "dateutil.relativedelta" (or incompatible with Python 3.9)
reporting/admin/usage_dashboard.py:10: error: Library stubs not installed for "dateutil.relativedelta" (or incompatible with Python 3.9)
Found 7 errors in 5 files (checked 88 source files)

I bet you ran mypy --install-types in your environment? I'm going to add all those stubs to requirements.[in, txt] so that they will be installed in the Docker image. Then, subsequent to pip-compile and pip-sync:

docker compose exec web mypy .
Success: no issues found in 88 source files

Regarding typing.Sequence

This turns out to be a known bug with the handling of custom model managers. As per the docs, QuerySet.as_manager() is not yet supported. But the workaround they suggest, Manager.from_queryset, as of the latest release, runs into django-stubs#1022... which is fixed in the merged, but as-yet-unreleased django-stubs#1028.

I installed from the merged commit:

docker compose exec web bash -c "yes | pip uninstall django-stubs"
docker compose exec web pip install git+https://github.com/typeddjango/django-stubs.git@84eff755661c3c498c36e66b7ff0b6cc8fc3d4bb

And behold:

docker compose exec web mypy main/models.py
Success: no issues found in 1 source file

Version 1.12.0 was released on June 17, 2022; hopefully that means we won't have to wait long for the next release!

I added a comment to that effect.

@rebeccacremona
Copy link
Contributor

Now let's see if I'm smart enough to resolve the merge conflict using the Github UI 😅

@lizadaly
Copy link
Contributor

@rebeccacremona I can do this in a bit if you want since I'm more familiar with the code.

@rebeccacremona rebeccacremona merged commit 297558f into harvard-lil:develop Jul 28, 2022
@rebeccacremona
Copy link
Contributor

@rebeccacremona I can do this in a bit if you want since I'm more familiar with the code.

whoops, thanks, didn't see this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants