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 type checking to our CI process #180

Merged
merged 28 commits into from
Mar 2, 2022
Merged

Add MyPy type checking to our CI process #180

merged 28 commits into from
Mar 2, 2022

Conversation

jonathangreen
Copy link
Member

@jonathangreen jonathangreen commented Feb 4, 2022

Description

Add Mypy type checking to our CI process.

Motivation and Context

As we are adding type annotation in our code base, it is nice to use them not just for understanding, but to actually type check our code. This shouldn't add any runtime overhead, only when we are actually running the type checker. This also prevents us from adding misleading annotations by mistake, since the annotations we add will actually be checked.

This PR takes the approach recommended in the mypy docs for existing code bases.

If your codebase is large, pick a subset of your codebase (say, 5,000 to 50,000 lines) and run mypy only on this subset at first, without any annotations. This shouldn’t take more than a day or two to implement, so you start enjoying benefits soon.

The files we are running mypy on are defined in pyproject.toml. This PR runs the type checker on:

  • core
  • tests/core

It added annotations where necessary to get the checker to run, and corrected annotations where they were incorrect.

In some of the code, I converted the older python 2 style doc comments with types to be actual type annotations, and removed the old comments, so they wouldn't get out of sync with the annotations. I used doc484 and com2ann to do the conversion, then did some hand correction of the annotations.

In the core/model/hassessioncache.py I modified the type annotations to better reflect whats going on in the code. Originally the type annotations were using a protocol, but this code is actually better suited to be typed as a generic.

Documentation

I added some documentation on mypy to the notion wiki:
https://www.notion.so/lyrasis/Mypy-d97f188926294d4c89ceff91ce9556bf

It covers some of the gotchas that I encountered and workarounds used when preparing this PR.

How Has This Been Tested?

Testing has been confined to running the type checker and running the unit tests.

@jonathangreen jonathangreen requested a review from a team February 4, 2022 16:47
@io7m
Copy link
Contributor

io7m commented Feb 4, 2022

This brings me joy. 🚀

Copy link
Contributor

@tdilauro tdilauro left a comment

Choose a reason for hiding this comment

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

LGTM! ✔️

@tdilauro tdilauro merged commit 532b7c7 into main Mar 2, 2022
@tdilauro tdilauro deleted the feature/mypy branch March 2, 2022 14:52
@tdilauro tdilauro mentioned this pull request Mar 2, 2022
2 tasks
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.

3 participants