Skip to content

Commit

Permalink
style: Improve typing, pre-commit, updates openfoodfacts package (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturLange authored Mar 8, 2024
1 parent 21c2793 commit ff03b0c
Show file tree
Hide file tree
Showing 33 changed files with 581 additions and 427 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ POSTGRES_PORT=5432

POSTGRES_EXPOSE=127.0.0.1:5432

ENVIRONMENT=net
ENVIRONMENT=net
15 changes: 7 additions & 8 deletions .github/workflows/generate-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
push:
branches:
- main

jobs:
publish-docs:
runs-on: ubuntu-20.04
Expand All @@ -23,32 +23,32 @@
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Setup Python version
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install Poetry
uses: snok/[email protected]
with:
virtualenvs-create: true
virtualenvs-in-project: true

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}

- name: Install dependencies
run: poetry install
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'

- name: Generate documentation
run: bash ./build_mkdocs.sh

# Deploy docs to gh_pages if we are pushing to main
# Example from https://github.com/marketplace/actions/deploy-to-github-pages
- name: Deploy 🚀
Expand All @@ -61,4 +61,3 @@
BRANCH: gh-pages # The branch the action should deploy to.
FOLDER: gh_pages # The folder the action should deploy.
CLEAN: true # Automatically remove deleted files from the deploy branch

2 changes: 1 addition & 1 deletion .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
python-version: '3.11'
- uses: pre-commit/[email protected]
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ dmypy.json

static/app
static/img/*/*
gh_pages
gh_pages
11 changes: 10 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
default_language_version:
python: python3.11
repos:
# Note for all linters: do not forget to update pyproject.toml when updating version.
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/ambv/black
rev: 23.11.0
hooks:
- id: black
language_version: python3.11

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
rev: 7.0.0
hooks:
- id: flake8

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ See [CONTRIBUTING.md](https://github.com/openfoodfacts/open-prices/blob/main/CON
* ![Google Meet](https://img.shields.io/badge/Google%20Meet-00897B?logo=google-meet&logoColor=white) Video call link: https://meet.google.com/oin-hiqp-tmd
* Join by phone: https://tel.meet/oin-hiqp-tmd?pin=5784334159966
* Add the Event to your Calendar by [adding the Open Food Facts community calendar to your calendar](https://wiki.openfoodfacts.org/Events)
* [Weekly Agenda](https://docs.google.com/document/u/0/d/1-OfMAi-cB7mi9_q172EbBCWHkfKDM0zVg4wzULW3pFY/edit): please add the Agenda items as early as you can.
* Make sure to check the Agenda items in advance of the meeting, so that we have the most informed discussions possible.
* [Weekly Agenda](https://docs.google.com/document/u/0/d/1-OfMAi-cB7mi9_q172EbBCWHkfKDM0zVg4wzULW3pFY/edit): please add the Agenda items as early as you can.
* Make sure to check the Agenda items in advance of the meeting, so that we have the most informed discussions possible.
* The meeting will handle Agenda items first, and if time permits, collaborative bug triage.
* We strive to timebox the core of the meeting (decision making) to 30 minutes, with an optional free discussion/live debugging afterwards.
* We take comprehensive notes in the Weekly Agenda of agenda item discussions and of decisions taken.
Expand Down
2 changes: 1 addition & 1 deletion app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
# Extra routes
# ------------------------------------------------------------------------------
@app.get("/api/v1/status", tags=["Status"])
def status_endpoint():
def status_endpoint() -> dict[str, str]:
return {"status": "running"}


Expand Down
7 changes: 4 additions & 3 deletions app/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from starlette.requests import Request
from starlette.status import HTTP_401_UNAUTHORIZED

from app import crud, schemas
from app import crud
from app.db import get_db
from app.models import User


# This class is derived from FastAPI's OAuth2PasswordBearer class,
Expand Down Expand Up @@ -70,7 +71,7 @@ async def __call__(self, request: Request) -> Optional[str]:

def get_current_user(
token: Annotated[str, Depends(oauth2_scheme)], db: Session = Depends(get_db)
) -> schemas.UserCreate:
) -> User:
"""Get the current user if authenticated.
This function is used as a dependency in endpoints that require
Expand All @@ -96,7 +97,7 @@ def get_current_user(
def get_current_user_optional(
token: Annotated[str, Depends(oauth2_scheme_no_error)],
db: Session = Depends(get_db),
) -> schemas.UserCreate | None:
) -> User | None:
"""Get the current user if authenticated, None otherwise.
This function is used as a dependency in endpoints that require
Expand Down
12 changes: 6 additions & 6 deletions app/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import typer

app = typer.Typer()
typer_app = typer.Typer()


@app.command()
@typer_app.command()
def import_product_db(batch_size: int = 1000) -> None:
"""Import from DB JSONL dump to insert/update product table."""
from app.db import session
from app.tasks import import_product_db
from app.utils import get_logger
from app.utils import get_logger # type: ignore

get_logger()
db = session()
import_product_db(db, batch_size=batch_size)


@app.command()
@typer_app.command()
def run_scheduler() -> None:
"""Launch the scheduler."""
from app import scheduler
from app.utils import get_logger
from app.utils import get_logger # type: ignore

get_logger()
scheduler.run()


def main() -> None:
app()
typer_app()
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LoggingLevel(Enum):
ERROR: str = "ERROR"
CRITICAL: str = "CRITICAL"

def to_int(self):
def to_int(self) -> int:
if self is LoggingLevel.NOTSET:
return 0
elif self is LoggingLevel.DEBUG:
Expand Down
Loading

0 comments on commit ff03b0c

Please sign in to comment.