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

Pre-commit demo #187

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Pre-commit demo #187

wants to merge 17 commits into from

Conversation

tstirrat15
Copy link
Contributor

Description

This is a POC of moving linting concerns into pre-commit across our various client repositories. Ideally this should centralize linting configuration and make it easier to get linting working on a particular repo.

Changes

Gonna annotate. There's a few of them.

Testing

Review, see that the lint action passes and does what you need it to do. Run pre-commit run -a locally and see that it succeeds. If you get errors about pyright not being able to find definitions, you might have to remove your poetry env and re-install so that it installs the venv locally in a place where pyright can find it.

@tstirrat15 tstirrat15 requested a review from a team as a code owner August 20, 2024 19:10
Copy link
Contributor Author

@tstirrat15 tstirrat15 left a comment

Choose a reason for hiding this comment

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

See comments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the configuration that will differ across repos.

@@ -0,0 +1,21 @@
---
repos:
- repo: "https://github.com/astral-sh/ruff-pre-commit"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I switched from black + pyflakes + isort to ruff, which is the current state-of-the-art for python linting and formatting. It's written in rust, opinionated in mostly sane ways, and is hella fast.

hooks:
# Run the linter.
- id: "ruff"
exclude: ".*_pb2.*"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ignore the generated files

# Run the formatter.
- id: "ruff-format"
exclude: ".*_pb2.*"
- repo: "https://github.com/adrienverge/yamllint"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can run yamllint through here as well

rev: "v1.35.1"
hooks:
- id: "yamllint"
- repo: "https://github.com/RobertCraigie/pyright-python"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also swapped from mypy to pyright - pyright is the one that vscode uses, and it's got some performance and typing benefits over mypy. There's a comparison here: https://github.com/microsoft/pyright/blob/main/docs/mypy-comparison.md

authzed/api/v1/__init__.py Show resolved Hide resolved
@@ -1,3 +1,9 @@
[virtualenvs]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was necessary to get pyright via pre-commit to play nicely with poetry. We store the venv locally (kinda like having a local node_modules) and then pyright can reference it.

Comment on lines +49 to +54
# NOTE: this isn't ideal. The issue here appears to be with the code that's
# generated for google grpc Statuses, and pyright complains that Status isn't
# on the module, which *does* appear to be true. It's unclear whether this is
# an issue with our usage or with the package that generates types.
# TODO: try using pyi to generate the stubs rather than mypy
reportAttributeAccessIssue = "warning"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just calling this out.

.github/workflows/lint.yaml Show resolved Hide resolved
Comment on lines 15 to 17
- name: "Install poetry"
run: "pipx install poetry"
- uses: "actions/setup-python@v5"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes deps available for pyright, and should also do a better job of caching deps between runs.

Copy link
Contributor Author

@tstirrat15 tstirrat15 left a comment

Choose a reason for hiding this comment

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

One more comment

Comment on lines +15 to +21
- name: "Install Poetry"
uses: "snok/install-poetry@v1"
with:
config-file: ".yamllint"
# This should come from pyproject.toml but poetry
# wasn't picking it up for whatever reason.
virtualenvs-in-project: true
virtualenvs-path: ".venv"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not super happy about this, but poetry doesn't otherwise seem to properly respect the pyproject.toml settings for this.

Copy link
Contributor

@vroldanbet vroldanbet left a comment

Choose a reason for hiding this comment

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

I'm having the issues with pyright you mentioned, no matter how many times I removed and recreated my poetry venv. Not sure if it has to do with PyCharm's poetry support. The other linters seem to pass, though.

I missed instructions on how to install pre-commit. I resourced to brew install pre-commit which brought a fair share of system-wide updates. I'd suggest adding a new section in the README that describes how to setup the development environment.

I like the idea of pre-commit hooks to align how linters are run across the projects, although I'd admit I'm not a fan of putting another tool in the critical (development) path (I learned that "git pre-commit hooks" and pre-commit are different things)

I left some questions as well.

.github/workflows/lint.yaml Show resolved Hide resolved
push:
branches:
- "main"
pull_request:
branches: ["*"]
jobs:
lint:
name: "Format & Lint"
Copy link
Contributor

Choose a reason for hiding this comment

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

why removal of the name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No strong reason - it was when i was deleting and replacing chunks of the configuration. I can reinstate it.

.github/workflows/lint.yaml Show resolved Hide resolved
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