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

Run linters and formatters via pre-commit #797

Closed
wants to merge 3 commits into from
Closed

Run linters and formatters via pre-commit #797

wants to merge 3 commits into from

Conversation

JoshKarpel
Copy link
Contributor

@JoshKarpel JoshKarpel commented Apr 9, 2022

Hi again!

While working on #794 I had a few flake8 linting errors locally that I didn't catch before pushing and had to push again to fix. On other projects I've found pre-commit very helpful for preventing these errors by forcing linters and formatters to run before committing. This PR adds a pre-commit configuration which includes the existing flake8 and isort checks, plus some basic formatters and linters suggested by pre-commit. I've updated CONTRIBUTING.md to include instructions for setting up pre-commit locally and installing the hooks.

In this initial take on the PR I've left flake8 and isort running through tox as well, but there's a CI service associated with pre-commit at https://pre-commit.ci/ that I like (free for open source projects). It runs pre-commit checks on PRs and sends a PR every week to update the pre-commit check dependencies. It's also possible to run the pre-commit checks in CircleCI, example config here. I'd recommend removing the tox configuration for running flake8 and isort and running them through pre-commit instead, either through pre-commit.ci or CircleCI - your preference, assuming you're interested in this :)

@JoshKarpel JoshKarpel marked this pull request as draft April 9, 2022 21:48
@JoshKarpel JoshKarpel marked this pull request as ready for review April 9, 2022 21:58
Comment on lines 292 to 293
families = text_string_to_metric_families("# HELP a \n"
"# UNIT a \n"
Copy link
Contributor Author

@JoshKarpel JoshKarpel Apr 9, 2022

Choose a reason for hiding this comment

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

Seems like the parser requires the trailing space on these lines, which the end-of-line fixer I added in the pre-commit config wanted to remove from the multiline string. Possibly a separate bug in the parser? I don't know enough about the openmetrics format to say.

Copy link
Contributor

Choose a reason for hiding this comment

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

The parser is correct, also changing things like this makes it harder to manually run these tests via copy&page. They're written this way for a reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good to know - lemme revert this part and remove the end-of-line fixer as a quick fix. There are ways to ignore particular files, but I don't love doing that, so it seems like that particular formatter needs some more thought.

Comment on lines 292 to 293
families = text_string_to_metric_families("# HELP a \n"
"# UNIT a \n"
Copy link
Contributor

Choose a reason for hiding this comment

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

The parser is correct, also changing things like this makes it harder to manually run these tests via copy&page. They're written this way for a reason.

@@ -272,7 +272,7 @@ def test_timestamps(self):
# TYPE b counter
# HELP b help
b 2 1234567890
b 88 1234566000
Copy link
Contributor

Choose a reason for hiding this comment

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

You are changing this test, this trailing space is meant to be here.

Copy link
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Personally, I am not a huge fan of pre-commit hooks as they add more overhead than I would like for some changes, or when I just want to push my progress somewhere. Yes --no-verify is an option, but I have still found them a bit jarring.

I would like to leave this open for a bit though, if other devs think it is helpful I am not opposed to merging it.

@JoshKarpel
Copy link
Contributor Author

Personally, I am not a huge fan of pre-commit hooks as they add more overhead than I would like for some changes, or when I just want to push my progress somewhere. Yes --no-verify is an option, but I have still found them a bit jarring.

Fair point! However, this set of checks is pretty fast.

At least on my computer, running these pre-commit checks on all files (which is the absolute maximum, since during a commit they'll only run on files in the diff) takes ~4 seconds

❯ time pre-commit run --all
check for added large files..............................................Passed
check python ast.........................................................Passed
check builtin type constructor use.......................................Passed
check for case conflicts.................................................Passed
check docstring is first.................................................Passed
check for merge conflicts................................................Passed
check toml...........................................(no files to check)Skipped
check yaml...............................................................Passed
debug statements (python)................................................Passed
fix end of files.........................................................Passed
flake8...................................................................Passed
isort....................................................................Passed

real    0m4.080s
user    0m8.024s
sys     0m1.443s

I'm running through WSL so I suspect this is longer than average due to how slow WSL is with filesystem operations. In my experience, those few seconds are worth it to not have to wait for CI to fail and do a whole extra commit + push to fix the typos.

@JoshKarpel JoshKarpel closed this by deleting the head repository Jun 14, 2023
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