Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion test/collection/test_validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List
from typing import Any, List, Sequence, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -37,3 +37,28 @@ def test_validator(inputs: Any, expected: List[Any], error: bool) -> None:
_validate_input(_ValidateArgument(expected=expected, name="test", value=inputs))
else:
_validate_input(_ValidateArgument(expected=expected, name="test", value=inputs))


@pytest.mark.parametrize(
"inputs,expected,error",
[
# every element matches one of the union members -> valid
(["a", 1, "b", 2], [Sequence[Union[str, int]]], False),
(["a", "b"], [Sequence[Union[str, int]]], False),
([1, 2], [Sequence[Union[str, int]]], False),
# one element (a float) matches neither union member -> must be rejected.
# regression test: the old implementation flattened the per-element check
# into a single any() across (value, union_arg) pairs, so it only took one
# element matching one type to pass the whole sequence, even if other
# elements didn't match anything.
(["a", 3.14], [Sequence[Union[str, int]]], True),
([3.14, "a"], [Sequence[Union[str, int]]], True),
([1, 3.14], [Sequence[Union[str, int]]], True),
],
)
def test_validator_sequence_of_union(inputs: Any, expected: List[Any], error: bool) -> None:
if error:
with pytest.raises(WeaviateInvalidInputError):
_validate_input(_ValidateArgument(expected=expected, name="test", value=inputs))
else:
_validate_input(_ValidateArgument(expected=expected, name="test", value=inputs))
4 changes: 3 additions & 1 deletion weaviate/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def _is_valid(expected: Any, value: Any) -> bool:
if len(args) == 1:
if get_origin(args[0]) is Union:
union_args = get_args(args[0])
return any(isinstance(val, union_arg) for val in value for union_arg in union_args)
return all(
any(isinstance(val, union_arg) for union_arg in union_args) for val in value
)
else:
return all(isinstance(val, args[0]) for val in value)
# bool is a subclass of int, so isinstance(True, int) is True. Reject a
Expand Down