Skip to content

Commit

Permalink
Merge pull request #1392 from Sage-Bionetworks/develop-FDS-1751-mypy-…
Browse files Browse the repository at this point in the history
…schema-utils

Develop fds 1751 mypy schema utils
  • Loading branch information
andrewelamb authored Apr 4, 2024
2 parents 8458565 + eca90a7 commit 41e9fb9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ jobs:
# add here when checked
# poetry run mypy --install-types --non-interactive
# add here when enforced
poetry run mypy --disallow-untyped-defs --install-types --non-interactive schematic/configuration/*.py schematic/exceptions.py schematic/help.py schematic/loader.py schematic/version.py schematic/visualization
poetry run mypy --disallow-untyped-defs --install-types --non-interactive schematic/utils/cli_utils.py schematic/utils/curie_utils.py schematic/utils/df_utils.py schematic/utils/general.py schematic/utils/google_api_utils.py schematic/utils/validate_rules_utils.py schematic/utils/viz_utils.py
poetry run mypy --disallow-untyped-defs --install-types --non-interactive schematic/configuration/*.py schematic/exceptions.py schematic/help.py schematic/loader.py schematic/version.py schematic/visualization schematic/utils/
#----------------------------------------------
# linting
Expand Down
2 changes: 1 addition & 1 deletion schematic/manifest/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ def _create_requests_body(
)
if isinstance(validation_rules, dict):
validation_rules = extract_component_validation_rules(
validation_rules=validation_rules, manifest_component=self.root
validation_rules_dict=validation_rules, manifest_component=self.root
)

# Add regex match validaiton rule to Google Sheets.
Expand Down
2 changes: 1 addition & 1 deletion schematic/models/GE_Helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def build_expectation_suite(
if isinstance(validation_rules, dict):
validation_rules = extract_component_validation_rules(
manifest_component=self.manifest["Component"][0],
validation_rules=validation_rules,
validation_rules_dict=validation_rules,
)
# iterate through all validation rules for an attribute
for rule in validation_rules:
Expand Down
2 changes: 1 addition & 1 deletion schematic/models/validate_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def validate_manifest_rules(
if validation_rules and isinstance(validation_rules, dict):
validation_rules = extract_component_validation_rules(
manifest_component=manifest["Component"][0],
validation_rules=validation_rules,
validation_rules_dict=validation_rules,
)

# Check that attribute rules conform to limits:
Expand Down
44 changes: 23 additions & 21 deletions schematic/utils/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,22 +325,23 @@ def check_for_duplicate_components(
)


def parse_component_validation_rules(validation_rule_string: str) -> dict:
def parse_component_validation_rules(
validation_rule_string: str,
) -> dict[str, list[str]]:
"""
If a validation rule is identified to be formatted as a component validation rule,
parse to a dictionary of components:rules
Args:
validation_rule_string (str): validation rule provided by user.
validation_rule_string (str): validation rule provided by user.
Returns:
dict: validation rules parsed to a dictionary where the key is the component name
(or 'all_other_components') and the value is the parsed validation rule for the
given component.
dict[str, list[str]]: validation rules parsed to a dictionary where the key
is the component name (or 'all_other_components') and the value is the parsed
validation rule for the given component.
"""

component_names = []
validation_rules = []
component_names: list[str] = []
validation_rules: list[list[str]] = []

component_rules = validation_rule_string.split(COMPONENT_RULES_DELIMITER)
# Extract component rules, per component
Expand Down Expand Up @@ -421,40 +422,41 @@ def parse_validation_rules(validation_rules: Union[list, dict]) -> Union[list, d


def extract_component_validation_rules(
manifest_component: str, validation_rules: dict[str, list]
) -> list:
manifest_component: str, validation_rules_dict: dict[str, Union[list, str]]
) -> list[Union[str, list]]:
"""
Parse a component validation rule dictionary to pull out the rule (if any) for a given manifest
Args:
manifest_component, str: Component label, pulled from the manifest directly
validation_rules, dict[str, list[Union[list,str]]: Validation rules dictionary, where keys
are the manifest component label, and the value is a parsed set of validation rules.
validation_rules_dict, dict[str, list[Union[list,str]]: Validation rules dictionary,
where keys are the manifest component label, and the value is a parsed set of
validation rules.
Returns:
validation_rules, list[str]: rule for the provided manifest component if one is available,
if a validation rule is not specified for a given component but "all_other_components"
is specified (as a key), then pull that one, otherwise return an empty list.
"""
manifest_component_rule = validation_rules.get(manifest_component)
all_component_rules = validation_rules.get("all_other_components")
manifest_component_rule = validation_rules_dict.get(manifest_component)
all_component_rules = validation_rules_dict.get("all_other_components")

# Capture situation where manifest_component rule is an empty string
if manifest_component_rule is not None:
if isinstance(manifest_component_rule, str):
if manifest_component_rule == "":
validation_rules = []
validation_rules_list: list[Union[str, list]] = []
else:
validation_rules = [manifest_component_rule]
validation_rules_list = [manifest_component_rule]
elif isinstance(manifest_component_rule, list):
validation_rules = manifest_component_rule
validation_rules_list = manifest_component_rule
elif all_component_rules:
if isinstance(all_component_rules, str):
validation_rules = [all_component_rules]
validation_rules_list = [all_component_rules]
elif isinstance(all_component_rules, list):
validation_rules = all_component_rules
validation_rules_list = all_component_rules
else:
validation_rules = []
return validation_rules
validation_rules_list = []
return validation_rules_list


def export_schema(schema: dict, file_path: str) -> None:
Expand Down

0 comments on commit 41e9fb9

Please sign in to comment.