Skip to content

Commit

Permalink
Simplify overly complex types
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoinkinen committed May 25, 2023
1 parent 33cdcf2 commit ab9e815
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 69 deletions.
2 changes: 1 addition & 1 deletion annif/corpus/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def from_string(
@staticmethod
def _parse_line(
line: str,
) -> Union[Tuple[None, None], Tuple[str, str], Tuple[None, str]]:
) -> Tuple[Optional[str], Optional[str]]:
uri = label = None
vals = line.split("\t")
for val in vals:
Expand Down
10 changes: 2 additions & 8 deletions annif/openapi/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import logging
from typing import Dict, List, Optional, Union
from typing import Dict, List, Union

import jsonschema
from connexion import decorators
Expand All @@ -21,13 +21,7 @@ def __init__(self, *args, **kwargs) -> None:

def validate_schema(
self,
data: Union[
List[Dict[str, Union[List[Dict[str, str]], str]]],
List[Dict[str, Optional[List[bool]]]],
Dict[str, List],
Dict[str, str],
Dict[str, List[Dict[str, str]]],
],
data: Union[List, Dict],
url: str,
) -> None:
"""Validate the request body against the schema."""
Expand Down
44 changes: 8 additions & 36 deletions annif/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import importlib
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union

import connexion

Expand Down Expand Up @@ -74,7 +74,7 @@ def list_projects() -> (

def show_project(
project_id: str,
) -> Union[Dict[str, Optional[Union[str, Dict[str, str], bool]]], ConnexionResponse]:
) -> Union[Dict, ConnexionResponse]:
"""return a single project formatted according to OpenAPI spec"""

try:
Expand All @@ -98,13 +98,7 @@ def _suggestion_to_dict(

def _hit_sets_to_list(
hit_sets: SuggestionResults, subjects: SubjectIndex, lang: str
) -> List[
Union[
Dict[str, List],
Dict[str, List[Dict[str, Union[str, float]]]],
Dict[str, List[Dict[str, Optional[Union[str, float]]]]],
]
]:
) -> List[Dict[str, List]]:
return [
{"results": [_suggestion_to_dict(hit, subjects, lang) for hit in hits]}
for hits in hit_sets
Expand All @@ -114,8 +108,6 @@ def _hit_sets_to_list(
def _is_error(
result: Union[
List[Dict[str, List]],
List[Dict[str, List[Dict[str, Optional[Union[str, float]]]]]],
List[Dict[str, List[Dict[str, Union[str, float]]]]],
ConnexionResponse,
]
) -> bool:
Expand All @@ -127,12 +119,7 @@ def _is_error(

def suggest(
project_id: str, body: Dict[str, Union[float, str]]
) -> Union[
Dict[str, List],
Dict[str, List[Dict[str, Optional[Union[str, float]]]]],
ConnexionResponse,
Dict[str, List[Dict[str, Union[str, float]]]],
]:
) -> Union[Dict[str, List], ConnexionResponse]:
"""suggest subjects for the given text and return a dict with results
formatted according to OpenAPI spec"""

Expand All @@ -151,12 +138,7 @@ def suggest_batch(
project_id: str,
body: Dict[str, Union[List, List[Dict[str, str]]]],
**query_parameters,
) -> Union[
List[Dict[str, None]],
List[Dict[str, Optional[List[Dict[str, Optional[Union[str, float]]]]]]],
List[Dict[str, Union[List[Dict[str, Optional[Union[str, float]]]], str]]],
ConnexionResponse,
]:
) -> Union[List[Dict[str, Any]], ConnexionResponse]:
"""suggest subjects for the given documents and return a list of dicts with results
formatted according to OpenAPI spec"""

Expand All @@ -174,12 +156,7 @@ def _suggest(
project_id: str,
documents: List[Dict[str, str]],
parameters: Dict[str, Union[float, str]],
) -> Union[
List[Dict[str, List]],
List[Dict[str, List[Dict[str, Optional[Union[str, float]]]]]],
List[Dict[str, List[Dict[str, Union[str, float]]]]],
ConnexionResponse,
]:
) -> Union[List[Dict[str, List]], ConnexionResponse]:
corpus = _documents_to_corpus(documents, subject_index=None)
try:
project = annif.registry.get_project(project_id, min_access=Access.hidden)
Expand All @@ -206,7 +183,7 @@ def _suggest(


def _documents_to_corpus(
documents: List[Union[Dict[str, str], Dict[str, Union[List[Dict[str, str]], str]]]],
documents: List[Dict[str, Any]],
subject_index: Optional[SubjectIndex],
) -> annif.corpus.document.DocumentList:
if subject_index is not None:
Expand All @@ -229,12 +206,7 @@ def _documents_to_corpus(

def learn(
project_id: str,
body: List[
Union[
Dict[str, Union[List[Dict[str, str]], str]],
Dict[str, Optional[List[bool]]],
]
],
body: List[Dict[str, Any]],
) -> Union[ConnexionResponse, Tuple[None, int]]:
"""learn from documents and return an empty 204 response if succesful"""

Expand Down
11 changes: 2 additions & 9 deletions annif/transform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple

import annif
from annif.exception import ConfigurationException
Expand All @@ -17,14 +17,7 @@

def parse_specs(
transform_specs: str,
) -> List[
Union[
Tuple[str, List, Dict[Any, Any]],
Tuple[str, List[str], Dict[str, str]],
Tuple[str, List[str], Dict[Any, Any]],
Tuple[str, List, Dict[str, str]],
]
]:
) -> List[Tuple[str, List, Dict]]:
"""Parse a transformation specification into a list of tuples, e.g.
'transf_1(x),transf_2(y=42),transf_3' is parsed to
[(transf_1, [x], {}), (transf_2, [], {y: 42}), (transf_3, [], {})]."""
Expand Down
18 changes: 3 additions & 15 deletions annif/transform/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import abc
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Type

from annif.corpus import TransformingDocumentCorpus
from annif.exception import ConfigurationException
Expand Down Expand Up @@ -43,13 +43,7 @@ class TransformChain:
def __init__(
self,
transform_classes: List[Type[BaseTransform]],
args: List[
Union[
Tuple[List, Dict[str, str]],
Tuple[List[str], Dict[Any, Any]],
Tuple[List, Dict[Any, Any]],
]
],
args: List[Tuple[List, Dict]],
project: Optional[AnnifProject],
) -> None:
self.project = project
Expand All @@ -58,13 +52,7 @@ def __init__(
def _init_transforms(
self,
transform_classes: List[Type[BaseTransform]],
args: List[
Union[
Tuple[List, Dict[str, str]],
Tuple[List[str], Dict[Any, Any]],
Tuple[List, Dict[Any, Any]],
]
],
args: List[Tuple[List, Dict]],
) -> List[Type[BaseTransform]]:
transforms = []
for trans, (posargs, kwargs) in zip(transform_classes, args):
Expand Down

0 comments on commit ab9e815

Please sign in to comment.