Skip to content

Add more pre-commit hooks and run them in CI#811

Merged
scragly merged 8 commits into
masterfrom
feat/deps/o138/pre-commit-hooks
Mar 4, 2020
Merged

Add more pre-commit hooks and run them in CI#811
scragly merged 8 commits into
masterfrom
feat/deps/o138/pre-commit-hooks

Conversation

@MarkKoz
Copy link
Copy Markdown
Contributor

@MarkKoz MarkKoz commented Mar 2, 2020

As per python-discord/organisation#138, new hooks were added for pre-commit and they will run in CI too. The pipenv run lint script will now run all the new hooks, including flake8.

Hooks added

A couple of these hooks automatically apply fixes. However, they still report failure and leave any changes they make uncommitted. Therefore, the user has to commit the automatic fixes.

  • check-merge-conflict - Check for files that contain merge conflict strings.
  • check-toml - Attempts to load all toml files to verify syntax.
  • check-yaml - Attempts to load all yaml files to verify syntax.
  • end-of-file-fixer - Makes sure files end in a newline and only a newline.
  • mixed-line-ending - Replaces mixed line endings with LF.
  • trailing-whitespace - Trims trailing whitespace.
  • python-check-blanket-noqa - Enforce that noqa annotations always occur with specific codes

CI

The pre-commit venv is cached. There should only be a cache miss if the .pre-commit-config.yaml file changes or if the location of the Python interpreter changes (more realistically, if the Python version changes). Maybe Azure wipes the cache sometimes too, but in that case pre-commit will simply re-create the environment.

ELA edit:
Also closes #775

* Remove trailing whitespaces
* Specify error code for a noqa in the free command
@MarkKoz MarkKoz added t: feature New feature or request a: dependencies Related to package dependencies and management a: CI Related to continuous intergration and deployment p: 2 - normal Normal Priority status: needs review labels Mar 2, 2020
@MarkKoz MarkKoz requested a review from a team as a code owner March 2, 2020 19:57
@MarkKoz MarkKoz requested review from SebastiaanZ and lemonsaurus and removed request for a team March 2, 2020 19:57
@MarkKoz MarkKoz force-pushed the feat/deps/o138/pre-commit-hooks branch from b6773db to e6c4887 Compare March 2, 2020 20:27
@SebastiaanZ
Copy link
Copy Markdown
Contributor

It's worth noting that moving flake8 out of the virtual environment also comes with a disadvantage for local development. While it's still going to be possible to run the linter from the command line using pre-commit, it will break editors that use flake8 to lint within the editor. There may be ways to deal with this, as I haven't looked into whether or not it's possible to configure editors in such a way that they're able to use pre-commit's flake8, but this will be a more complicated and non-standard set-up.

For me, personally, it probably means that I'm going to maintain a separate dependency listing locally to be able to create a pipenv or other Python virtual environment where flake8 and the plugins we use are installed to be able to use a linter in editor.

For reference, this is what you'd get with a default linter set-up in Sublime Text:

SublimeLinter: #2 flake8 test_base.py ERROR:
============================================

/home/sebastiaan/.local/share/virtualenvs/bot-I8fh9oki/bin/python: No module named flake8

@lemonsaurus
Copy link
Copy Markdown
Contributor

I think what @SebastiaanZ is saying here is a dealbreaker for removing these dependencies from the pipfile.

I think it would be better to maintain duplicate dependencies in two files than to require a weird non-standard setup to get linting working on sublime or vscode. These are popular editors, and it should be frictionless to set up linting in our repos.

@MarkKoz
Copy link
Copy Markdown
Contributor Author

MarkKoz commented Mar 3, 2020

OK. What do you all think of using this hack in CI?

printf '%s\n%s' '#!/bin/bash' '"${@:2}"' >> pipenv && chmod +x pipenv

It creates a mock pipenv run executable. It will ignore the first arg (i.e. ignore the run in pipenv run) and interpret the rest as if it was a command. It'll get prepended to PATH somewhere so that when pre-commit runs the flake8 hook, it will prefer it as the pipenv executable over the normal one.

@sco1 sco1 mentioned this pull request Mar 4, 2020
3 tasks
MarkKoz added 3 commits March 3, 2020 19:46
A cache for an outdated pre-commit environment may still be useful. It
may be the case that only some hooks need to be updated rather than
all.
The mock gets used by the flake8 pre-commit hook, which invokes flake8
via `pipenv run flake8`. It's normally useful to use pipenv here cause
it ensures flake8 is invoked within the context of the venv. However, in
CI, there is no venv - dependencies are installed directly to the
system site-packages. `pipenv run` does not work in such case because it
tries to create a new venv if one doesn't exist (it doesn't consider the
system interpreter to be a venv).

This workaround (okay, it's a hack) creates an executable shell script
which replaces the original pipenv binary. The shell script simply
ignores the first argument (i.e. ignores `run` in `pipenv run`) and
executes the rest of the arguments as a command. It essentially makes
`pipenv run flake8` equivalent to just having ran `flake8`. When
pre-commit executes pipenv, the aforementioned script is what will run.
@MarkKoz MarkKoz force-pushed the feat/deps/o138/pre-commit-hooks branch from 33c77a5 to 93f29f8 Compare March 4, 2020 03:47
sco1 added 3 commits March 3, 2020 22:51
This was added by the now-removed Snake cog & is not used elsewhere on bot.
@sco1
Copy link
Copy Markdown
Contributor

sco1 commented Mar 4, 2020

Per #775 I've also added pep8-naming to the linting toolchain and updated the codebase to fix the handful of new linting issues.

Copy link
Copy Markdown
Contributor

@SebastiaanZ SebastiaanZ left a comment

Choose a reason for hiding this comment

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

Looks good and this works well with local linting set-ups.

This PR does make me realize that I badly need to update the tests README.

@scragly scragly merged commit 8942d31 into master Mar 4, 2020
@scragly scragly deleted the feat/deps/o138/pre-commit-hooks branch March 4, 2020 08:37
@lemonsaurus
Copy link
Copy Markdown
Contributor

@MarkKoz I like it. It's hacky and clever and I don't mind at all because you've documented it well and it only happens in the CI. Excellent work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: CI Related to continuous intergration and deployment a: dependencies Related to package dependencies and management p: 2 - normal Normal Priority t: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add pep8-naming to linting toolchain

5 participants