Skip to content

CI: mypy&pyright in manual pre-commit stage #47075

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

Merged
merged 5 commits into from
May 22, 2022
Merged

CI: mypy&pyright in manual pre-commit stage #47075

merged 5 commits into from
May 22, 2022

Conversation

twoertwein
Copy link
Member

name: mypy
entry: mypy
# note: assumes python env is setup and activated
language: system
Copy link
Member

Choose a reason for hiding this comment

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

would it work to use the mypy hook, and add numpy (and whatever else is needed for types) in additional_dependencies? I tried this a while ago and there some were inconsistencies with how mypy runs on the whole project versus as a hook, but if we were able to get it to pass as a hook, then it'd solve a whole bunch of typing headaches

I've seen a bunch of other projects do this, and it seems pretty reliable

Copy link
Member Author

Choose a reason for hiding this comment

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

I would like that! I think the only downside is that the current pre-commit run is very fast as it doesn't need to install all the dependencies from environment.yml. I'm not familiar with pre-commit, is there a nice way of sharing the exact dependency versions (basically everything in environment.yml)?

I guess to get pyright working, we would need to use its venv argument to point it to pre-commit/mypy's environment (I don't think pre-commit allows installing python dependencies for a node program).

Copy link
Member

Choose a reason for hiding this comment

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

does it need to be everything in environment.yml? I would've thought that only a few packages would be necessary, like numpy and maybe pytest

is there a nice way of sharing the exact dependency versions (basically everything in environment.yml)?

I don't think so, other than writing our own script (like the one that checks flake8 versions match)

Copy link
Member Author

Choose a reason for hiding this comment

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

does it need to be everything in environment.yml?

Probably almost everything: imported libraries might have type annotations (and pyright explicitly checks that it can find all imported modules).

I think I will just see what a mess it will be :) then we can decide on whether it is worth it or not

Copy link
Member

Choose a reason for hiding this comment

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

😄 no worries then, system is fine, as long as the stage is manual (it used to not be system, and then there were a tonne of false-positives, and people would tend to just turn off pre-commit)

Copy link
Member Author

@twoertwein twoertwein May 20, 2022

Choose a reason for hiding this comment

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

Okay, let's try system for this PR! Two questions:

  1. Does github action create a new shell when executing pre-commit: the system hook doesn't find mypy from the activated conda env?
  2. It would be great to have only pyright and mypy in manual. By default all hooks are in all stages. Could change default_stages, then we need to call pre-commit only once for mypy+pyright (might reduce some overhead). Do you think that would be helpful? If so, what value should default_stages have?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is actually not that bad listing all dependencies for mypy, see the latest commit.

The issue is to programmatically find the python env that pre-commit created for mypy so that pyright can use it as well.

While it is possible to move mypy and later move pyright, I would really like to make sure that pyright uses the same env as mypy, as pyright errors when it encounters any missing modules. It is easy to forget a module, which is then resolved as Any (can hide errors).

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought I found an ugly way that should work on linux but it seems that pyright (and probably mypy too?) needs pandas to be compiled for some of its checks, see the error in pre-commit now.

Compiling pandas in the pre-commit is a little bit too much (more dependencies and it takes much longer). Let's go back to the system+manual approach.

Copy link
Member Author

Choose a reason for hiding this comment

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

@MarcoGorelli I'm mostly pattern matching when it comes to github action+pre-commit but I found a working system+manual version :)

@twoertwein twoertwein marked this pull request as ready for review May 21, 2022 18:20
@jreback jreback added CI Continuous Integration Typing type annotations, mypy/pyright type checking labels May 21, 2022
@twoertwein twoertwein requested a review from mroeschke May 21, 2022 19:54
- name: Mypy
uses: pre-commit/[email protected]
with:
extra_args: --hook-stage manual --all-files mypy
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to both run mypy and pyright in this step and have this be a - name: Typing check?

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I know, that is possible if mypy&pyright are in the same stage (that is the case) AND if no other hook is in this stage.

I'm not sure how people use per-commit, could reserve manual for mypy&pyright.

Copy link
Member

Choose a reason for hiding this comment

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

That sounds good to me (for now). We could reserve manual for more expensive code checks like these

Copy link
Member Author

Choose a reason for hiding this comment

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

Somehow two of the hooks are not affected by default_stages, so I explicitly set their stages.

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Looks great! Looks like there's an unrelated failure on master which prevented this from being run. Do you have a run where this executed?

@twoertwein
Copy link
Member Author

Looks great! Looks like there's an unrelated failure on master which prevented this from being run. Do you have a run where this executed?

I had a run, but then I rebased&force pushed :( Happy to wait until the doc-issue is fixed.

@twoertwein
Copy link
Member Author

I rebased but removed #47018 should hopefully work now

@twoertwein
Copy link
Member Author

I rebased but removed #47018 should hopefully work now

Seems like there is another doc issue. I included the commit again. I'll rebase when main is green.

Co-authored-by: Marco Edward Gorelli <[email protected]>
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Looks fine to me, though not entirely sure how a system hook would behave for everybody

Can always try this out and see

@mroeschke mroeschke merged commit d2a7eff into pandas-dev:main May 22, 2022
@mroeschke
Copy link
Member

Sweet thanks @twoertwein! Yeah this should be fine despite the failure on main

@twoertwein twoertwein deleted the precommit branch May 26, 2022 01:58
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
* CI: mypy&pyright in manual pre-commit stage

* move mypy+pyright to pre-commit

* Revert "move mypy+pyright to pre-commit"

* system+manual

* Update .pre-commit-config.yaml

Co-authored-by: Marco Edward Gorelli <[email protected]>

Co-authored-by: Marco Edward Gorelli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI Continuous Integration Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants