Skip to content

Commit bbe7cce

Browse files
committed
Update deprecatedFrom validation and tests
1 parent 869458e commit bbe7cce

4 files changed

Lines changed: 87 additions & 11 deletions

File tree

hed/errors/error_types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ class SchemaWarnings:
127127
class SchemaAttributeErrors:
128128
SCHEMA_ATTRIBUTE_INVALID = 'SCHEMA_ATTRIBUTE_INVALID'
129129
SCHEMA_ATTRIBUTE_VALUE_INVALID = 'SCHEMA_ATTRIBUTE_VALUE_INVALID'
130+
SCHEMA_DEPRECATION_ERROR = 'SCHEMA_DEPRECATION_ERROR'
130131
SCHEMA_DEPRECATED_INVALID = "SCHEMA_DEPRECATED_INVALID"
132+
SCHEMA_CHILD_OF_DEPRECATED = "SCHEMA_CHILD_OF_DEPRECATED"
133+
SCHEMA_ATTRIBUTE_VALUE_DEPRECATED = "SCHEMA_ATTRIBUTE_VALUE_DEPRECATED"
131134
SCHEMA_SUGGESTED_TAG_INVALID = "SCHEMA_SUGGESTED_TAG_INVALID"
132135

133136
SCHEMA_UNIT_CLASS_INVALID = "SCHEMA_UNIT_CLASS_INVALID"
@@ -136,7 +139,7 @@ class SchemaAttributeErrors:
136139
SCHEMA_IN_LIBRARY_INVALID = "SCHEMA_IN_LIBRARY_INVALID"
137140

138141
SCHEMA_DEFAULT_UNITS_INVALID = "SCHEMA_DEFAULT_UNITS_INVALID"
139-
SCHEMA_CHILD_OF_DEPRECATED = "SCHEMA_CHILD_OF_DEPRECATED"
142+
SCHEMA_DEFAULT_UNITS_DEPRECATED = "SCHEMA_DEFAULT_UNITS_DEPRECATED"
140143
SCHEMA_CONVERSION_FACTOR_NOT_POSITIVE = "SCHEMA_CONVERSION_FACTOR_NOT_POSITIVE"
141144

142145

hed/errors/schema_error_messages.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,24 @@ def schema_warning_non_placeholder_class(tag_name, invalid_attribute_name):
4949

5050

5151
@hed_error(SchemaAttributeErrors.SCHEMA_DEPRECATED_INVALID,
52-
actual_code=SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_INVALID)
52+
actual_code=SchemaAttributeErrors.SCHEMA_DEPRECATION_ERROR)
5353
def schema_error_SCHEMA_DEPRECATED_INVALID(tag_name, invalid_deprecated_version):
5454
return f"'{tag_name}' has invalid or unknown value in attribute deprecatedFrom: '{invalid_deprecated_version}'."
5555

5656

5757
@hed_error(SchemaAttributeErrors.SCHEMA_CHILD_OF_DEPRECATED,
58-
actual_code=SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_INVALID)
58+
actual_code=SchemaAttributeErrors.SCHEMA_DEPRECATION_ERROR)
5959
def schema_error_SCHEMA_CHILD_OF_DEPRECATED(deprecated_tag, non_deprecated_child):
6060
return f"Deprecated tag '{deprecated_tag}' has a child that is not deprecated: '{non_deprecated_child}'."
6161

6262

63+
@hed_error(SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_DEPRECATED,
64+
actual_code=SchemaAttributeErrors.SCHEMA_DEPRECATION_ERROR)
65+
def schema_error_SCHEMA_ATTRIBUTE_VALUE_DEPRECATED(tag, deprecated_suggestion, attribute_name):
66+
return (f"Tag '{tag}' {attribute_name} uses '{deprecated_suggestion}' which has been deprecated "
67+
f"and an alternative method of tagging should be used.")
68+
69+
6370
@hed_error(SchemaAttributeErrors.SCHEMA_SUGGESTED_TAG_INVALID,
6471
actual_code=SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_INVALID)
6572
def schema_error_SCHEMA_SUGGESTED_TAG_INVALID(suggestedTag, invalidSuggestedTag, attribute_name):
@@ -85,6 +92,12 @@ def schema_error_SCHEMA_DEFAULT_UNITS_INVALID(tag, bad_unit, valid_units):
8592
return f"Tag '{tag}' has an invalid defaultUnit '{bad_unit}'. Valid units are: '{valid_units}'."
8693

8794

95+
@hed_error(SchemaAttributeErrors.SCHEMA_DEFAULT_UNITS_DEPRECATED,
96+
actual_code=SchemaAttributeErrors.SCHEMA_DEPRECATION_ERROR)
97+
def schema_error_SCHEMA_DEFAULT_UNITS_DEPRECATED(unit_class, bad_unit):
98+
return f"Unit class '{unit_class}' defaultUnit '{bad_unit}' is deprecated. Please find an alternative default."
99+
100+
88101
@hed_error(SchemaAttributeErrors.SCHEMA_CONVERSION_FACTOR_NOT_POSITIVE,
89102
actual_code=SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_INVALID)
90103
def schema_error_SCHEMA_CONVERSION_FACTOR_NOT_POSITIVE(tag, conversion_factor):

hed/schema/schema_attribute_validators.py

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from hed.errors.error_types import SchemaWarnings, ValidationErrors, SchemaAttributeErrors
1515
from hed.errors.error_reporter import ErrorHandler
1616
from hed.schema.hed_cache import get_hed_versions
17-
from hed.schema.hed_schema_constants import HedKey, character_types
17+
from hed.schema.hed_schema_constants import HedKey, character_types, HedSectionKey
1818
from hed.schema.schema_validation_util import schema_version_for_library
1919
from semantic_version import Version
2020

@@ -39,6 +39,36 @@ def tag_is_placeholder_check(hed_schema, tag_entry, attribute_name):
3939
return issues
4040

4141

42+
def attribute_is_deprecated(hed_schema, tag_entry, attribute_name):
43+
""" Check if the attribute is deprecated.
44+
45+
does not check value.
46+
47+
Parameters:
48+
hed_schema (HedSchema): The schema to use for validation
49+
tag_entry (HedSchemaEntry): The schema entry for this tag.
50+
attribute_name (str): The name of this attribute
51+
52+
Returns:
53+
list: A list of issues. Each issue is a dictionary.
54+
"""
55+
issues = []
56+
# Attributes has to check properties
57+
section_key = HedSectionKey.Attributes
58+
if tag_entry.section_key == HedSectionKey.Attributes:
59+
section_key = HedSectionKey.Properties
60+
61+
attribute_entry = hed_schema.get_tag_entry(attribute_name, section_key)
62+
if (attribute_entry and attribute_entry.has_attribute(HedKey.DeprecatedFrom)
63+
and not tag_entry.has_attribute(HedKey.DeprecatedFrom)):
64+
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_DEPRECATED,
65+
tag_entry.name,
66+
attribute_entry.name,
67+
attribute_name)
68+
69+
return issues
70+
71+
4272
# todo: This needs to be refactored, these next several functions are near identical
4373
def tag_exists_check(hed_schema, tag_entry, attribute_name):
4474
""" Check if the list of possible tags exists in the schema.
@@ -56,11 +86,18 @@ def tag_exists_check(hed_schema, tag_entry, attribute_name):
5686
possible_tags = tag_entry.attributes.get(attribute_name, "")
5787
split_tags = possible_tags.split(",")
5888
for org_tag in split_tags:
59-
if org_tag and org_tag not in hed_schema.tags:
89+
org_entry = hed_schema.tags.get(org_tag)
90+
if org_tag and not org_entry:
6091
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_SUGGESTED_TAG_INVALID,
6192
tag_entry.name,
6293
org_tag,
6394
attribute_name)
95+
elif (org_entry and org_entry.has_attribute(HedKey.DeprecatedFrom)
96+
and not tag_entry.has_attribute(HedKey.DeprecatedFrom)):
97+
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_DEPRECATED,
98+
tag_entry.name,
99+
org_tag,
100+
attribute_name)
64101

65102
return issues
66103

@@ -70,11 +107,18 @@ def unit_class_exists(hed_schema, tag_entry, attribute_name):
70107
possible_unit_classes = tag_entry.attributes.get(attribute_name, "")
71108
split_tags = possible_unit_classes.split(",")
72109
for org_tag in split_tags:
73-
if org_tag and org_tag not in hed_schema.unit_classes:
110+
unit_class_entry = hed_schema.unit_classes.get(org_tag)
111+
if org_tag and not unit_class_entry:
74112
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_UNIT_CLASS_INVALID,
75113
tag_entry.name,
76114
org_tag,
77115
attribute_name)
116+
elif (unit_class_entry and unit_class_entry.has_attribute(HedKey.DeprecatedFrom)
117+
and not tag_entry.has_attribute(HedKey.DeprecatedFrom)):
118+
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_DEPRECATED,
119+
tag_entry.name,
120+
org_tag,
121+
attribute_name)
78122

79123
return issues
80124

@@ -83,24 +127,38 @@ def value_class_exists(hed_schema, tag_entry, attribute_name):
83127
issues = []
84128
possible_value_classes = tag_entry.attributes.get(attribute_name, "")
85129
split_tags = possible_value_classes.split(",")
130+
86131
for org_tag in split_tags:
87-
if org_tag and org_tag not in hed_schema.value_classes:
132+
value_class_entry = hed_schema.value_classes.get(org_tag)
133+
if org_tag and not value_class_entry:
88134
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_VALUE_CLASS_INVALID,
89135
tag_entry.name,
90136
org_tag,
91137
attribute_name)
138+
elif (value_class_entry and value_class_entry.has_attribute(HedKey.DeprecatedFrom)
139+
and not tag_entry.has_attribute(HedKey.DeprecatedFrom)):
140+
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_ATTRIBUTE_VALUE_DEPRECATED,
141+
tag_entry.name,
142+
org_tag,
143+
attribute_name)
92144

93145
return issues
94146

95147

96148
def unit_exists(hed_schema, tag_entry, attribute_name):
97149
issues = []
98-
default_unit = tag_entry.attributes.get(attribute_name, "")
99-
if default_unit and default_unit not in tag_entry.derivative_units:
150+
unit = tag_entry.attributes.get(attribute_name, "")
151+
unit_entry = tag_entry.derivative_units.get(unit)
152+
if unit and not unit_entry:
100153
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_DEFAULT_UNITS_INVALID,
101154
tag_entry.name,
102-
default_unit,
155+
unit,
103156
tag_entry.units)
157+
elif (unit_entry and unit_entry.has_attribute(HedKey.DeprecatedFrom)
158+
and not tag_entry.has_attribute(HedKey.DeprecatedFrom)):
159+
issues += ErrorHandler.format_error(SchemaAttributeErrors.SCHEMA_DEFAULT_UNITS_DEPRECATED,
160+
tag_entry.name,
161+
unit_entry.name)
104162

105163
return issues
106164

hed/schema/schema_compliance.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ def check_attributes(self):
8686
for tag_entry in self.hed_schema[section_key].values():
8787
self.error_handler.push_error_context(ErrorContext.SCHEMA_TAG, tag_entry.name)
8888
for attribute_name in tag_entry.attributes:
89-
validators = self.attribute_validators.get(attribute_name, [])
89+
# Always check deprecated
90+
validators = self.attribute_validators.get(attribute_name, []) \
91+
+ [schema_attribute_validators.attribute_is_deprecated]
9092
for validator in validators:
9193
self.error_handler.push_error_context(ErrorContext.SCHEMA_ATTRIBUTE, attribute_name)
9294
new_issues = validator(self.hed_schema, tag_entry, attribute_name)

0 commit comments

Comments
 (0)