diff --git a/src/metakb/cli.py b/src/metakb/cli.py index 3314c0a0..51e89894 100644 --- a/src/metakb/cli.py +++ b/src/metakb/cli.py @@ -7,7 +7,7 @@ from os import environ from pathlib import Path from timeit import default_timer as timer -from typing import Optional +from typing import Callable, Optional from zipfile import ZipFile import asyncclick as click @@ -228,7 +228,7 @@ def _load_normalizers_db() -> None: def _update_normalizer_db( name: str, - update_normalizer_db_fn: callable, + update_normalizer_db_fn: Callable, ) -> None: """Update Normalizer DynamoDB database. diff --git a/src/metakb/harvesters/base.py b/src/metakb/harvesters/base.py index 9f9747df..70649ba8 100644 --- a/src/metakb/harvesters/base.py +++ b/src/metakb/harvesters/base.py @@ -17,7 +17,6 @@ def harvest(self) -> bool: any manner, but must be retrievable by :method:`iterate_records`. :return: `True` if operation was successful, `False` otherwise. - :rtype: bool """ raise NotImplementedError @@ -26,8 +25,8 @@ def create_json( ) -> bool: """Create composite and individual JSON for harvested data. - :param Dict items: item types keyed to Lists of values - :param Optional[str] filename: custom filename for composite document + :param items: item types keyed to Lists of values + :param filename: custom filename for composite document :return: `True` if JSON creation was successful. `False` otherwise. """ composite_dict = {} diff --git a/src/metakb/harvesters/civic.py b/src/metakb/harvesters/civic.py index d5c0da4f..6100aba1 100644 --- a/src/metakb/harvesters/civic.py +++ b/src/metakb/harvesters/civic.py @@ -1,7 +1,6 @@ """A module for the CIViC harvester.""" import logging -from pathlib import Path -from typing import Dict, List, Optional +from typing import Any, Dict, List, Optional, Union from civicpy import LOCAL_CACHE_PATH from civicpy import civic as civicpy @@ -18,7 +17,7 @@ def __init__( self, update_cache: bool = False, update_from_remote: bool = True, - local_cache_path: Optional[Path] = LOCAL_CACHE_PATH, + local_cache_path: Union[str, bool] = LOCAL_CACHE_PATH, ) -> None: """Initialize CivicHarvester class. @@ -48,7 +47,6 @@ def harvest(self, filename: Optional[str] = None) -> bool: :param filename: File name for composite json :return: `True` if operation was successful, `False` otherwise. - :rtype: bool """ try: self.evidence = self.harvest_evidence() @@ -79,7 +77,7 @@ def harvest(self, filename: Optional[str] = None) -> bool: logger.info("CIViC Harvester was successful.") return True - def harvest_evidence(self) -> List[Dict]: + def harvest_evidence(self) -> List[Union[None, Dict, List]]: """Harvest all CIViC evidence item records. :return: A list of all CIViC evidence item records represented as dicts @@ -87,7 +85,7 @@ def harvest_evidence(self) -> List[Dict]: evidence_items = civicpy.get_all_evidence() return [self._dictify(e) for e in evidence_items] - def harvest_genes(self) -> List[Dict]: + def harvest_genes(self) -> List[Union[None, Dict, List]]: """Harvest all CIViC gene records. :return: A list of all CIViC gene records represented as dicts @@ -95,7 +93,7 @@ def harvest_genes(self) -> List[Dict]: genes = civicpy.get_all_genes() return [self._dictify(g) for g in genes] - def harvest_variants(self) -> List[Dict]: + def harvest_variants(self) -> List[Union[None, Dict, List]]: """Harvest all CIViC variant records. :return: A list of all CIViC variant records represented as dicts @@ -103,7 +101,7 @@ def harvest_variants(self) -> List[Dict]: variants = civicpy.get_all_variants() return [self._dictify(v) for v in variants] - def harvest_molecular_profiles(self) -> List[Dict]: + def harvest_molecular_profiles(self) -> List[Union[None, Dict, List]]: """Harvest all CIViC Molecular Profile records :return: A list of all CIViC molecular profile records represented as dicts @@ -111,7 +109,7 @@ def harvest_molecular_profiles(self) -> List[Dict]: molecular_profiles = civicpy.get_all_molecular_profiles() return [self._dictify(mp) for mp in molecular_profiles] - def harvest_assertions(self) -> List[Dict]: + def harvest_assertions(self) -> List[Union[None, Dict, List]]: """Harvest all CIViC assertion records. :return: A list of all CIViC assertion records represented as dicts @@ -119,7 +117,7 @@ def harvest_assertions(self) -> List[Dict]: assertions = civicpy.get_all_assertions() return [self._dictify(a) for a in assertions] - def _dictify(self, obj: any) -> Dict: + def _dictify(self, obj: Any) -> Union[None, Dict, List]: # noqa: ANN401 """Recursively convert object to dictionary :param obj: Object to convert to dict diff --git a/src/metakb/harvesters/moa.py b/src/metakb/harvesters/moa.py index 1390b0ab..25be3fd9 100644 --- a/src/metakb/harvesters/moa.py +++ b/src/metakb/harvesters/moa.py @@ -1,6 +1,6 @@ """A module for the Molecular Oncology Almanac harvester""" import logging -from typing import Dict, List, Optional +from typing import Dict, List, Optional, Tuple import requests import requests_cache @@ -17,9 +17,8 @@ def harvest(self, filename: Optional[str] = None) -> bool: """Retrieve and store sources, variants, and assertions records from MOAlmanac in composite and individual JSON files. - :param Optional[str] filename: File name for composite json + :param filename: File name for composite json :return: True if successfully retrieved, False otherwise - :rtype: bool """ try: assertion_resp = self._get_all_assertions() @@ -62,9 +61,8 @@ def _harvest_genes() -> List[Dict]: def _harvest_sources(self, assertion_resp: List[Dict]) -> List[Dict]: """Harvest all MOA sources - :param List[Dict] assertion_resp: A list of MOA assertion records + :param assertion_resp: A list of MOA assertion records :return: A list of sources - :rtype: list """ sources = [] @@ -76,11 +74,10 @@ def _harvest_sources(self, assertion_resp: List[Dict]) -> List[Dict]: return sources - def harvest_variants(self) -> List[Dict]: + def harvest_variants(self) -> Tuple[List[Dict], List[Dict]]: """Harvest all MOA variants :return: A list of variants - :rtype: list """ variants_list = self._get_all_variants() variants = [] @@ -96,10 +93,9 @@ def harvest_assertions( ) -> List[Dict]: """Harvest all MOA assertions - :param List[Dict] assertion_resp: A list of MOA assertion records - :param List[Dict] variants_list: A list of MOA variant records + :param assertion_resp: A list of MOA assertion records + :param variants_list: A list of MOA variant records :return: A list of assertions - :rtype: list """ assertions = [] for assertion in assertion_resp: @@ -129,9 +125,8 @@ def _get_all_variants(self) -> List[Dict]: def _source_item(self, source: Dict) -> Dict: """Harvest an individual MOA source of evidence - :param Dict source: source record of each assertion record + :param source: source record of each assertion record :return: a dictionary containing MOA source of evidence data - :rtype: dict """ return { "id": source["source_id"], @@ -146,9 +141,8 @@ def _source_item(self, source: Dict) -> Dict: def _harvest_variant(self, variant: Dict) -> Dict: """Harvest an individual MOA variant record. - :param Dict variant: A MOA variant record + :param variant: A MOA variant record :return: A dictionary containing MOA variant data - :rtype: dict """ variant_record = {"id": variant["feature_id"]} @@ -160,10 +154,9 @@ def _harvest_variant(self, variant: Dict) -> Dict: def _harvest_assertion(self, assertion: Dict, variants_list: List[Dict]) -> Dict: """Harvest an individual MOA assertion record - :param Dict assertion: a MOA assertion record - :param List[Dict] variants_list: a list of MOA variant records + :param assertion: a MOA assertion record + :param variants_list: a list of MOA variant records :return: A dictionary containing MOA assertion data - :rtype: dict """ assertion_record = { "id": assertion["assertion_id"], @@ -197,12 +190,9 @@ def _harvest_assertion(self, assertion: Dict, variants_list: List[Dict]) -> Dict def _get_therapy(self, resistance: bool, sensitivity: bool) -> Optional[str]: """Get therapy response data. - :param bool resistance: `True` if Therapy Resistance. - `False` if not Therapy Resistance - :param bool sensitivity: `True` if Therapy Sensitivity. - `False` if not Therapy Sensitivity + :param resistance: `True` if Therapy Resistance. `False` if not Therapy Resistance + :param sensitivity: `True` if Therapy Sensitivity. `False` if not Therapy Sensitivity :return: whether the therapy response is resistance or sensitivity - :rtype: str """ if resistance: return "resistance" @@ -213,9 +203,8 @@ def _get_therapy(self, resistance: bool, sensitivity: bool) -> Optional[str]: def _get_feature(self, v: Dict) -> Dict: """Get feature name from the harvested variants - :param Dict v: harvested MOA variant + :param v: harvested MOA variant :return: feature name same format as displayed in moalmanac.org - :rtype: dict """ feature_type = v["feature_type"] if feature_type == "rearrangement":