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

lint: add pre-commit config #582

Merged
merged 3 commits into from
May 24, 2023

Conversation

lukpueh
Copy link
Member

@lukpueh lukpueh commented May 22, 2023

Add basic pre-commit config file, with hooks for black and isort, plus two convenient builtin checkers.

Related changes in this PR

  • Moves config for black and isort to pyproject.toml, to be re-used by tox and pre-commit
  • Adds pre-commit to dev requirements
  • Updates .gitignore to allow pre-commit config file.

Caveat
hooks in config file must be manually initialized and updated:

pre-commit install # once
pre-commit autoupdate # regularly

lukpueh added 2 commits May 22, 2023 11:06
This allows re-using the same config in tox and other tools, e.g.
pre-commit.

Signed-off-by: Lukas Puehringer <[email protected]>
Add basic pre-commit config file, with hooks for black and isort, plus
two convenient builtin checkers.

Also adds pre-commit to dev requirements and removes the config file
name from .gitignore

Caveat:
hooks in config file must be manually initialzed and updated, e.g. with
the following commands:
```
pre-commit install
pre-commit autoupdate
```

Signed-off-by: Lukas Puehringer <[email protected]>
@@ -2,6 +2,7 @@
# requirements for local testing with tox, and also for the running test suite
# or individual tests manually
tox
pre-commit
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 have no strong feelings about including this here or not. Given that devs must enable hooks manually, we could also ask them to install pre-commit themselves.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Adding just pre-commit is not enough I assume. You would also need to add black and isort, right? (or maybe I don't understand what pre-commit handles).

In any case i'd lean on not adding it here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Adding just pre-commit is not enough I assume. You would also need to add black and isort, right?

No, pre-commit is a package manager and will install black and isort pre-commit plugins from GitHub, where they are managed by the upstream authors themselves via .pre-commit-hooks.yaml.

In any case i'd lean on not adding it here.

Works for me. I'll remove it.

@lukpueh
Copy link
Member Author

lukpueh commented May 22, 2023

FYI: In python-tuf we decided to not include the pre-commit config file, because of the caveat mentioned above: theupdateframework/python-tuf#1314 (review)

In hindsight, I think, the convenience of including the file outweighs the burden of additional maintenance.

Copy link
Collaborator

@jku jku left a comment

Choose a reason for hiding this comment

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

I don't object if this makes things easier for you...

Out of interest, what's the advantage of this infrastructure over a git commit hook that runs isort && black?

Comment on lines +7 to +24
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
args: ["."]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: ["."]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not forced to worry about this so it's not a big deal... but:

  • is it a good idea to have two slightly different sets of linting passes (tox -e lint and this pre-commit list)?
  • do we understand the dependency chains here? I assume black and isort hooks depend on already installed tools (or does pre-commit install magically install them from somewhere?)... but what about the "builtin" hooks? Do they use code embedded in the pre-commit package?
  • are we in trouble when we inevitably forget to update these versions? I suppose not?

Copy link
Member Author

Choose a reason for hiding this comment

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

is it a good idea to have two slightly different sets of linting passes (tox -e lint and this pre-commit list)?

I don't see a big problem there. Config is the same for both. Differences would mainly come from different versions of black && isort. But tox -e lint on CI is the ultimate authority.

do we understand the dependency chains here? I assume black and isort hooks depend on already installed tools (or does pre-commit install magically install them from somewhere?)... but what about the "builtin" hooks? Do they use code embedded in the pre-commit package?

I admit, I don't exactly know where pre-commit installs the dependencies, but I suppose in some virtual environment. It claims that "python hooks work without any system-level dependencies".

are we in trouble when we inevitably forget to update these versions? I suppose not?

I don't think, we are. But yeah, it would be nice to automate updates. There are some 3rd-party solutions to do this, and a feature request for Dependabot.

@@ -2,6 +2,7 @@
# requirements for local testing with tox, and also for the running test suite
# or individual tests manually
tox
pre-commit
Copy link
Collaborator

Choose a reason for hiding this comment

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

Adding just pre-commit is not enough I assume. You would also need to add black and isort, right? (or maybe I don't understand what pre-commit handles).

In any case i'd lean on not adding it here.

@lukpueh
Copy link
Member Author

lukpueh commented May 24, 2023

Out of interest, what's the advantage of this infrastructure over a git commit hook that runs isort && black?

Good question. I haven't used isort && black with vanilla git hooks. Main reason for me was the easy setup and use. Plus it seemed popular enough, especially the enabled plugins, to be relatively trustworthy.

pre-commit is optional for devs, we should not make everyone install it.

Signed-off-by: Lukas Puehringer <[email protected]>
@lukpueh lukpueh merged commit 135567f into secure-systems-lab:main May 24, 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.

2 participants