Skip to content

Add more pre-commit hooks#339

Merged
MarkKoz merged 3 commits into
masterfrom
feat/deps/o138/pre-commit-hooks
Mar 10, 2020
Merged

Add more pre-commit hooks#339
MarkKoz merged 3 commits into
masterfrom
feat/deps/o138/pre-commit-hooks

Conversation

@MarkKoz
Copy link
Copy Markdown
Contributor

@MarkKoz MarkKoz commented Mar 5, 2020

Relevant Issues

python-discord/organisation#138

Description

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. As a side effect of running in CI, flake8's output will now be shown in stdout in addition to the XML that's published.

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

Reasoning

  • Dev dependencies were updated and re-pinned because why not? They were getting old enough. I'm just following along with the other repos.
  • The pre-commit environment is cached to speed up builds.
  • The reasoning for the use of the hack to mock the pipenv binary is detailed in the commit message.

Additional Details

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.

MarkKoz added 2 commits March 4, 2020 18:52
Hooks added:

* check-merge-conflict - checks for files with 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 - ensures 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 - enforces that noqa annotations always
  occur with specific codes

Changes made to comply with new hooks:

* Remove trailing whitespaces
* Remove some useless noqa annotations
* Specify errors for noqa annotations
* Add missing newlines at end of files

See: python-discord/organisation#138
@MarkKoz MarkKoz added area: dependencies Related to package dependencies and management type: feature New feature or request priority: 2 - normal Normal Priority area: CI Related to continuous intergration and deployment status: needs review labels Mar 5, 2020
@MarkKoz MarkKoz requested a review from a team as a code owner March 5, 2020 03:59
@MarkKoz MarkKoz requested review from eivl and jb3 March 5, 2020 03:59
@MarkKoz MarkKoz force-pushed the feat/deps/o138/pre-commit-hooks branch from 020141f to 242c6d1 Compare March 5, 2020 04:03
This also means flake8 will output to stdout in CI now. It didn't
before because it output to an XML format for publishing.

Pre-commit creates its own environment in which it installs hooks. To
speed up runs, the pipeline will cache this for use with future jobs.
The cache will update if .pre-commit-config.yaml changes.

The flake8 pre-commit hook 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 242c6d1 to 21dcf7a Compare March 5, 2020 05:20
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. There's one diff I don't quite get, the last line of CONTRIBUTING.MD, but that's just because I don't see a difference; I'm probably overlooking something and there's nothing wrong with the changed version.

@scragly
Copy link
Copy Markdown
Contributor

scragly commented Mar 6, 2020

the last line of CONTRIBUTING.MD

It's removed an empty line from the end of file.

@SebastiaanZ
Copy link
Copy Markdown
Contributor

the last line of CONTRIBUTING.MD

It's removed an empty line from the end of file.

That's what I suspected; the GitHub diff just isn't showing that. It shows that difference for other removed lines at the end of files, but for this one it shows up as if the line the diff is referencing has been removed and inserted unchanged.

This:
2020-03-06_12-45

vs:
2020-03-06_12-45_1

Guess it's a GH diff glitch.

@scragly
Copy link
Copy Markdown
Contributor

scragly commented Mar 6, 2020

Yep. I had to download the raw files to confirm it just in case; github wasn't showing the correct diff for it as you said.

Copy link
Copy Markdown
Contributor

@lemonsaurus lemonsaurus left a comment

Choose a reason for hiding this comment

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

Yep, this all looks good. I guess I don't fully understand the newline at end fixer, I thought it would ensure newlines at the end of any kind of file but it seems this is not the case. Can anyone shed some light on this?

Anyway, it looks good enough to merge, and I'm sure the fixer is doing what it should be doing.

Comment thread .gitattributes
# See e.g. https://stackoverflow.com/a/38588882/4464570
*.png binary
*.whl binary

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this a result of running the end-of-file-fixer?

If so, I don't really understand why this file doesn't get a newline at the end of the file. I thought every kind of file was supposed to have one but maybe there's more to it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was an extra newline at the end of the file. It had two final newlines. Now it has one. Some things, like GitHub, git, and neovim, aren't showing the final newline as a separate line, while others, like PyCharm, are.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ahaaa

max-width: none;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another special rule for css files?

I guess there must be files then that should not have newlines to the end of the file.

Copy link
Copy Markdown
Contributor

@eivl eivl 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, I do not really understand the azure-pipelines configuration well enough to comment anything other than what it looks like.

@MarkKoz MarkKoz merged commit 07f6278 into master Mar 10, 2020
@MarkKoz MarkKoz deleted the feat/deps/o138/pre-commit-hooks branch March 10, 2020 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: CI Related to continuous intergration and deployment area: dependencies Related to package dependencies and management priority: 2 - normal Normal Priority type: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants