-
Notifications
You must be signed in to change notification settings - Fork 178
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
refactor(api): more clear error messages for type validation when creating runtime parameters #14903
refactor(api): more clear error messages for type validation when creating runtime parameters #14903
Changes from 1 commit
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 | ||||
---|---|---|---|---|---|---|
|
@@ -29,6 +29,10 @@ def validate_variable_name_unique( | |||||
|
||||||
def ensure_display_name(display_name: str) -> str: | ||||||
"""Validate display name is within the character limit.""" | ||||||
if not isinstance(display_name, str): | ||||||
raise ParameterNameError( | ||||||
f"Display name must be a string and less than {DISPLAY_NAME_MAX_LEN} characters." | ||||||
) | ||||||
if len(display_name) > DISPLAY_NAME_MAX_LEN: | ||||||
raise ParameterNameError( | ||||||
f"Display name {display_name} greater than {DISPLAY_NAME_MAX_LEN} characters." | ||||||
|
@@ -38,6 +42,8 @@ def ensure_display_name(display_name: str) -> str: | |||||
|
||||||
def ensure_variable_name(variable_name: str) -> str: | ||||||
"""Validate variable name is a valid python variable name.""" | ||||||
if not isinstance(variable_name, str): | ||||||
raise ParameterNameError("Variable name must be a string.") | ||||||
if not variable_name.isidentifier(): | ||||||
raise ParameterNameError( | ||||||
"Variable name must only contain alphanumeric characters, underscores, and cannot start with a digit." | ||||||
|
@@ -49,19 +55,29 @@ def ensure_variable_name(variable_name: str) -> str: | |||||
|
||||||
def ensure_description(description: Optional[str]) -> Optional[str]: | ||||||
"""Validate description is within the character limit.""" | ||||||
if description is not None and len(description) > DESCRIPTION_MAX_LEN: | ||||||
raise ParameterNameError( | ||||||
f"Description {description} greater than {DESCRIPTION_MAX_LEN} characters." | ||||||
) | ||||||
if description is not None: | ||||||
if not isinstance(description, str): | ||||||
raise ParameterNameError( | ||||||
f"Description must be a string and less than {DESCRIPTION_MAX_LEN} characters." | ||||||
) | ||||||
if len(description) > DESCRIPTION_MAX_LEN: | ||||||
raise ParameterNameError( | ||||||
f"Description {description} greater than {DESCRIPTION_MAX_LEN} characters." | ||||||
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. Does |
||||||
) | ||||||
return description | ||||||
|
||||||
|
||||||
def ensure_unit_string_length(unit: Optional[str]) -> Optional[str]: | ||||||
"""Validate unit is within the character limit.""" | ||||||
if unit is not None and len(unit) > UNIT_MAX_LEN: | ||||||
raise ParameterNameError( | ||||||
f"Description {unit} greater than {UNIT_MAX_LEN} characters." | ||||||
) | ||||||
if unit is not None: | ||||||
if not isinstance(unit, str): | ||||||
raise ParameterNameError( | ||||||
f"Unit must be a string and less than {UNIT_MAX_LEN} characters." | ||||||
) | ||||||
if len(unit) > UNIT_MAX_LEN: | ||||||
raise ParameterNameError( | ||||||
f"Description {unit} greater than {UNIT_MAX_LEN} characters." | ||||||
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.
Suggested change
|
||||||
) | ||||||
return unit | ||||||
|
||||||
|
||||||
|
@@ -173,7 +189,7 @@ def _validate_choices( | |||||
ensure_display_name(display_name) | ||||||
if not isinstance(value, parameter_type): | ||||||
raise ParameterDefinitionError( | ||||||
f"All choices provided must match type {type(parameter_type)}" | ||||||
f"All choices provided must match type {parameter_type}" | ||||||
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.
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. Maybe write it as |
||||||
) | ||||||
|
||||||
|
||||||
|
@@ -192,21 +208,25 @@ def _validate_min_and_max( | |||||
"If a maximum value is provided a minimum must also be provided." | ||||||
) | ||||||
elif maximum is not None and minimum is not None: | ||||||
if isinstance(maximum, (int, float)) and isinstance(minimum, (int, float)): | ||||||
if maximum <= minimum: | ||||||
if parameter_type is int or parameter_type is float: | ||||||
if not isinstance(minimum, parameter_type): | ||||||
raise ParameterDefinitionError( | ||||||
"Maximum must be greater than the minimum" | ||||||
f"Minimum is type {type(minimum)}, must match parameter type {parameter_type}" | ||||||
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. comma splice, easy to fix by adding a conjunction
Suggested change
replace |
||||||
) | ||||||
|
||||||
if not isinstance(minimum, parameter_type) or not isinstance( | ||||||
maximum, parameter_type | ||||||
): | ||||||
if not isinstance(maximum, parameter_type): | ||||||
raise ParameterDefinitionError( | ||||||
f"Minimum and maximum must match type {parameter_type}" | ||||||
f"Maximum is type {type(minimum)}, must match parameter type {parameter_type}" | ||||||
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.
Suggested change
|
||||||
) | ||||||
# These asserts are for the type checker and should never actually be asserted false | ||||||
assert isinstance(minimum, (int, float)) | ||||||
assert isinstance(maximum, (int, float)) | ||||||
if maximum <= minimum: | ||||||
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. |
||||||
raise ParameterDefinitionError( | ||||||
"Maximum must be greater than the minimum" | ||||||
) | ||||||
else: | ||||||
raise ParameterDefinitionError( | ||||||
"Only parameters of type float or int can have a minimum and maximum" | ||||||
f"Parameter of type {parameter_type} does not support minimum or maximum arguments." | ||||||
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 think the previous revision of this message was more informative. It says what does support min/max, not only that the current type doesn't. And I can't see a future type that isn't |
||||||
) | ||||||
|
||||||
|
||||||
|
@@ -226,7 +246,10 @@ def validate_options( | |||||
parameter_type: type, | ||||||
) -> None: | ||||||
"""Validate default values and all possible constraints for a valid parameter definition.""" | ||||||
validate_type(default, parameter_type) | ||||||
if not isinstance(default, parameter_type): | ||||||
raise ParameterValueError( | ||||||
f"Parameter default {default} has type {type(default)}, must match type {parameter_type}." | ||||||
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. apply two decisions from above:
|
||||||
) | ||||||
|
||||||
if choices is None and minimum is None and maximum is None: | ||||||
raise ParameterDefinitionError( | ||||||
|
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.
Probably
at most
rather thanless than
if the maximum is inclusive.