Skip to content

Commit

Permalink
Update type hints for prefix map merging (#454)
Browse files Browse the repository at this point in the history
This PR does the following:

1. Creates a literal type for the 3 different prefix map merge modes
2. Updates type hints based on that
  • Loading branch information
cthoyt authored Nov 6, 2023
1 parent ad70a83 commit 77b4263
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/sssom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sys
from operator import itemgetter
from pathlib import Path
from typing import Any, Callable, List, Optional, TextIO, Tuple
from typing import Any, Callable, List, Optional, TextIO, Tuple, get_args

import click
import curies
Expand All @@ -28,7 +28,7 @@

from sssom.constants import (
DEFAULT_VALIDATION_TYPES,
PREFIX_MAP_MODES,
MergeMode,
SchemaValidationType,
_get_sssom_schema_object,
)
Expand Down Expand Up @@ -172,9 +172,9 @@ def convert(input: str, output: TextIO, output_format: str):
default="metadata_only",
show_default=True,
required=True,
type=click.Choice(PREFIX_MAP_MODES, case_sensitive=False),
type=click.Choice(get_args(MergeMode), case_sensitive=False),
help="Defines whether the prefix map in the metadata should be extended or replaced with "
"the SSSOM default prefix map. Must be one of metadata_only, sssom_default_only, merged",
"the SSSOM default prefix map.",
)
@click.option(
"-p",
Expand Down Expand Up @@ -205,7 +205,7 @@ def parse(
input: str,
input_format: str,
metadata: str,
prefix_map_mode: str,
prefix_map_mode: MergeMode,
clean_prefixes: bool,
strict_clean_prefixes: bool,
output: TextIO,
Expand Down
14 changes: 5 additions & 9 deletions src/sssom/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import uuid
from enum import Enum
from functools import lru_cache
from typing import Any, Dict, List
from typing import Any, Dict, List, Literal

import pkg_resources
import yaml
Expand Down Expand Up @@ -38,14 +38,10 @@
]

UNKNOWN_IRI = "http://w3id.org/sssom/unknown_prefix/"
PREFIX_MAP_MODE_METADATA_ONLY = "metadata_only"
PREFIX_MAP_MODE_SSSOM_DEFAULT_ONLY = "sssom_default_only"
PREFIX_MAP_MODE_MERGED = "merged"
PREFIX_MAP_MODES = [
PREFIX_MAP_MODE_METADATA_ONLY,
PREFIX_MAP_MODE_SSSOM_DEFAULT_ONLY,
PREFIX_MAP_MODE_MERGED,
]
MergeMode = Literal["metadata_only", "sssom_default_only", "merged"]
PREFIX_MAP_MODE_METADATA_ONLY: MergeMode = "metadata_only"
PREFIX_MAP_MODE_SSSOM_DEFAULT_ONLY: MergeMode = "sssom_default_only"
PREFIX_MAP_MODE_MERGED: MergeMode = "merged"
ENTITY_REFERENCE = "EntityReference"

# Slot Constants
Expand Down
12 changes: 8 additions & 4 deletions src/sssom/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
PREFIX_MAP_MODE_MERGED,
PREFIX_MAP_MODE_METADATA_ONLY,
PREFIX_MAP_MODE_SSSOM_DEFAULT_ONLY,
MergeMode,
MetadataType,
SchemaValidationType,
get_default_metadata,
Expand Down Expand Up @@ -52,9 +53,10 @@ def convert_file(
def parse_file(
input_path: str,
output: TextIO,
*,
input_format: Optional[str] = None,
metadata_path: Optional[str] = None,
prefix_map_mode: Optional[str] = None,
prefix_map_mode: Optional[MergeMode] = None,
clean_prefixes: bool = True,
strict_clean_prefixes: bool = True,
embedded_mode: bool = True,
Expand All @@ -68,7 +70,7 @@ def parse_file(
:param metadata_path: The path to a file containing the sssom metadata (including prefix_map)
to be used during parse.
:param prefix_map_mode: Defines whether the prefix map in the metadata should be extended or replaced with
the SSSOM default prefix map. Must be one of metadata_only, sssom_default_only, merged
the SSSOM default prefix map derived from the :mod:`bioregistry`.
:param clean_prefixes: If True (default), records with unknown prefixes are removed from the SSSOM file.
:param strict_clean_prefixes: If True (default), clean_prefixes() will be in strict mode.
:param embedded_mode:If True (default), the dataframe and metadata are exported in one file (tsv), else two separate files (tsv and yaml).
Expand Down Expand Up @@ -129,7 +131,7 @@ def split_file(input_path: str, output_directory: Union[str, Path]) -> None:


def get_metadata_and_prefix_map(
metadata_path: Union[None, str, Path] = None, *, prefix_map_mode: Optional[str] = None
metadata_path: Union[None, str, Path] = None, *, prefix_map_mode: Optional[MergeMode] = None
) -> Tuple[Converter, MetadataType]:
"""
Load SSSOM metadata from a YAML file, and then augment it with default prefixes.
Expand All @@ -150,7 +152,9 @@ def get_metadata_and_prefix_map(
return converter, metadata


def _merge_converter(converter: Converter, prefix_map_mode: str = None) -> Converter:
def _merge_converter(
converter: Converter, prefix_map_mode: Optional[MergeMode] = None
) -> Converter:
"""Merge the metadata's converter with the default converter."""
if prefix_map_mode is None or prefix_map_mode == PREFIX_MAP_MODE_METADATA_ONLY:
return converter
Expand Down

0 comments on commit 77b4263

Please sign in to comment.