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
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 8 additions & 53 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
---
name: "Lint"
on: # yamllint disable-line rule:truthy
on: # yamllint disable-line rule:truthy
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.

# Uses pre-commit to run all checks
runs-on: "ubuntu-latest"
strategy:
matrix:
python-version:
- "3.10"
fail-fast: false
vroldanbet marked this conversation as resolved.
Show resolved Hide resolved
steps:
- uses: "actions/checkout@v4"
- uses: "bewuethr/yamllint-action@v1.3.0"
with:
config-file: ".yamllint"
- 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.

with:
python-version: "3.10"
- name: "Setup Python Environment"
run: "pip install -U pip virtualenv"
- name: "Install Dependencies"
run: |
virtualenv ~/.cache/virtualenv/authzedpy
source ~/.cache/virtualenv/authzedpy/bin/activate
pip install poetry
poetry env info
poetry install --only dev
- name: "Pyflakes"
run: |
source ~/.cache/virtualenv/authzedpy/bin/activate
find . -name "*.py" | grep -v "_pb2" | xargs pyflakes
- name: "Blacken"
run: |
source ~/.cache/virtualenv/authzedpy/bin/activate
black --check --diff .
- name: "Isort"
run: |
source ~/.cache/virtualenv/authzedpy/bin/activate
find . -name "*.py" | grep -v "_pb2" | xargs isort --check --diff
python-version: "3.11"
cache: 'poetry'
- run: "poetry install"
- uses: "pre-commit/action@v3.0.1"
vroldanbet marked this conversation as resolved.
Show resolved Hide resolved
codeql:
name: "Analyze with CodeQL"
runs-on: "ubuntu-latest"
Expand Down Expand Up @@ -75,24 +51,3 @@ jobs:
uses: "github/codeql-action/upload-sarif@v3"
with:
sarif_file: "trivy-results.sarif"
mypy:
vroldanbet marked this conversation as resolved.
Show resolved Hide resolved
name: "Type Check with Mypy"
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4"
- uses: "actions/setup-python@v5"
with:
python-version: "3.10"
- name: "Setup Python Environment"
run: "pip install -U pip virtualenv"
- name: "Install Dependencies"
run: |
virtualenv ~/.cache/virtualenv/authzedpy
source ~/.cache/virtualenv/authzedpy/bin/activate
pip install poetry
poetry env info
poetry install --only dev
- name: "mypy"
run: |
source ~/.cache/virtualenv/authzedpy/bin/activate
mypy authzed
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
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.

Original file line number Diff line number Diff line change
@@ -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.

# Ruff version.
rev: "v0.6.1"
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

rev: "v1.1.376"
hooks:
- id: "pyright"
args: ["--verbose"]
4 changes: 3 additions & 1 deletion authzed/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
from authzed.api.v1.watch_service_pb2_grpc import WatchServiceStub


class Client(SchemaServiceStub, PermissionsServiceStub, ExperimentalServiceStub, WatchServiceStub):
class Client(
vroldanbet marked this conversation as resolved.
Show resolved Hide resolved
SchemaServiceStub, PermissionsServiceStub, ExperimentalServiceStub, WatchServiceStub
):
"""
v1 Authzed gRPC API client - Auto-detects sync or async depending on if initialized within an event loop
"""
Expand Down
14 changes: 11 additions & 3 deletions authzed/api/v1/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ from authzed.api.v1.schema_service_pb2 import (
WriteSchemaRequest,
WriteSchemaResponse,
)
from authzed.api.v1.schema_service_pb2_grpc import SchemaServiceAsyncStub, SchemaServiceStub
from authzed.api.v1.schema_service_pb2_grpc import (
SchemaServiceAsyncStub,
SchemaServiceStub,
)
from authzed.api.v1.watch_service_pb2 import WatchRequest, WatchResponse
from authzed.api.v1.watch_service_pb2_grpc import WatchServiceAsyncStub, WatchServiceStub
from authzed.api.v1.watch_service_pb2_grpc import (
WatchServiceAsyncStub,
WatchServiceStub,
)

class Client(SchemaServiceStub, PermissionsServiceStub, ExperimentalServiceStub, WatchServiceStub):
class Client(
SchemaServiceStub, PermissionsServiceStub, ExperimentalServiceStub, WatchServiceStub
):
"""The Client is typed as a synchronous client (though in practice it works with both sync and async code).
If you are using the async code, you should switch to the AsyncClient class instead in order to get proper type hints
"""
Expand Down
10 changes: 8 additions & 2 deletions examples/v1/bulk_check_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@
)
)
assert len(resp.pairs) == 2
assert resp.pairs[0].item.permissionship == CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION
assert resp.pairs[1].item.permissionship == CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION
assert (
resp.pairs[0].item.permissionship
== CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION
)
assert (
resp.pairs[1].item.permissionship
== CheckPermissionResponse.PERMISSIONSHIP_HAS_PERMISSION
)
2 changes: 1 addition & 1 deletion examples/v1/bulk_import_export_relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
)
]

import_reps = client.BulkImportRelationships(((req for req in reqs)))
import_reps = client.BulkImportRelationships((req for req in reqs))
assert import_reps.num_loaded == 2

export_resp = client.BulkExportRelationships(
Expand Down
4 changes: 3 additions & 1 deletion examples/v1alpha1/read_schema_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
async def async_new_client():
# Within an async context, the client's methods are all async:
client = Client("grpc.authzed.com:443", bearer_token_credentials("mytoken"))
resp = await client.ReadSchema(ReadSchemaRequest(object_definitions_names=["example/user"]))
resp = await client.ReadSchema(
ReadSchemaRequest(object_definitions_names=["example/user"])
)
print(resp)


Expand Down
Loading
Loading