diff --git a/.github/workflows/test.yml b/.github/workflows/ci.yml similarity index 88% rename from .github/workflows/test.yml rename to .github/workflows/ci.yml index 81d8e28..7687425 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/ci.yml @@ -27,9 +27,13 @@ jobs: run: | python -m pip install --upgrade pip pip install -r space2stats_api/requirements.txt + pip install pre-commit - name: Set PYTHONPATH run: echo "PYTHONPATH=$(pwd)/space2stats_api" >> $GITHUB_ENV + - name: Run pre-commit + run: pre-commit run --all-files + - name: Run tests run: pytest space2stats_api/tests \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..d8ad7b7 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,15 @@ +repos: + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.5.5 + hooks: + - id: ruff + args: [--fix] + files: ^space2stats_api/ + + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.11.1 + hooks: + - id: mypy + args: [--ignore-missing-imports] + files: ^space2stats_api/ + exclude: ^space2stats/env/ diff --git a/space2stats_api/app/routers/api.py b/space2stats_api/app/routers/api.py index 3935c87..9dabf67 100644 --- a/space2stats_api/app/routers/api.py +++ b/space2stats_api/app/routers/api.py @@ -1,7 +1,7 @@ -from typing import List, Dict, Any, Literal, Optional +from typing import List, Dict, Any, Literal, Optional, TypeAlias from fastapi import APIRouter -from pydantic import BaseModel, create_model +from pydantic import BaseModel from geojson_pydantic import Feature, Polygon from app.utils.h3_utils import generate_h3_ids, generate_h3_geometries @@ -10,22 +10,14 @@ router = APIRouter() -AOIModel = Feature[Polygon, Dict] +AOIModel: TypeAlias = Feature[Polygon, Dict] class SummaryRequest(BaseModel): aoi: AOIModel spatial_join_method: Literal["touches", "centroid", "within"] fields: List[str] - geometry: Optional[Literal["polygon", "point"]] = False - - -def create_response_model(fields: List[str]): - field_definitions = {field: (Any, ...) for field in fields} - field_definitions["hex_id"] = (str, ...) - if "geometry" in fields: - field_definitions["geometry"] = (Dict[str, Any], ...) - return create_model("DynamicSummaryResponse", **field_definitions) + geometry: Optional[Literal["polygon", "point"]] = None @router.post("/summary", response_model=List[Dict[str, Any]]) diff --git a/space2stats_api/app/settings.py b/space2stats_api/app/settings.py index 630898a..e6f48eb 100644 --- a/space2stats_api/app/settings.py +++ b/space2stats_api/app/settings.py @@ -8,8 +8,8 @@ class Settings(BaseSettings): DB_USER: str DB_PASSWORD: str DB_TABLE_NAME: str - - model_config = SettingsConfigDict(env_file="../local_db.env") + + model_config = SettingsConfigDict(env_file='db.env') settings = Settings() diff --git a/local_db.env b/space2stats_api/local_db.env similarity index 100% rename from local_db.env rename to space2stats_api/local_db.env diff --git a/space2stats_api/tests/test_api.py b/space2stats_api/tests/test_api.py index 8eb42a2..649f12b 100644 --- a/space2stats_api/tests/test_api.py +++ b/space2stats_api/tests/test_api.py @@ -3,7 +3,6 @@ from unittest.mock import patch from app.main import app -from app.utils.h3_utils import generate_h3_geometries client = TestClient(app) diff --git a/space2stats_api/tests/test_h3_utils.py b/space2stats_api/tests/test_h3_utils.py index fd1d0da..8813581 100644 --- a/space2stats_api/tests/test_h3_utils.py +++ b/space2stats_api/tests/test_h3_utils.py @@ -1,5 +1,5 @@ import pytest -from shapely.geometry import Polygon, mapping, Point +from shapely.geometry import Polygon, mapping from app.utils.h3_utils import generate_h3_ids, generate_h3_geometries polygon_coords = [