Skip to content

CI: add pre-commit action, include pyupgrade #36471

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 11 commits into from
Sep 22, 2020

Conversation

MarcoGorelli
Copy link
Member

@MarcoGorelli MarcoGorelli commented Sep 19, 2020

xref this comment #36426 (comment) by @TomAugspurger

I would recommend running the linting commands on CI through pre-commit. That way the versions in .pre-commit-config.yaml are used everywhere.

So, this PR does that.

It also adds pyupgrade as a pre-commit hook (xref #36450 ) and removes some unnecessary configurations from the .pre-commit-config.yaml file (all of these hooks already specify language: python, see e.g. https://github.com/psf/black/blob/master/.pre-commit-hooks.yaml )

TODO

update https://pandas.pydata.org/docs/development/contributing.html#code-standards (thanks Ali!)

@@ -3,23 +3,19 @@ repos:
rev: 19.10b0
hooks:
- id: black
language_version: python3
Copy link
Member Author

Choose a reason for hiding this comment

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

- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.3
hooks:
- id: flake8
language: python_venv
Copy link
Member Author

Choose a reason for hiding this comment

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

Same, they already specify this here https://gitlab.com/pycqa/flake8/-/blob/master/.pre-commit-hooks.yaml

and according to pre-commit docs https://pre-commit.com/

The python_venv language is now an alias to python

files: \.pxi\.in$
types:
- file
args: [--append-config=flake8/cython-template.cfg]
- repo: https://github.com/pre-commit/mirrors-isort
Copy link
Member Author

Choose a reason for hiding this comment

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

this is deprecated, see https://github.com/pre-commit/mirrors-isort

This mirror repository is deprecated, use isort directly.

hooks:
- id: isort
language: python_venv
Copy link
Member Author

Choose a reason for hiding this comment

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

@alimcmaster1 alimcmaster1 added the CI Continuous Integration label Sep 19, 2020
@alimcmaster1
Copy link
Member

Might need to rework the code_check docs if we do this: https://pandas.pydata.org/docs/development/contributing.html#code-standards

@@ -48,31 +48,6 @@ fi
### LINTING ###
if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then

echo "black --version"
Copy link
Contributor

Choose a reason for hiding this comment

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

how are these no longer needed?

Copy link
Member Author

@MarcoGorelli MarcoGorelli Sep 19, 2020

Choose a reason for hiding this comment

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

They're run during the pre-commit CI check, see https://github.com/pandas-dev/pandas/pull/36471/checks?check_run_id=1137265216 . Could always duplicate them just in case if you like

Copy link
Contributor

Choose a reason for hiding this comment

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

but do we run the pre-commit checks on CI? i think have a script that is separate from the pre-commit is needed as well (it can duplicate or be called by pre-commit), but key is to have everything in sync.

Copy link
Member

Choose a reason for hiding this comment

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

Two cents: I think keeping this for a time just to ensure it never disagrees with pre-commit makes sense, then can remove later once we're fairly sure they're identical (also makes it easier to revert if misconfigured)

Copy link
Member Author

@MarcoGorelli MarcoGorelli Sep 20, 2020

Choose a reason for hiding this comment

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

but do we run the pre-commit checks on CI?

That's what this PR adds - if you go down to "All checks have passed" and expand, you'll see an extra GitHub Action called "pre-commit". If you expand that one further, it says

Run pre-commit/[email protected]
install pre-commit
Cache Size: ~21 MB (21895398 B)
/bin/tar --use-compress-program zstd -d -xf /home/runner/work/_temp/f09139a0-b8b5-427f-bc76-10b2403b8816/cache.tzst -P -C /home/runner/work/pandas/pandas
/opt/hostedtoolcache/Python/3.8.5/x64/bin/pre-commit run --show-diff-on-failure --color=always --all-files
black....................................................................Passed
flake8...................................................................Passed
flake8-pyx...............................................................Passed
flake8-pxd...............................................................Passed
isort....................................................................Passed
pyupgrade................................................................Passed

Copy link
Member

Choose a reason for hiding this comment

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

Two cents: I think keeping this for a time just to ensure it never disagrees with pre-commit makes sense, then can remove later once we're fairly sure they're identical (also makes it easier to revert if misconfigured)

This seems like a good approach! Also would be good to check that github actions CI fails if any of these pre commit stages doesn’t pass.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure - have added the ci/code checks back in for now then

Also would be good to check that github actions CI fails if any of these pre commit stages doesn’t pass.

From my experience using this in my own repo (https://github.com/nbQA-dev/nbQA) that's what happens

Copy link
Member

Choose a reason for hiding this comment

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

Should we open an issue to remember to deduplicate these checks later?

Copy link
Member

Choose a reason for hiding this comment

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

sure

Copy link
Member

Choose a reason for hiding this comment

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

@TomAugspurger
Copy link
Contributor

Thanks. Happy to have these run in both code_checks and the pre-commit GH Action for a bit to make sure things are working properly.

@simonjayhawkins
Copy link
Member

@MarcoGorelli should pyupgrade be added to environment.yml?

@MarcoGorelli
Copy link
Member Author

@MarcoGorelli should pyupgrade be added to environment.yml?

Not necessary, as pre-commit runs it a separate environment and that's the only place here pyupgrade is being used

https://github.com/pandas-dev/pandas/pull/36471/checks?check_run_id=1143999007 shows:

black....................................................................Passed
flake8...................................................................Passed
flake8-pyx...............................................................Passed
flake8-pxd...............................................................Passed
isort....................................................................Passed
pyupgrade................................................................Passed

@simonjayhawkins
Copy link
Member

pre-commit for the contributor is optional. (https://pandas.pydata.org/pandas-docs/dev/development/contributing.html#pre-commit)

so as a reviewer, what's the process to recommend if a pyupgrade failure occurs. advise the contributor to install pyupgrade or does the output of the failed job tell you what needs to be done?

@MarcoGorelli
Copy link
Member Author

MarcoGorelli commented Sep 21, 2020

pre-commit for the contributor is optional. (https://pandas.pydata.org/pandas-docs/dev/development/contributing.html#pre-commit)

so as a reviewer, what's the process to recommend if a pyupgrade failure occurs. advise the contributor to install pyupgrade or does the output of the failed job tell you what needs to be done?

That's a really good point, and yes - it'll show you the diff, e.g. see here https://github.com/pymc-devs/pymc3/runs/1121338004 for an example from PyMC3, which shows:

nbqa-isort...............................................................Failed
- hook id: nbqa
- files were modified by this hook

Fixing /home/runner/work/pymc3/pymc3/docs/source/notebooks/GLM-logistic.ipynb

pre-commit hook(s) made changes.
If you are seeing this message in CI, reproduce locally with: `pre-commit run --all-files`.
To run `pre-commit` as part of git workflow, use `pre-commit install`.
All changes made by hooks:
diff --git a/docs/source/notebooks/GLM-logistic.ipynb b/docs/source/notebooks/GLM-logistic.ipynb
index 96c9d11..71d53e8 100644
--- a/docs/source/notebooks/GLM-logistic.ipynb
+++ b/docs/source/notebooks/GLM-logistic.ipynb
@@ -29,6 +29,9 @@
     }
    ],
    "source": [
+    "from collections import OrderedDict\n",
+    "from time import time\n",
+    "\n",
     "import matplotlib.pyplot as plt\n",
     "import numpy as np\n",
     "import pandas as pd\n",
@@ -37,10 +40,8 @@
     "import theano as thno\n",
     "import theano.tensor as T\n",
     "\n",
-    "from collections import OrderedDict\n",
     "from scipy import integrate\n",
     "from scipy.optimize import fmin_powell\n",
-    "from time import time\n",
     "\n",
     "print(\"Running on PyMC3 v{}\".format(pm.__version__))"
    ]
##[error]The process '/opt/hostedtoolcache/Python/3.8.5/x64/bin/pre-commit' failed with exit code 1

@dsaxton
Copy link
Member

dsaxton commented Sep 21, 2020

@MarcoGorelli should pyupgrade be added to environment.yml?

Since all the other development tools (e.g., testing, typing and linting) are in environment.yml I think adding it makes sense, especially if it's also in CI (would probably minimize friction later when errors do occur). Thoughts @MarcoGorelli ?

@MarcoGorelli
Copy link
Member Author

Makes sense - have added that an pre-commit (since its the recommended way to run pyupgrade)

@simonjayhawkins
Copy link
Member

Makes sense - have added that an pre-commit (since its the recommended way to run pyupgrade)

AFAIK pre-commit doesn't run out-of-the-box on Windows after using pip install pre-commit

you get bash: pre-commit: command not found in git bash and in anaconda prompt

'pre-commit' is not recognized as an internal or external command,
operable program or batch file.

so I think additional instructions/link to relevant docs could be included to lower the barrier to entry for new contributors.

@MarcoGorelli
Copy link
Member Author

🤔 I have it working on my Windows laptop for work, pretty sure all I did was pip install pre-commit in the anaconda prompt. Is that not your experience? Could you try again with their latest version?

The docs already link to the pre-commit website https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#pre-commit , do you think more should be added (in terms of installation instructions)?

@simonjayhawkins
Copy link
Member

🤔 I have it working on my Windows laptop for work, pretty sure all I did was pip install pre-commit in the anaconda prompt. Is that not your experience? Could you try again with their latest version?

using pip install -U pre-commit updated from 2.6.0 -> 2.7.1 in the environment that I was using and solves 'pre-commit' is not recognized

but creating a clean environment from environment.yml (from this PR)

(test) C:\Users\simon\pandas>conda list pre-commit
# packages in environment at C:\Users\simon\Anaconda3\envs\test:
#
# Name                    Version                   Build  Channel
pre-commit                2.7.1            py38h32f6830_0    conda-forge

(test) C:\Users\simon\pandas>pre-commit
[INFO] Initializing environment for https://github.com/PyCQA/isort.
[INFO] Initializing environment for https://github.com/asottile/pyupgrade.
[INFO] Installing environment for https://github.com/PyCQA/isort.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('C:\\Users\\simon\\Anaconda3\\envs\\test\\python.exe', '-mvirtualenv', 'C:\\Users\\simon\\.cache\\pre-commit\\repo6988w0ti\\py_env-default', '-p', 'C:\\Users\\simon\\Anaconda3\\envs\\test\\python.exe')
return code: 1
expected return code: 0
stdout:
    ModuleNotFoundError: No module named 'virtualenv.seed.via_app_data'

stderr: (none)
Check the log at C:\Users\simon\.cache\pre-commit\pre-commit.log

moving pre-commit to the pip section and creating a new environment from that seems to work out-the-box.

(test2) C:\Users\simon\pandas>conda list pre-commit
# packages in environment at C:\Users\simon\Anaconda3\envs\test2:
#
# Name                    Version                   Build  Channel
pre-commit                2.7.1                    pypi_0    pypi

(test2) C:\Users\simon\pandas>pre-commit
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\simon\.cache\pre-commit\patch1600770739.
[INFO] Installing environment for https://github.com/PyCQA/isort.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/asottile/pyupgrade.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
black................................................(no files to check)Skipped
flake8...............................................(no files to check)Skipped
flake8-pyx...........................................(no files to check)Skipped
flake8-pxd...........................................(no files to check)Skipped
isort................................................(no files to check)Skipped
pyupgrade............................................(no files to check)Skipped
[INFO] Restored changes from C:\Users\simon\.cache\pre-commit\patch1600770739.

of course, this comparison is not very well controlled without caches etc being cleared and the first environment installing pre-commit using conda now works as well.

(test) C:\Users\simon\pandas>pre-commit
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\simon\.cache\pre-commit\patch1600771578.
black................................................(no files to check)Skipped
flake8...............................................(no files to check)Skipped
flake8-pyx...........................................(no files to check)Skipped
flake8-pxd...........................................(no files to check)Skipped
isort................................................(no files to check)Skipped
pyupgrade............................................(no files to check)Skipped
[INFO] Restored changes from C:\Users\simon\.cache\pre-commit\patch1600771578.

and I'm can't remember what I did in the past to get pre-commit to work in conda environments that may have affected my settings/configuration.

so maybe just leave as is and see if any other contributors report issues.

@MarcoGorelli
Copy link
Member Author

Cool, thanks for the detailed report

@jreback jreback added this to the 1.2 milestone Sep 22, 2020
@jreback
Copy link
Contributor

jreback commented Sep 22, 2020

thanks @MarcoGorelli lgtm. @TomAugspurger @simonjayhawkins if ok pls merge.

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

lgtm.

@TomAugspurger TomAugspurger merged commit e1d0b67 into pandas-dev:master Sep 22, 2020
@TomAugspurger
Copy link
Contributor

Thanks!

@MarcoGorelli MarcoGorelli deleted the pyupgrade-pre-commit branch September 22, 2020 16:17
kesmit13 pushed a commit to kesmit13/pandas that referenced this pull request Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI Continuous Integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants