Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit a4e8e72

Browse files
author
Phil Varner
committed
change types of backend methods, refactor use of mypy
1 parent a54e282 commit a4e8e72

File tree

11 files changed

+41
-44
lines changed

11 files changed

+41
-44
lines changed

.github/workflows/pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ jobs:
2424
run: |
2525
python -m pip install pre-commit
2626
pre-commit run --all-files
27+
mypy src
2728
- name: Run tests
2829
run: poetry run nox

.pre-commit-config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ repos:
1919
rev: v1.13.0
2020
hooks:
2121
- id: mypy
22-
files: ".*\\.py$"
22+
args: [] # override default of [--strict, --ignore-missing-imports]
23+
files: src/
2324
additional_dependencies:
2425
- types-pyRFC3339~=1.1.1
26+
- pydantic~=2.9.2
27+
- returns~=0.23.0
28+
- fastapi~=0.115.0
29+
- geojson_pydantic~=1.1.1

bin/server.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
from uuid import uuid4
33

44
from fastapi import FastAPI, Request
5-
from returns.maybe import Maybe, Nothing
5+
from returns.maybe import Maybe
66
from returns.result import Failure, Result, Success
77

88
from stapi_fastapi.backends.product_backend import ProductBackend
99
from stapi_fastapi.backends.root_backend import RootBackend
10-
from stapi_fastapi.exceptions import failure_with
1110
from stapi_fastapi.models.conformance import CORE
1211
from stapi_fastapi.models.opportunity import (
1312
Opportunity,
@@ -39,24 +38,20 @@ class MockRootBackend(RootBackend):
3938
def __init__(self, orders: MockOrderDB) -> None:
4039
self._orders: MockOrderDB = orders
4140

42-
async def get_orders(
43-
self, request: Request
44-
) -> Result[OrderCollection, Maybe[Exception]]:
41+
async def get_orders(self, request: Request) -> Result[OrderCollection, Exception]:
4542
"""
4643
Show all orders.
4744
"""
4845
return Success(OrderCollection(features=list(self._orders.values())))
4946

5047
async def get_order(
5148
self, order_id: str, request: Request
52-
) -> Result[Order, Maybe[Exception]]:
49+
) -> Result[Maybe[Order], Exception]:
5350
"""
5451
Show details for order with `order_id`.
5552
"""
56-
if order := self._orders.get(order_id):
57-
return Success(order)
5853

59-
return Failure(Nothing)
54+
return Success(Maybe.from_optional(self._orders.get(order_id)))
6055

6156

6257
class MockProductBackend(ProductBackend):
@@ -70,17 +65,17 @@ async def search_opportunities(
7065
product_router: ProductRouter,
7166
search: OpportunityRequest,
7267
request: Request,
73-
) -> Result[list[Opportunity], Maybe[Exception]]:
68+
) -> Result[list[Opportunity], Exception]:
7469
try:
7570
return Success(
7671
[o.model_copy(update=search.model_dump()) for o in self._opportunities]
7772
)
7873
except Exception as e:
79-
return failure_with(e)
74+
return Failure(e)
8075

8176
async def create_order(
8277
self, product_router: ProductRouter, payload: OrderRequest, request: Request
83-
) -> Result[Order, Maybe[Exception]]:
78+
) -> Result[Order, Exception]:
8479
"""
8580
Create a new order.
8681
"""
@@ -112,7 +107,7 @@ async def create_order(
112107
self._orders[order.id] = order
113108
return Success(order)
114109
except Exception as e:
115-
return failure_with(e)
110+
return Failure(e)
116111

117112

118113
class MyOpportunityProperties(OpportunityProperties):

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pytest-coverage = "^0.0"
2929
pyrfc3339 = "^1.1"
3030
pre-commit = "^3.7.0"
3131
nox = "^2024.4.15"
32+
mypy = "^1.13.0"
3233

3334
[tool.poetry.scripts]
3435
dev = "stapi_fastapi.__dev__:cli"
@@ -78,3 +79,10 @@ build-backend = "poetry_dynamic_versioning.backend"
7879

7980
[tool.poetry-dynamic-versioning]
8081
enable = true
82+
83+
[[tool.mypy.overrides]]
84+
module = "pygeofilter.parsers.*"
85+
ignore_missing_imports = true
86+
87+
# [tool.mypy]
88+
#plugins = ['pydantic.mypy']

src/stapi_fastapi/backends/product_backend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Protocol
44

55
from fastapi import Request
6-
from returns.maybe import Maybe
76
from returns.result import Result
87

98
from stapi_fastapi.models.opportunity import Opportunity, OpportunityRequest
@@ -17,7 +16,7 @@ async def search_opportunities(
1716
product_router: ProductRouter,
1817
search: OpportunityRequest,
1918
request: Request,
20-
) -> Result[list[Opportunity], Maybe[Exception]]:
19+
) -> Result[list[Opportunity], Exception]:
2120
"""
2221
Search for ordering opportunities for the given search parameters.
2322
@@ -30,7 +29,7 @@ async def create_order(
3029
product_router: ProductRouter,
3130
search: OrderRequest,
3231
request: Request,
33-
) -> Result[Order, Maybe[Exception]]:
32+
) -> Result[Order, Exception]:
3433
"""
3534
Create a new order.
3635

src/stapi_fastapi/backends/root_backend.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88

99

1010
class RootBackend(Protocol): # pragma: nocover
11-
async def get_orders(
12-
self, request: Request
13-
) -> Result[OrderCollection, Maybe[Exception]]:
11+
async def get_orders(self, request: Request) -> Result[OrderCollection, Exception]:
1412
"""
1513
Return a list of existing orders.
1614
"""
1715
...
1816

1917
async def get_order(
2018
self, order_id: str, request: Request
21-
) -> Result[Order, Maybe[Exception]]:
19+
) -> Result[Maybe[Order], Exception]:
2220
"""
2321
Get details for order with `order_id`.
2422

src/stapi_fastapi/exceptions.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
from typing import Any, Optional
22

33
from fastapi import HTTPException, status
4-
from returns.maybe import Some
5-
from returns.result import Failure
6-
7-
8-
def failure_with(e: Exception) -> Failure[Some[Exception]]:
9-
return Failure(Some(e))
104

115

126
class StapiException(HTTPException):

src/stapi_fastapi/models/product.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def with_links(self: Self, links: list[Link] | None = None) -> Self:
8484

8585

8686
class ProductsCollection(BaseModel):
87-
type_: Literal["ProductCollection"] = Field("ProductCollection", alias="type")
87+
type_: Literal["ProductCollection"] = Field(
88+
default="ProductCollection", alias="type"
89+
)
8890
links: list[Link] = Field(default_factory=list)
8991
products: list[Product]

src/stapi_fastapi/types/filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from typing import Annotated
1+
from typing import Annotated, Any
22

33
from pydantic import BeforeValidator
44
from pygeofilter.parsers import cql2_json
55

66

7-
def validate(v: dict):
7+
def validate(v: dict[str, Any]) -> dict[str, Any]:
88
if v:
99
try:
1010
cql2_json.parse({"filter": v})

src/stapi_fastapi/types/json_schema_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
)
99

1010

11-
def validate(v: Any):
11+
def validate(v: Any) -> Any:
1212
if not issubclass(v, BaseModel):
1313
raise RuntimeError("BaseModel class required")
1414
return v

0 commit comments

Comments
 (0)