-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Ruff formatter and Pre commit #1602
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
base: master
Are you sure you want to change the base?
Changes from all commits
1cd5049
fdf26db
49ea1fb
85104ce
707b645
937a966
b14a8bc
352b134
0e8af53
a511b82
cd57b0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,4 +27,4 @@ docker-compose* | |
| target | ||
| data/v2/cries | ||
| data/v2/csv | ||
| data/v2/sprites | ||
| data/v2/sprites | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
|
|
||
| blank_issues_enabled: false | ||
| contact_links: | ||
| - name: Question | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| repos: | ||
|
|
||
| - repo: https://github.com/pre-commit/pre-commit-hooks | ||
| rev: v4.3.0 | ||
| hooks: | ||
| - id: check-ast | ||
| - id: check-json | ||
| - id: check-toml | ||
| - id: check-yaml | ||
| args: | ||
| - --allow-multiple-documents | ||
| - id: end-of-file-fixer | ||
| - id: fix-byte-order-marker | ||
| - id: pretty-format-json | ||
| args: | ||
| - --no-sort-keys | ||
| - --no-ensure-ascii | ||
| - id: sort-simple-yaml | ||
| - id: trailing-whitespace | ||
| args: | ||
| - --markdown-linebreak-ext=md | ||
|
|
||
| # TODO: Enable in the future when adding the ruff linter | ||
| # - repo: https://github.com/pre-commit/pygrep-hooks | ||
| # rev: v1.9.0 | ||
| # hooks: | ||
| # - id: python-check-blanket-noqa | ||
| # - id: python-check-blanket-type-ignore | ||
| # - id: python-use-type-annotations | ||
|
|
||
| - repo: local | ||
| hooks: | ||
| - id: check-csv | ||
| name: Check CSV | ||
| entry: uv run ./Resources/scripts/check_csv.py | ||
| language: system | ||
| args: [--encoding, utf-8] | ||
| files: \.csv$ | ||
|
|
||
| - id: ruff-format | ||
| name: Ruff Format | ||
| entry: uv run ruff format . | ||
| language: system | ||
| types_or: [python, pyi] | ||
| pass_filenames: false | ||
|
|
||
| - id: build-and-test | ||
| name: Build and Test | ||
| entry: >- | ||
| python -c " | ||
| import subprocess, sys; | ||
| run = lambda cmd, inp=None: subprocess.run(cmd, input=inp, text=True).returncode == 0 or sys.exit(1); | ||
| run(['uv', 'run', 'manage.py', 'migrate', '--settings=config.local']); | ||
| run(['uv', 'run', 'manage.py', 'shell', '--settings=config.local'], inp='from data.v2.build import build_all; build_all()\n'); | ||
| run(['uv', 'run', 'manage.py', 'test', '--settings=config.local']) | ||
| " | ||
| language: system | ||
| pass_filenames: false | ||
| always_run: true | ||
| stages: [manual] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,12 @@ install: check-uv # Install requirements for local development | |
| install-base: check-uv # Install minimal requirements for runtime/pipeline environments | ||
| uv sync --locked --no-dev | ||
|
|
||
| pre-commit-install: check-uv # Install pre-commit hooks | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of this target? isn't
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that dosent install pre-commit the dependency but pre-commit the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, got it! Ok, so, I don't want the hook on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but arent both make install and make pre-commit-install optional like you decide whether to run it or not, although I get your point once the hooks are installed you need to run
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think forcing pre-commit on everyone is a difficult sell. But if we can have an optional path of using pre-commit with git, that is good with me :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes we've already implemented do require As far as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I can also specify explicitly in the readme/contributing md files that installing the hooks into git hooks is optional and add an emphasis that you can run it manually too so new contributors don't get confused if they aren't too familiar with pre-commit
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, looking at the .pre-commit-config.yaml file, having it run automatically with every commit might be intensive! There's a check that builds and tests the entire database, which can be very lengthy. Having that run every time you commit may be too much. Maybe leave this off as a target and if someone wants it they can run
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/PokeAPI/pokeapi/pull/1602/changes#diff-63a9c44a44acf85fea213a857769990937107cf072831e1a26808cfde9d096b9R60 the build and test is marked as manual so it doesn't run every commit rest of the stuff is just formatting checks where all of them together take like 3-5secs to run a manual hook you need to explicitly invoke via a pre-commit hook id as such
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ok, I was unfamiliar with the manual key and didn't process that. With that in mind disregard my last comment - I think the pre-commit checks are great and having the install and run be separate targets is a good way to go. Thank you for clarifying! |
||
| uv run pre-commit install | ||
|
|
||
| pre-commit: check-uv # Run pre-commit hooks | ||
| uv run pre-commit run --all-files | ||
|
|
||
| setup: check-uv # Set up the project database | ||
| uv run manage.py migrate ${local_config} | ||
|
|
||
|
|
@@ -89,10 +95,10 @@ docker-prod: | |
| docker-setup: docker-up docker-migrate docker-build-db # (Docker) Start services, prepare the latest DB schema, populate the DB | ||
|
|
||
| format: check-uv # Format the source code | ||
| uv run black . --extend-exclude '.+/scripts/.+' | ||
| uv run ruff format . | ||
|
|
||
| format-check: check-uv # Check the source code has been formatted | ||
| uv run black . --check --extend-exclude '.+/scripts/.+' | ||
| uv run ruff format . --check | ||
|
|
||
| pull: | ||
| git checkout master | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| ADMINS=PokeAPI,change.me@pokeapi.co | ||
| BASE_URL=http://localhost/ | ||
| POKEAPI_CHECKOUT_REF=master | ||
| POKEAPI_CHECKOUT_REF=master |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| HASURA_GRAPHQL_ADMIN_SECRET=pokeapi-change-me | ||
| HASURA_GRAPHQL_ADMIN_SECRET=pokeapi-change-me |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| POSTGRES_PASSWORD=pokeapi-change-me | ||
| POSTGRES_PASSWORD=pokeapi-change-me |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import argparse | ||
| import csv | ||
| import locale | ||
| import typing | ||
| from concurrent.futures import ThreadPoolExecutor, as_completed | ||
|
|
||
|
|
||
| def check_csv(filename: str, encoding: str, delimiter: str) -> tuple[str, bool, str]: | ||
| try: | ||
| with open(filename, encoding=encoding, newline='') as f: | ||
| reader = csv.reader(f, delimiter=delimiter) | ||
| first_len = None | ||
| for line_no, row in enumerate(reader, start=1): | ||
| if first_len is None: | ||
| first_len = len(row) | ||
| elif len(row) != first_len: | ||
| message = f'line {line_no}: contains inconsistent columns (expected {first_len}, got {len(row)})' | ||
| return filename, False, message | ||
| except UnicodeDecodeError: | ||
| return filename, False, f'failed to decode csv text file using {encoding!r}' | ||
|
|
||
| return filename, True, '' | ||
|
|
||
|
|
||
| def main(argv: typing.Sequence[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('-e', '--encoding', dest='encoding', default=locale.getpreferredencoding()) | ||
| parser.add_argument('-d', '--delimiter', dest='delimiter', default=',') | ||
| parser.add_argument('-j', '--jobs', dest='jobs', type=int, default=8) | ||
| parser.add_argument('filenames', nargs='*', help='Filenames to check.') | ||
| args = parser.parse_args(argv) | ||
|
|
||
| print(f'Checking CSV files using encoding {args.encoding!r} and delimiter {args.delimiter!r}...') | ||
| print(f'Checking {len(args.filenames)} files with {args.jobs} workers...') | ||
|
|
||
| retval = 0 | ||
| with ThreadPoolExecutor(max_workers=args.jobs) as executor: | ||
| futures = { | ||
| executor.submit(check_csv, filename, args.encoding, args.delimiter): filename | ||
| for filename in args.filenames | ||
| } | ||
| for future in as_completed(futures): | ||
| filename, ok, msg = future.result() | ||
| if not ok: | ||
| print(f'{filename}: {msg}') | ||
| retval = 1 | ||
| return retval | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| raise SystemExit(main()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,4 +89,4 @@ | |
| writer.writerow(header) | ||
| entriesList.sort() | ||
| writer.writerows(entriesList) | ||
| print("Done") | ||
| print("Done") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,4 +86,4 @@ module.exports.new_pokemons = [ | |
| "Zacian", | ||
| "Zamazenta", | ||
| "Zarude" | ||
| ] | ||
| ] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in another comment, the pre-commit checks include a full "build-and-test" run. Adding it to the CI would be re-doing these tests?
Would it make sense to leave this off of the pre-commit altogether and otherwise leave the Makefile as-is?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/PokeAPI/pokeapi/pull/1602/changes#diff-63a9c44a44acf85fea213a857769990937107cf072831e1a26808cfde9d096b9R60
since its marked as manual its automatically skipped in the ci too along with ruff format since the ci already does that
this is the default suite that runs https://app.circleci.com/pipelines/github/PokeAPI/pokeapi/2512/workflows/5be65b3b-415b-46ad-87db-634725d10589/jobs/6139/parallel-runs/0/steps/0-104
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok great - I mentioned this in the other comment. Thanks for the clarification!