Skip to content

Commit

Permalink
CTX-4587: Fix invalid results for some of the validation cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dule1322 committed Sep 14, 2023
1 parent 4ac1316 commit abb7ba2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
9 changes: 9 additions & 0 deletions coretex/coretex/experiment/parameter/base_list_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ def validate(self) -> Tuple[bool, Optional[str]]:
if not self.required and self.value is None:
return True, None

if self.required and len(self.value) == 0: # type: ignore[arg-type]
return False, f"Required parameter \"{self.name}\" must contain a non-empty array"

# self.value is of type Optional[Any], but base class validate method already
# checks if it is a list, if that fails this is not reachable
for element in self.value: # type: ignore[union-attr]

# bool is a subclass of int, do not allow validation to pass if
# we are looking for integer, but bool is received
if isinstance(element, bool) and int in self.listTypes and not bool in self.listTypes:
return False, None

if not any(isinstance(element, type_) for type_ in self.listTypes):
return False, None

Expand Down
5 changes: 5 additions & 0 deletions coretex/coretex/experiment/parameter/base_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def validate(self) -> Tuple[bool, Optional[str]]:
if not self.required and self.value is None:
return True, None

# bool is a subclass of int, do not allow validation to pass if
# we are looking for integer, but bool is received
if isinstance(self.value, bool) and int in self.types and not bool in self.types:
return False, None

if not any(isinstance(self.value, dataType) for dataType in self.types):
return False, None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def validate(self) -> Tuple[bool, Optional[str]]:
selected = value["selected"]
options = value["options"]

if not isinstance(selected, int):
if selected is None and not self.required:
return True, None

if not type(selected) is int:
return False, f"Enum parameter \"{self.name}.selected\" has invalid type. Expected \"int\", got \"{type(selected).__name__}\""

if selected >= len(options) or selected < 0:
Expand All @@ -34,4 +37,8 @@ def parseValue(self, task: SpaceTask) -> Optional[Any]:
if self.value is None:
return self.value

return self.value["options"][self.value["selected"]]
selected: Optional[int] = self.value.get("selected")
if selected is None:
return None

return self.value["options"][selected]
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ def validate(self) -> Tuple[bool, Optional[str]]:
selected = value["selected"]
options = value["options"]

if selected is None and not self.required:
return True, None

if not isinstance(selected, list):
return False, f"Enum list parameter \"{self.name}.selected\" has invalid type. Expected \"list[int]\", got \"{type(selected).__name__}\""

if not all(isinstance(element, int) for element in selected):
if not all(type(element) is int for element in selected):
elementTypes = ", ".join({type(element).__name__ for element in selected})
return False, f"Enum list parameter \"{self.name}.selected\" has invalid type. Expected \"list[int]\", got \"list[{elementTypes}]\""

Expand All @@ -43,7 +46,10 @@ def parseValue(self, task: SpaceTask) -> Optional[Any]:
if self.value is None:
return self.value

selected: List[int] = self.value["selected"]
selected: Optional[List[int]] = self.value["selected"]
options: List[str] = self.value["options"]

if selected is None:
return None

return [options[value] for value in selected]

0 comments on commit abb7ba2

Please sign in to comment.