10
10
import uuid
11
11
from collections import defaultdict
12
12
from pathlib import Path
13
- from typing import Literal , Optional , cast
13
+ from typing import Literal , cast
14
14
15
15
import falcon
16
16
import orjson
@@ -119,9 +119,9 @@ def get_server_type_from_req(
119
119
COUNTRY_NAME_TO_ENUM = {COUNTRY_CODE_TO_NAME [item ]: item for item in Country }
120
120
121
121
122
- def get_countries_from_req (req : falcon .Request ) -> Optional [ list [Country ]] :
122
+ def get_countries_from_req (req : falcon .Request ) -> list [Country ] | None :
123
123
"""Parse `countries` query string from request."""
124
- countries_str : Optional [ list [str ]] = req .get_param_as_list ("countries" )
124
+ countries_str : list [str ] | None = req .get_param_as_list ("countries" )
125
125
if countries_str is None :
126
126
return None
127
127
@@ -202,12 +202,12 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
202
202
response : JSONType = {}
203
203
count : int = req .get_param_as_int ("count" , min_value = 1 , default = 25 )
204
204
page : int = req .get_param_as_int ("page" , min_value = 1 , default = 1 )
205
- keep_types : Optional [ list [str ]] = req .get_param_as_list (
205
+ keep_types : list [str ] | None = req .get_param_as_list (
206
206
"insight_types" , required = False
207
207
)
208
- barcode : Optional [ str ] = normalize_req_barcode (req .get_param ("barcode" ))
209
- annotated : Optional [ bool ] = req .get_param_as_bool ("annotated" )
210
- annotation : Optional [ int ] = req .get_param_as_int ("annotation" )
208
+ barcode : str | None = normalize_req_barcode (req .get_param ("barcode" ))
209
+ annotated : bool | None = req .get_param_as_bool ("annotated" )
210
+ annotation : int | None = req .get_param_as_int ("annotation" )
211
211
value_tag : str = req .get_param ("value_tag" )
212
212
brands = req .get_param_as_list ("brands" ) or None
213
213
predictor = req .get_param ("predictor" )
@@ -272,8 +272,8 @@ class RandomInsightResource:
272
272
def on_get (self , req : falcon .Request , resp : falcon .Response ):
273
273
response : JSONType = {}
274
274
275
- insight_type : Optional [ str ] = req .get_param ("type" )
276
- value_tag : Optional [ str ] = req .get_param ("value_tag" )
275
+ insight_type : str | None = req .get_param ("type" )
276
+ value_tag : str | None = req .get_param ("value_tag" )
277
277
count : int = req .get_param_as_int ("count" , min_value = 1 , default = 25 )
278
278
predictor = req .get_param ("predictor" )
279
279
server_type = get_server_type_from_req (req )
@@ -303,7 +303,7 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
303
303
resp .media = response
304
304
305
305
306
- def parse_auth (req : falcon .Request ) -> Optional [ OFFAuthentication ] :
306
+ def parse_auth (req : falcon .Request ) -> OFFAuthentication | None :
307
307
session_cookie = req .get_cookie_values ("session" )
308
308
309
309
if session_cookie :
@@ -917,13 +917,13 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
917
917
count : int = req .get_param_as_int (
918
918
"count" , min_value = 1 , max_value = 2000 , default = 25
919
919
)
920
- type_ : Optional [ str ] = req .get_param ("type" )
921
- barcode : Optional [ str ] = normalize_req_barcode (req .get_param ("barcode" ))
922
- value : Optional [ str ] = req .get_param ("value" )
923
- taxonomy_value : Optional [ str ] = req .get_param ("taxonomy_value" )
924
- min_confidence : Optional [ float ] = req .get_param_as_float ("min_confidence" )
920
+ type_ : str | None = req .get_param ("type" )
921
+ barcode : str | None = normalize_req_barcode (req .get_param ("barcode" ))
922
+ value : str | None = req .get_param ("value" )
923
+ taxonomy_value : str | None = req .get_param ("taxonomy_value" )
924
+ min_confidence : float | None = req .get_param_as_float ("min_confidence" )
925
925
random : bool = req .get_param_as_bool ("random" , default = False )
926
- annotated : Optional [ bool ] = req .get_param_as_bool ("annotated" )
926
+ annotated : bool | None = req .get_param_as_bool ("annotated" )
927
927
928
928
if type_ is None and (value is not None or taxonomy_value is not None ):
929
929
raise falcon .HTTPBadRequest (
@@ -1031,7 +1031,7 @@ def on_put(self, req: falcon.Request, resp: falcon.Response, logo_id: int):
1031
1031
)
1032
1032
1033
1033
with db .atomic ():
1034
- logo : Optional [ LogoAnnotation ] = LogoAnnotation .get_or_none (id = logo_id )
1034
+ logo : LogoAnnotation | None = LogoAnnotation .get_or_none (id = logo_id )
1035
1035
if logo is None :
1036
1036
resp .status = falcon .HTTP_404
1037
1037
return
@@ -1201,7 +1201,7 @@ def on_post(self, req: falcon.Request, resp: falcon.Response):
1201
1201
1202
1202
class ANNResource :
1203
1203
def on_get (
1204
- self , req : falcon .Request , resp : falcon .Response , logo_id : Optional [ int ] = None
1204
+ self , req : falcon .Request , resp : falcon .Response , logo_id : int | None = None
1205
1205
):
1206
1206
"""Search for nearest neighbors of:
1207
1207
- a random logo (if logo_id not provided)
@@ -1311,9 +1311,9 @@ def on_get(self, req: falcon.Request, resp: falcon.Response, barcode: str):
1311
1311
# hash of the IPs as a backup.
1312
1312
device_id = device_id_from_request (req )
1313
1313
1314
- auth : Optional [ OFFAuthentication ] = parse_auth (req )
1314
+ auth : OFFAuthentication | None = parse_auth (req )
1315
1315
1316
- keep_types : Optional [ list [str ]] = req .get_param_as_list (
1316
+ keep_types : list [str ] | None = req .get_param_as_list (
1317
1317
"insight_types" , required = False
1318
1318
)
1319
1319
keep_types = filter_question_insight_types (keep_types )
@@ -1379,23 +1379,23 @@ def get_questions_resource_on_get(
1379
1379
page : int = req .get_param_as_int ("page" , min_value = 1 , default = 1 )
1380
1380
count : int = req .get_param_as_int ("count" , min_value = 1 , default = 25 )
1381
1381
lang : str = req .get_param ("lang" , default = "en" )
1382
- keep_types : Optional [ list [str ]] = req .get_param_as_list (
1382
+ keep_types : list [str ] | None = req .get_param_as_list (
1383
1383
"insight_types" , required = False
1384
1384
)
1385
1385
keep_types = filter_question_insight_types (keep_types )
1386
1386
value_tag : str = req .get_param ("value_tag" )
1387
1387
brands = req .get_param_as_list ("brands" ) or None
1388
- reserved_barcode : Optional [ bool ] = req .get_param_as_bool (
1388
+ reserved_barcode : bool | None = req .get_param_as_bool (
1389
1389
"reserved_barcode" , default = False
1390
1390
)
1391
1391
server_type = get_server_type_from_req (req )
1392
1392
countries = get_countries_from_req (req )
1393
1393
# filter by annotation campaigns
1394
- campaigns : Optional [ list [str ]] = req .get_param_as_list ("campaigns" ) or None
1394
+ campaigns : list [str ] | None = req .get_param_as_list ("campaigns" ) or None
1395
1395
1396
1396
if campaigns is None :
1397
1397
# `campaign` is a deprecated field, use campaigns now instead
1398
- campaign : Optional [ str ] = req .get_param ("campaign" )
1398
+ campaign : str | None = req .get_param ("campaign" )
1399
1399
campaigns = [campaign ] if campaign is not None else None
1400
1400
1401
1401
predictor = req .get_param ("predictor" )
@@ -1404,7 +1404,7 @@ def get_questions_resource_on_get(
1404
1404
# hash of the IPs as a backup.
1405
1405
device_id = device_id_from_request (req )
1406
1406
1407
- auth : Optional [ OFFAuthentication ] = parse_auth (req )
1407
+ auth : OFFAuthentication | None = parse_auth (req )
1408
1408
1409
1409
if reserved_barcode :
1410
1410
# Include all results, including non reserved barcodes
@@ -1542,10 +1542,10 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
1542
1542
response : JSONType = {}
1543
1543
count : int = req .get_param_as_int ("count" , min_value = 1 , default = 25 )
1544
1544
page : int = req .get_param_as_int ("page" , min_value = 1 , default = 1 )
1545
- with_predictions : Optional [ bool ] = req .get_param_as_bool (
1545
+ with_predictions : bool | None = req .get_param_as_bool (
1546
1546
"with_predictions" , default = False
1547
1547
)
1548
- barcode : Optional [ str ] = normalize_req_barcode (req .get_param ("barcode" ))
1548
+ barcode : str | None = normalize_req_barcode (req .get_param ("barcode" ))
1549
1549
server_type = get_server_type_from_req (req )
1550
1550
1551
1551
get_images_ = functools .partial (
@@ -1573,9 +1573,9 @@ class PredictionCollection:
1573
1573
def on_get (self , req : falcon .Request , resp : falcon .Response ):
1574
1574
page : int = req .get_param_as_int ("page" , min_value = 1 , default = 1 )
1575
1575
count : int = req .get_param_as_int ("count" , min_value = 1 , default = 25 )
1576
- barcode : Optional [ str ] = normalize_req_barcode (req .get_param ("barcode" ))
1576
+ barcode : str | None = normalize_req_barcode (req .get_param ("barcode" ))
1577
1577
value_tag : str = req .get_param ("value_tag" )
1578
- keep_types : Optional [ list [str ]] = req .get_param_as_list ("types" , required = False )
1578
+ keep_types : list [str ] | None = req .get_param_as_list ("types" , required = False )
1579
1579
server_type = get_server_type_from_req (req )
1580
1580
1581
1581
if keep_types :
@@ -1614,15 +1614,15 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
1614
1614
count : int = req .get_param_as_int ("count" , min_value = 1 , default = 25 )
1615
1615
insight_type : str = req .get_param ("type" )
1616
1616
countries = get_countries_from_req (req )
1617
- reserved_barcode : Optional [ bool ] = req .get_param_as_bool (
1617
+ reserved_barcode : bool | None = req .get_param_as_bool (
1618
1618
"reserved_barcode" , default = False
1619
1619
)
1620
1620
server_type = get_server_type_from_req (req )
1621
1621
# filter by annotation campaigns
1622
- campaigns : Optional [ list [str ]] = req .get_param_as_list ("campaigns" ) or None
1622
+ campaigns : list [str ] | None = req .get_param_as_list ("campaigns" ) or None
1623
1623
if campaigns is None :
1624
1624
# `campaign` is a deprecated field, use campaigns now instead
1625
- campaign : Optional [ str ] = req .get_param ("campaign" )
1625
+ campaign : str | None = req .get_param ("campaign" )
1626
1626
campaigns = [campaign ] if campaign is not None else None
1627
1627
1628
1628
predictor = req .get_param ("predictor" )
@@ -1660,9 +1660,9 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
1660
1660
response : JSONType = {}
1661
1661
count : int = req .get_param_as_int ("count" , min_value = 1 , default = 25 )
1662
1662
page : int = req .get_param_as_int ("page" , min_value = 1 , default = 1 )
1663
- with_logo : Optional [ bool ] = req .get_param_as_bool ("with_logo" , default = False )
1664
- barcode : Optional [ str ] = normalize_req_barcode (req .get_param ("barcode" ))
1665
- type : Optional [ str ] = req .get_param ("type" )
1663
+ with_logo : bool | None = req .get_param_as_bool ("with_logo" , default = False )
1664
+ barcode : str | None = normalize_req_barcode (req .get_param ("barcode" ))
1665
+ type : str | None = req .get_param ("type" )
1666
1666
server_type = get_server_type_from_req (req )
1667
1667
1668
1668
query_parameters = {
@@ -1695,9 +1695,9 @@ def on_get(self, req: falcon.Request, resp: falcon.Response):
1695
1695
class LogoAnnotationCollection :
1696
1696
def on_get (self , req : falcon .Request , resp : falcon .Response ):
1697
1697
response : JSONType = {}
1698
- barcode : Optional [ str ] = normalize_req_barcode (req .get_param ("barcode" ))
1698
+ barcode : str | None = normalize_req_barcode (req .get_param ("barcode" ))
1699
1699
server_type = get_server_type_from_req (req )
1700
- keep_types : Optional [ list [str ]] = req .get_param_as_list ("types" , required = False )
1700
+ keep_types : list [str ] | None = req .get_param_as_list ("types" , required = False )
1701
1701
value_tag : str = req .get_param ("value_tag" )
1702
1702
page : int = req .get_param_as_int ("page" , min_value = 1 , default = 1 )
1703
1703
count : int = req .get_param_as_int ("count" , min_value = 1 , default = 25 )
0 commit comments