Skip to content

Commit

Permalink
add ruff checking
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Dec 23, 2024
1 parent 2f33e9e commit 4e597da
Show file tree
Hide file tree
Showing 204 changed files with 506 additions and 741 deletions.
6 changes: 3 additions & 3 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Legal Boilerplate

Look, I get it.
Contributing to HOPE Workspace, I retain all rights, title and interest in and to my contributions, and by keeping
this boilerplate intact I confirm that HOPE Workspace can use, modify, copy, and redistribute my contributions,
Look, I get it.
Contributing to HOPE Workspace, I retain all rights, title and interest in and to my contributions, and by keeping
this boilerplate intact I confirm that HOPE Workspace can use, modify, copy, and redistribute my contributions,
under HOPE's choice of terms.
8 changes: 4 additions & 4 deletions .github/actions/docker_build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ runs:
run: |
build_date=$(date +"%Y-%m-%d %H:%M")
echo "BUILD_DATE=$build_date" >> $GITHUB_ENV
if [[ "${{inputs.target}}" == "dist" ]]; then
echo "TAG_PREFIX=" >> $GITHUB_ENV
else
Expand Down Expand Up @@ -114,7 +114,7 @@ runs:
- name: "Check Image"
id: image_status
shell: bash
run: |
run: |
set +e
echo "::notice::ℹ Checking checksum for ${{ steps.image_name.outputs.name }}"
image_checksum=$(regctl image inspect \
Expand All @@ -134,7 +134,7 @@ runs:
echo "::warning::🤬 Checksum: found '${image_checksum}' expected '${code_checksum}'"
echo "updated=false" >> $GITHUB_OUTPUT
fi
if [[ "${{inputs.rebuild}}" == "true" ]]; then
if [[ "${{inputs.rebuild}}" == "true" ]]; then
echo "::warning::⚠ Forced build due input parameter"
fi
- name: Set up Docker BuildX
Expand Down Expand Up @@ -188,7 +188,7 @@ runs:
if: (steps.image_status.outputs.updated != 'true' || inputs.rebuild == 'true') && inputs.dryrun != 'true'
shell: bash
run: |
echo "${{ toJSON(steps.build_push.outputs) }}"
echo "${{ toJSON(steps.build_push.outputs) }}"
regctl image inspect -p linux/amd64 ${{ steps.image_name.outputs.name }}
echo "::notice:: Image ${{ steps.meta.outputs.tags }} successfully built and pushed"
echo "created=true" >> $GITHUB_OUTPUT
16 changes: 8 additions & 8 deletions .github/actions/last_commit/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ outputs:
runs:
using: "composite"
steps:
- name: Setup Environment (PR)
if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: |
- name: Setup Environment (PR)
if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: |
echo "LAST_COMMIT_SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
- name: Setup Environment (Push)
if: ${{ github.event_name == 'push' }}
- name: Setup Environment (Push)
if: ${{ github.event_name == 'push' }}
shell: bash
run: |
run: |
echo "LAST_COMMIT_SHA=${GITHUB_SHA}" >> $GITHUB_ENV
- id: result
shell: bash
run: |
raw=${{env.LAST_COMMIT_SHA}}
raw=${{env.LAST_COMMIT_SHA}}
echo "last_commit_sha=$raw" >> $GITHUB_OUTPUT
echo "last_commit_short_sha=${raw::8}" >> $GITHUB_OUTPUT
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
The property '#/updates/0/package-ecosystem' value "" did not match one of the following values: npm, bundler, composer, devcontainers, maven, mix, cargo, gradle, nuget, gomod, docker, elm, gitsubmodule, github-actions, pip, terraform, pub, swift
version: 2
updates:
- package-ecosystem: "pip"
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "weekly"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ jobs:
# django-admin upgrade

- name: Publish images
run: |
run: |
docker push ${{needs.test.outputs.image}}
docker inspect ${{needs.test.outputs.image}} | jq -r '.[0].Config.Labels'
echo "::notice::✅ Image ${{needs.test.outputs.image}} built and pushed"
39 changes: 12 additions & 27 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/adamchainz/djade-pre-commit
rev: "1.3.2"
hooks:
Expand All @@ -10,28 +15,17 @@ repos:
hooks:
- id: isort
stages: [pre-commit]
- repo: https://github.com/ambv/black
rev: 24.10.0
hooks:
- id: black
args: [--config=pyproject.toml]
exclude: "migrations|snapshots"
stages: [pre-commit]
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8
args: [--config=.flake8]
stages: [ pre-commit ]
- repo: https://github.com/PyCQA/bandit
rev: '1.7.10' # Update me!
hooks:
- id: bandit
args: ["-c", "bandit.yaml"]
- repo: https://github.com/twisted/towncrier
rev: 24.8.0
hooks:
- id: towncrier-check
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
- id: ruff
- id: ruff-format
args:
- --check

- repo: https://github.com/saxix/pch
rev: '0.1'
Expand All @@ -47,12 +41,3 @@ repos:
- src
- tests
stages: [ pre-push ]

- id: check-forbidden
args:
- -p
- /\.showbrowser\(/
- -p
- /print\(111/
stages: [ pre-commit ]
additional_dependencies: [ setuptools ]
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ RUN --mount=type=cache,target=/root/.uv-cache \

# ------- production only deps-------
FROM builder AS production
ENV PATH=/app/.venv/bin:/usr/local/bin/:/usr/bin:/bin \
ENV PATH=/venv/bin:/usr/local/bin/:/usr/bin:/bin \
DJANGO_SETTINGS_MODULE=country_workspace.config.settings \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
Expand Down Expand Up @@ -147,7 +147,7 @@ COPY . /app/
COPY --chown=hope:unicef --from=production /venv /venv
COPY --from=builder /usr/local/bin/uwsgi /usr/local/bin/uv /usr/local/bin/
RUN --mount=type=cache,target=/root/.uv-cache \
uv --cache-dir=/root/.uv-cache pip install --link-mode=copy --no-deps . \
uv --cache-dir=/root/.uv-cache pip install --no-deps . \
&& rm -fr /app


Expand Down
2 changes: 1 addition & 1 deletion docs/_theme/css/style.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions docs/src/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ Install [uv](https://docs.astral.sh/uv/)
wget https://github.com/sqreen/PyMiniRacer/files/7575004/libmini_racer.dylib.zip

1. Unzip The Dylib file

unzip libmini_racer.dylib.zip

1. MV Dylib file to your site-packages

mv libmini_racer.dylib /opt/homebrew/Caskroom/miniconda/base/lib/python3.8/site-packages/py_mini_racer/.

1. Import Success.

>>> from py_mini_racer import MiniRacer


Expand All @@ -42,11 +42,11 @@ CSS. CSS sources are located in the `country_workspace/workspaces/theme/static_s
If you need to edit the CSS follow the below steps:

1. Install node dependencies

./manage.py tailwind install

1. Configure the enviroment

export EXTRA_APPS="country_workspace.contrib.hope,django_browser_reload"
export EXTRA_MIDDLEWARES="django_browser_reload.middleware.BrowserReloadMiddleware,"

Expand Down
2 changes: 1 addition & 1 deletion docs/src/dev_helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
show_bases: false
show_bases: false
show_root_heading: true
show_source: true
show_source: true
2 changes: 1 addition & 1 deletion docs/src/flows/import.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Spreadsheet import


```mermaid
```mermaid
graph TD
A[Upload file] --> B{All records are valid?}
Expand Down
4 changes: 2 additions & 2 deletions docs/src/flows/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ erDiagram
Spreadsheet {
string title
}
Error {
string sheet
int row
string column
}
Program ||--o{ Spreadsheet: has
Program ||--o{ Individual: has
Program ||--o{ Household: has
Expand Down
65 changes: 65 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
target-version = "py312"
line-length = 120

[lint.isort]
case-sensitive = true

[lint]
select = [
"A", ## prevent using keywords that clobber python builtins
"B", # bugbear: security warnings
"BLE", # blind exceptions
"C4", # flake8-comprehensions
"C901", # McCabe complexity
"D", # pydocstyle
"DJ", # flake8-django
"E", # pycodestylex
"E4", "E7", "E9",
"ERA", # eradicate
"F", # pyflakes
"FURB", # refurb
"I", # isort
"ISC", # implicit string concatenation
"PERF", # perflint
"PIE", # flake8-pie
"PL", # PyLint
"S", # bandit,
"SIM", # flake8-simplify
"T10", # flake8-debugger
"UP", # pyupgrade
]
extend-select = ["UP", ]
ignore = [
"B904", # raise-without-from-inside-except: syntax not compatible with py2
"D100", # Missing docstring in public module
"D101", # Missing docstring in public class
"D102", # Missing docstring in public method
"D103", # Missing docstring in public function
"D104", # Missing docstring in public package
"D105", # Missing docstring in magic method
"D106", # Missing docstring in public nested class
"D107", # Missing docstring in `__init__`
"D203", # one-blank-line-before-class
"D212", # multi-line-summary-first-line
"D213", # multi-line-summary-second-line
"E731", # lambda-assignment: lambdas are substential in maintenance of py2/3 codebase
"ISC001", # conflicts with ruff format command
"RUF005", # collection-literal-concatenation: syntax not compatible with py2
"RUF012", # mutable-class-default: typing is not available for py2
"I001", # unsorted imports https://docs.astral.sh/ruff/rules/unsorted-imports/#unsorted-imports-i001
"UP037", # [*] Remove quotes from type annotation
"UP035", # Import from `collections.abc` instead: `Sequence`
"UP031", # Use format specifiers instead of percent format
"SIM108", # Use ternary operator instead of...
"PLR2004", # Magic value used in comparison
"DJ001", # Avoid using `null=True` on string-based fields such as `CharField`
]

[format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[lint.per-file-ignores]
"tests/**.py" = ["S101", "PLR2004", "S", "SIM117", "D"]
1 change: 0 additions & 1 deletion src/country_workspace/admin/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def html_attrs(self):
class IsValidFilter(SimpleListFilter):
title = "Valid"
parameter_name = "valid"
# template = "workspace/adminfilters/combobox.html"

def lookups(self, request, model_admin):
return (
Expand Down
1 change: 0 additions & 1 deletion src/country_workspace/admin/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
@admin.register(Individual)
class IndividualAdmin(BaseModelAdmin):
list_display = ("name", "batch")
# readonly_fields = ("country_office",)
search_fields = ("name",)
list_filter = (
("batch__country_office", LinkedAutoCompleteFilter.factory(parent=None)),
Expand Down
4 changes: 2 additions & 2 deletions src/country_workspace/admin/job.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Sequence
from typing import Sequence

from django.contrib import admin
from django.http import HttpRequest
Expand All @@ -23,7 +23,7 @@ class AsyncJobAdmin(CeleryTaskModelAdmin, BaseModelAdmin):
FailedFilter,
)

def get_readonly_fields(self, request: "HttpRequest", obj: "Optional[AsyncJob]" = None) -> Sequence[str]:
def get_readonly_fields(self, request: "HttpRequest", obj: "AsyncJob | None" = None) -> Sequence[str]:
if obj:
return ("program", "batch", "owner", "local_status", "type", "action", "sentry_id")
return super().get_readonly_fields(request, obj)
4 changes: 2 additions & 2 deletions src/country_workspace/admin/locations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import TYPE_CHECKING, Any, List, Tuple
from typing import TYPE_CHECKING, Any

from django.contrib import admin
from django.contrib.admin import ModelAdmin, RelatedFieldListFilter
Expand Down Expand Up @@ -44,7 +44,7 @@ class AreaTypeAdmin(AdminFiltersMixin, admin.ModelAdmin):


class AreaTypeFilter(RelatedFieldListFilter):
def field_choices(self, field: Any, request: "HttpRequest", model_admin: ModelAdmin) -> List[Tuple[str, str]]:
def field_choices(self, field: Any, request: "HttpRequest", model_admin: ModelAdmin) -> list[tuple[str, str]]:
if "area_type__country__exact" not in request.GET:
return []
return AreaType.objects.filter(country=request.GET["area_type__country__exact"]).values_list("id", "name")
Expand Down
3 changes: 0 additions & 3 deletions src/country_workspace/admin/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ def _action(request: HttpRequest) -> HttpResponse:
success_message="Successfully executed",
)

# base = reverse("admin:country_workspace_individual_changelist")
# btn.href = f"{base}?household__exact={obj.pk}"

@button()
def sync(self, request: HttpRequest) -> None:
from country_workspace.contrib.hope.sync.office import sync_programs
Expand Down
4 changes: 1 addition & 3 deletions src/country_workspace/admin/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ def autocomplete(self, request):
qs = qs.filter(
Q(roles__program__id=program) | Q(is_superuser=True) | Q(roles__country_office__programs__pk=program)
)
results = []
for user in qs.all():
results.append({"id": user.id, "text": user.username})
results = [{"id": user.id, "text": user.username} for user in qs.all()]
res = {"results": results, "pagination": {"more": False}}
return JsonResponse(res)
2 changes: 1 addition & 1 deletion src/country_workspace/cache/ddt.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def process_request(self, request):

@property
def nav_subtitle(self):
return "~{} gets / ~{} sets".format(len(self.gets), len(self.sets))
return f"~{len(self.gets)} gets / ~{len(self.sets)} sets"

def generate_stats(self, request, response):
self.record_stats(
Expand Down
8 changes: 3 additions & 5 deletions src/country_workspace/cache/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
@receiver(post_save)
def update_cache(sender: "type[Model]", instance: Model, **kwargs):
program = None
if isinstance(instance, (Household, Individual, CountryHousehold, CountryIndividual)):
if isinstance(instance, (Household | Individual | CountryHousehold | CountryIndividual)):
program = instance.program
elif isinstance(instance, (Program, CountryProgram)):
elif isinstance(instance, (Program | CountryProgram)):
program = instance
elif isinstance(instance, (AsyncJob, CountryAsyncJob)):
program = instance.program
elif isinstance(instance, (Batch, CountryBatch)):
elif isinstance(instance, (AsyncJob | CountryAsyncJob | Batch | CountryBatch)):
program = instance.program
if program:
cache_manager.incr_cache_version(program=program)
Loading

0 comments on commit 4e597da

Please sign in to comment.