@@ -48,37 +48,48 @@ def _get_default_value_class_validators(self):
4848
4949 return validator_dict
5050
51- def check_tag_unit_class_units_are_valid (self , original_tag , validate_text , report_as = None , error_code = None ) -> list [dict ]:
51+ def check_tag_unit_class_units_are_valid (
52+ self , original_tag , validate_text , report_as = None , error_code = None , allow_placeholders = True
53+ ) -> list [dict ]:
5254 """Report incorrect unit class or units.
5355
5456 Parameters:
5557 original_tag (HedTag): The original tag that is used to report the error.
5658 validate_text (str): The text to validate.
5759 report_as (HedTag): Report errors as coming from this tag, rather than original_tag.
5860 error_code (str): Override error codes.
61+ allow_placeholders (bool): Whether placeholders are allowed (affects value class validation for "#")
5962
6063 Returns:
6164 list: Validation issues. Each issue is a dictionary.
6265 """
66+ if not original_tag .is_unit_class_tag ():
67+ return []
68+
6369 validation_issues = []
64- if original_tag .is_unit_class_tag ():
65-
66- # Check the units first
67- stripped_value , units = original_tag .get_stripped_unit_value (validate_text )
68- if not stripped_value :
69- validation_issues += self ._report_bad_units (original_tag , report_as )
70- return validation_issues
71-
72- # Check the value classes
73- validation_issues += self ._check_value_class (original_tag , stripped_value , report_as )
74- if validation_issues :
75- return validation_issues
76-
77- # We don't want to give this overall error twice
78- if error_code and validation_issues and not any (error_code == issue ["code" ] for issue in validation_issues ):
79- new_issue = validation_issues [0 ].copy ()
80- new_issue ["code" ] = error_code
81- validation_issues += [new_issue ]
70+ # Check the units first
71+ stripped_value , units = original_tag .get_stripped_unit_value (validate_text )
72+ if not stripped_value :
73+ # stripped_value is None only when invalid units are present
74+ validation_issues += self ._report_bad_units (original_tag , report_as )
75+ return validation_issues
76+
77+ # If value is a placeholder (#) and placeholders are allowed, it's valid
78+ # Invalid units would have been caught above (stripped_value would be None)
79+ if stripped_value == "#" and allow_placeholders :
80+ return validation_issues
81+
82+ # Check the value classes
83+ # If placeholders are NOT allowed, "#" will fail value class validation (e.g., not a valid number)
84+ validation_issues += self ._check_value_class (original_tag , stripped_value , report_as )
85+ if validation_issues :
86+ return validation_issues
87+
88+ # We don't want to give this overall error twice
89+ if error_code and validation_issues and not any (error_code == issue ["code" ] for issue in validation_issues ):
90+ new_issue = validation_issues [0 ].copy ()
91+ new_issue ["code" ] = error_code
92+ validation_issues += [new_issue ]
8293
8394 return validation_issues
8495
0 commit comments