Skip to content

Commit

Permalink
Feature/pre commit (#24)
Browse files Browse the repository at this point in the history
* Add pre-commit with ruff and mypy

Fix errors and warning flagged by mypy around typing.

* Add pytest to pre-commit

Required adding relative path handling to settings.py to manage running uvicorn/pytest from the space2stats_api directory but also support running pre-commit from the root.

* Small typo fix in relative path

* Update from feedback on PR

Remove execution of tests on pre-commit. Remove current directory check for setting path of env file.

* Add pre-commit to CI

* Move env file to space2stats_api directory
  • Loading branch information
zacdezgeo authored Aug 1, 2024
1 parent a1c09bc commit 0a96ba1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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/
16 changes: 4 additions & 12 deletions space2stats_api/app/routers/api.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]])
Expand Down
4 changes: 2 additions & 2 deletions space2stats_api/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
File renamed without changes.
1 change: 0 additions & 1 deletion space2stats_api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion space2stats_api/tests/test_h3_utils.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down

0 comments on commit 0a96ba1

Please sign in to comment.