Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Fix type checking

Russ Garrett77416cd6222b
diff --git a/web-backend/__init__.py b/web-backend/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/web-backend/create_indexes.py b/web-backend/create_indexes.py
index 5bc8751..7e5462d 100644
--- a/web-backend/create_indexes.py
+++ b/web-backend/create_indexes.py
@@ -3,7 +3,7 @@ Programmatically create search indexes for tables. This has to be run manually a
`ADMIN_DATABASE_URL` must be set to a connection with the privileges to create indexes.
"""
-from search import SEARCH_LANGUAGES
+from views.search import SEARCH_LANGUAGES
from databases import Database
from config import config
diff --git a/web-backend/data.py b/web-backend/data.py
index 60657db..1daaf23 100644
--- a/web-backend/data.py
+++ b/web-backend/data.py
@@ -69,7 +69,18 @@ async def get_plant_generator_summary(plant_id):
async def latest_stats_date():
- return (await database.fetch_one("SELECT max(time) FROM stats.power_line"))[0]
+ res = await database.fetch_one("SELECT max(time) FROM stats.power_line")
+ return res[0] if res else None
+
+
+def coalesce_result(result, value):
+ if result is None:
+ return value
+
+ res = result.result()
+ if res is None:
+ return value
+ return res[0] or value
async def stats_power_line(union=None, territory_iso3=None, date=None) -> dict:
@@ -94,7 +105,7 @@ async def stats_power_line(union=None, territory_iso3=None, date=None) -> dict:
async with asyncio.TaskGroup() as tg:
for low, high in windowed(chain(VOLTAGE_SCALE, [None]), 2):
- low = low * 1000
+ low = (low or 0) * 1000
query = f"""SELECT sum(length)
FROM stats.power_line {join_clause}
WHERE time = :time
@@ -135,16 +146,18 @@ async def stats_power_line(union=None, territory_iso3=None, date=None) -> dict:
)
)
- lines = {(low, high): res.result()[0] or 0 for (low, high), res in lines.items()}
-
return {
- "lines": lines,
- "total": total.result()[0] or 0.01,
- "unspecified": unspecified.result()[0] or 0,
+ "lines": {
+ (low, high): coalesce_result(res, 0) for (low, high), res in lines.items()
+ },
+ "total": coalesce_result(total, 0.01),
+ "unspecified": coalesce_result(unspecified, 0),
}
-async def plant_stats(country_gid: int = None, territory_iso3: str = None, date=None):
+async def plant_stats(
+ country_gid: Optional[int] = None, territory_iso3: Optional[str] = None, date=None
+):
where_clause = ""
values = {
"time": date or await latest_stats_date(),
diff --git a/web-backend/util.py b/web-backend/util.py
index 757137c..c8c8b0d 100644
--- a/web-backend/util.py
+++ b/web-backend/util.py
@@ -1,4 +1,5 @@
from functools import wraps
+from typing import Optional
from starlette.exceptions import HTTPException
from urllib.parse import unquote_plus
from datetime import timedelta
@@ -22,7 +23,11 @@ def region_required(func):
return wrap_region
-def cache_for(seconds: int = None, hours: int = None, days: int = None):
+def cache_for(
+ seconds: Optional[int] = None,
+ hours: Optional[int] = None,
+ days: Optional[int] = None,
+):
def cache_for_inner(func):
@wraps(func)
async def wrap_cache(*args, **kwargs):