Skip to content

Commit

Permalink
fix: remove mypy and Pylance warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
anesson-cs committed Feb 7, 2024
1 parent 721f3f4 commit 8040800
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
34 changes: 11 additions & 23 deletions eodag/plugins/search/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def get_metadata_mapping(
"metadata_mapping", self.config.metadata_mapping
)

def set_sort_by_arg(self, kwargs: Dict[str, Any]) -> Optional[SortByList]: # type: ignore
def set_sort_by_arg(self, kwargs: Dict[str, Any]) -> Optional[SortByList]:
"""Extract the "sortBy" argument from the kwargs or set it to the provider default sort configuration if needed
:param kwargs: Search arguments
Expand All @@ -183,8 +183,8 @@ def set_sort_by_arg(self, kwargs: Dict[str, Any]) -> Optional[SortByList]: # ty
)
return sort_by_arg

def transform_sort_by_params_for_search_request(
self, sort_by_arg: SortByList # type: ignore
def transform_sort_by_arg_for_search_request(
self, sort_by_arg: SortByList
) -> Union[str, Dict[str, List[Dict[str, str]]]]:
"""Build the sorting part of the query string or body by transforming
the "sortBy" argument into a provider-specific string or dictionnary
Expand All @@ -207,7 +207,8 @@ def transform_sort_by_params_for_search_request(
else:
sort_by_provider_keyword = list(self.config.sort["sort_body_tpl"].keys())[0]
sort_by_params = {sort_by_provider_keyword: []}
sort_by_params_tmp = []
sort_by_params_tmp: List[Dict[str, str]] = []
sort_by_param: Dict[str, str]
for one_sort_by_param in sort_by_arg:
# Remove leading and trailing whitespace(s) if exist
eodag_sort_param = one_sort_by_param[0]
Expand All @@ -231,7 +232,6 @@ def transform_sort_by_params_for_search_request(
params,
)
sort_order = one_sort_by_param[1]
sort_by_param: Union[Tuple[str, str], Dict[str, str]]
# TODO: remove this code block when search args model validation is embeded
sort_order = sort_order.strip().upper()
if sort_order[:3] != "ASC" and sort_order[:3] != "DES":
Expand All @@ -241,23 +241,14 @@ def transform_sort_by_params_for_search_request(
)
sort_order = sort_order[:3]

if sort_order == "ASC" and self.config.sort.get("sort_url_tpl"):
sort_by_param = (provider_sort_param, "asc")
elif sort_order == "ASC":
if sort_order == "ASC":
sort_by_param = {"field": provider_sort_param, "direction": "asc"}
elif sort_order == "DES" and self.config.sort.get("sort_url_tpl"):
sort_by_param = (provider_sort_param, "desc")
else:
sort_by_param = {"field": provider_sort_param, "direction": "desc"}
for sort_by_param_tmp in sort_by_params_tmp:
# since duplicated tuples or dictionnaries have been removed, if two sorting parameters are equal,
# then their sorting order is different and there is a contradiction that would raise an error
if (
isinstance(sort_by_param, tuple)
and sort_by_param[0] == sort_by_param_tmp[0]
or isinstance(sort_by_param, dict)
and sort_by_param["field"] == sort_by_param_tmp["field"]
):
if sort_by_param["field"] == sort_by_param_tmp["field"]:
raise ValidationError(
"'{}' parameter is called several times to sort results with different sorting orders. "
"Please set it to only one ('ASC' (ASCENDING) or 'DESC' (DESCENDING))".format(
Expand All @@ -277,15 +268,12 @@ def transform_sort_by_params_for_search_request(
self.config.sort["max_sort_params"], self.provider
)
)
if isinstance(sort_by_params, str) and isinstance(sort_by_param, tuple):
if isinstance(sort_by_params, str):
sort_by_params += self.config.sort["sort_url_tpl"].format(
sort_param=sort_by_param[0], sort_order=sort_by_param[1]
sort_param=sort_by_param["field"],
sort_order=sort_by_param["direction"],
)
else:
assert (
isinstance(sort_by_params, dict)
and sort_by_provider_keyword
and isinstance(sort_by_param, dict)
)
assert sort_by_provider_keyword is not None
sort_by_params[sort_by_provider_keyword].append(sort_by_param)
return sort_by_params
8 changes: 4 additions & 4 deletions eodag/plugins/search/qssearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,11 @@ def query(
# remove "product_type" from search args if exists for compatibility with QueryStringSearch methods
kwargs.pop("product_type", None)

sort_by_arg: Optional[SortByList] = self.set_sort_by_arg(kwargs) # type: ignore
sort_by_arg: Optional[SortByList] = self.set_sort_by_arg(kwargs)
sort_by_params: Union[str, Dict[str, List[Dict[str, str]]]] = (
""
if sort_by_arg is None
else self.transform_sort_by_params_for_search_request(sort_by_arg)
else self.transform_sort_by_arg_for_search_request(sort_by_arg)
)

provider_product_type = self.map_product_type(product_type)
Expand Down Expand Up @@ -1053,11 +1053,11 @@ def query(
product_type = kwargs.get("productType", None)
# remove "product_type" from search args if exists for compatibility with QueryStringSearch methods
kwargs.pop("product_type", None)
sort_by_arg: Optional[SortByList] = self.set_sort_by_arg(kwargs) # type: ignore
sort_by_arg: Optional[SortByList] = self.set_sort_by_arg(kwargs)
sort_by_params: Union[str, Dict[str, List[Dict[str, str]]]] = (
{}
if sort_by_arg is None
else self.transform_sort_by_params_for_search_request(sort_by_arg)
else self.transform_sort_by_arg_for_search_request(sort_by_arg)
)
provider_product_type = self.map_product_type(product_type)
keywords = {k: v for k, v in kwargs.items() if k != "auth" and v is not None}
Expand Down
7 changes: 4 additions & 3 deletions eodag/types/search_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@
from datetime import datetime
from typing import Dict, List, Optional, Tuple, Union, cast

from pydantic import BaseModel, ConfigDict, Field, conint, conlist, field_validator
from annotated_types import MinLen
from pydantic import BaseModel, ConfigDict, Field, conint, field_validator
from shapely import wkt
from shapely.errors import GEOSException
from shapely.geometry import Polygon, shape
from shapely.geometry.base import GEOMETRY_TYPES, BaseGeometry

from eodag.types.bbox import BBox
from eodag.utils import DEFAULT_ITEMS_PER_PAGE, DEFAULT_PAGE
from eodag.utils import DEFAULT_ITEMS_PER_PAGE, DEFAULT_PAGE, Annotated

NumType = Union[float, int]
GeomArgs = Union[List[NumType], Tuple[NumType], Dict[str, NumType], str, BaseGeometry]

PositiveInt = conint(gt=0)
SortByList = conlist(Tuple[str, str], min_length=1)
SortByList = Annotated[List[Tuple[str, str]], MinLen(1)]


class SearchArgs(BaseModel):
Expand Down

0 comments on commit 8040800

Please sign in to comment.