Skip to content

Commit

Permalink
Merge pull request #415 from bento-platform/fix/type-hinting
Browse files Browse the repository at this point in the history
fix: extension schema dict type hinting & export decl
  • Loading branch information
davidlougheed committed Jun 21, 2023
2 parents 8fadb60 + 40f038f commit 98df4cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
10 changes: 7 additions & 3 deletions chord_metadata_service/chord/ingest/phenopackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from chord_metadata_service.phenopackets.schemas import PHENOPACKET_SCHEMA
from chord_metadata_service.patients.values import KaryotypicSex
from chord_metadata_service.restapi.schema_utils import patch_project_schemas
from chord_metadata_service.restapi.types import ExtensionSchemaDict
from chord_metadata_service.restapi.utils import iso_duration_to_years

from .exceptions import IngestError
Expand All @@ -19,7 +20,7 @@
from .schema import schema_validation
from .utils import get_output_or_raise, map_if_list, query_and_check_nulls, workflow_file_output_to_path

from typing import Any, Optional, Union
from typing import Any, Dict, Iterable, Optional, Union


def get_or_create_phenotypic_feature(pf: dict) -> pm.PhenotypicFeature:
Expand Down Expand Up @@ -310,14 +311,17 @@ def ingest_phenopacket_workflow(workflow_outputs, table_id) -> Union[list[pm.Phe
json_data = json.load(jf)

project_id = Project.objects.get(datasets__table_ownership=table_id)
project_schemas = ProjectJsonSchema.objects.filter(project_id=project_id).values(
project_schemas: Iterable[ExtensionSchemaDict] = ProjectJsonSchema.objects.filter(project_id=project_id).values(
"json_schema",
"required",
"schema_type",
)

# Map with key:schema_type and value:json_schema
extension_schemas = {proj_schema["schema_type"].lower(): proj_schema for proj_schema in project_schemas}
extension_schemas: Dict[str, ExtensionSchemaDict] = {
proj_schema["schema_type"].lower(): proj_schema
for proj_schema in project_schemas
}
json_schema = patch_project_schemas(PHENOPACKET_SCHEMA, extension_schemas)

# First, validate all phenopackets
Expand Down
11 changes: 7 additions & 4 deletions chord_metadata_service/restapi/schema_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from chord_metadata_service.logger import logger
from bento_lib.search import queries as q
from .description_utils import describe_schema
from typing import List, Optional, Dict
from chord_metadata_service.logger import logger
from copy import deepcopy
from typing import List, Optional, Dict

from .description_utils import describe_schema
from .types import ExtensionSchemaDict

__all__ = [
"merge_schema_dictionaries",
Expand All @@ -13,6 +15,7 @@
"tag_ids_and_describe",
"customize_schema",
"schema_list",
"patch_project_schemas",
]


Expand Down Expand Up @@ -151,7 +154,7 @@ def schema_list(schema):
}


def patch_project_schemas(base_schema: dict, extension_schemas: Dict[str, object]) -> dict:
def patch_project_schemas(base_schema: dict, extension_schemas: Dict[str, ExtensionSchemaDict]) -> dict:
if not isinstance(base_schema, dict) or "type" not in base_schema:
return base_schema

Expand Down
11 changes: 11 additions & 0 deletions chord_metadata_service/restapi/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import TypedDict

__all__ = [
"ExtensionSchemaDict",
]


class ExtensionSchemaDict(TypedDict):
json_schema: dict
required: bool
schema_type: str

0 comments on commit 98df4cd

Please sign in to comment.