-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update type hints for prefix map merging #454
Changes from 3 commits
fe4a858
a73ffb6
813f218
0aa53b6
2a4eb0f
b39b54b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
PREFIX_MAP_MODE_MERGED, | ||
PREFIX_MAP_MODE_METADATA_ONLY, | ||
PREFIX_MAP_MODE_SSSOM_DEFAULT_ONLY, | ||
MergeMode, | ||
SchemaValidationType, | ||
) | ||
from .context import get_converter | ||
|
@@ -59,7 +60,7 @@ def parse_file( | |
output: TextIO, | ||
input_format: Optional[str] = None, | ||
metadata_path: Optional[str] = None, | ||
prefix_map_mode: Optional[str] = None, | ||
merge_mode: Optional[MergeMode] = None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All borders have fallen now, but renaming parameters to central methods like this is really annoying for users, but the change makes sense, so lets just break and mend it all now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a feeling this one is unused - it is only part of a method that should itself be private. In fact, I would rather remove this option completely and have it automatically chain on all instances. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reverted this and removed the renames |
||
clean_prefixes: bool = True, | ||
strict_clean_prefixes: bool = True, | ||
embedded_mode: bool = True, | ||
|
@@ -72,17 +73,15 @@ def parse_file( | |
:param input_format: The string denoting the input format. | ||
: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 | ||
:param merge_mode: Defines whether the prefix map in the metadata should be extended or replaced with | ||
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). | ||
:param mapping_predicate_filter: Optional list of mapping predicates or filepath containing the same. | ||
""" | ||
raise_for_bad_path(input_path) | ||
metadata = get_metadata_and_prefix_map( | ||
metadata_path=metadata_path, prefix_map_mode=prefix_map_mode | ||
) | ||
metadata = get_metadata_and_prefix_map(metadata_path=metadata_path, merge_mode=merge_mode) | ||
parse_func = get_parsing_function(input_format, input_path) | ||
mapping_predicates = None | ||
# Get list of predicates of interest. | ||
|
@@ -134,13 +133,15 @@ 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, | ||
*, | ||
merge_mode: Optional[MergeMode] = None, | ||
) -> Metadata: | ||
""" | ||
Load SSSOM metadata from a file, and then augments it with default prefixes. | ||
|
||
:param metadata_path: The metadata file in YAML format | ||
:param prefix_map_mode: one of metadata_only, sssom_default_only, merged | ||
:param merge_mode: one of metadata_only, sssom_default_only, merged | ||
:return: a prefix map dictionary and a metadata object dictionary | ||
""" | ||
if metadata_path is None: | ||
|
@@ -159,20 +160,20 @@ def get_metadata_and_prefix_map( | |
else: | ||
prefix_map = {} | ||
converter = Converter.from_prefix_map(prefix_map) | ||
converter = _merge_converter(converter, prefix_map_mode=prefix_map_mode) | ||
converter = _merge_converter(converter, mode=merge_mode) | ||
cthoyt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return Metadata(converter=converter, metadata=metadata) | ||
|
||
|
||
def _merge_converter(converter: Converter, prefix_map_mode: str = None) -> Converter: | ||
def _merge_converter(converter: Converter, 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: | ||
if mode is None or mode == PREFIX_MAP_MODE_METADATA_ONLY: | ||
return converter | ||
if prefix_map_mode == PREFIX_MAP_MODE_SSSOM_DEFAULT_ONLY: | ||
if mode == PREFIX_MAP_MODE_SSSOM_DEFAULT_ONLY: | ||
return get_converter() | ||
if prefix_map_mode == PREFIX_MAP_MODE_MERGED: | ||
if mode == PREFIX_MAP_MODE_MERGED: | ||
return curies.chain([converter, get_converter()]) | ||
raise ValueError(f"Invalid prefix map mode: {prefix_map_mode}") | ||
raise ValueError(f"Invalid prefix map mode: {mode}") | ||
|
||
|
||
def get_list_of_predicate_iri(predicate_filter: tuple, converter: Converter) -> list: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never seen this syntax. What does this do, when considering line 40? Looks redundant at first glance (twice the same assignment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes checking that one of the constants is used properly is checked by type checking. It further builds the string enumeration into the automatic documentation generation