Skip to content

Commit d3585a8

Browse files
fix: resolve flaky tenant input validation in Python 3.12 (#1355)
Replaces typing.Sequence with collections.abc.Sequence for runtime type-checking to avoid flaky behavior in Python 3.12+. Signed-off-by: Unmilan Mukherjee <unmilanm@gmail.com>
1 parent 17a9887 commit d3585a8

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

weaviate/validator.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass
2+
import collections.abc
23
from typing import Any, List, Sequence, Union, get_args, get_origin
34

45
from weaviate.exceptions import WeaviateInvalidInputError
@@ -47,16 +48,25 @@ def _is_valid(expected: Any, value: Any) -> bool:
4748
if expected_origin is Union:
4849
args = get_args(expected)
4950
return any(isinstance(value, arg) for arg in args)
51+
52+
# FIX for #1355: Use collections.abc.Sequence for robust runtime check in Python 3.12
5053
if expected_origin is not None and (
51-
issubclass(expected_origin, Sequence) or expected_origin is list
54+
issubclass(expected_origin, collections.abc.Sequence) or expected_origin is list
5255
):
53-
if not isinstance(value, Sequence) and not isinstance(value, list):
56+
if not isinstance(value, (collections.abc.Sequence, list)):
5457
return False
5558
args = get_args(expected)
5659
if len(args) == 1:
5760
if get_origin(args[0]) is Union:
5861
union_args = get_args(args[0])
59-
return any(isinstance(val, union_arg) for val in value for union_arg in union_args)
62+
# Ensure every element in the sequence matches the Union types
63+
return all(any(isinstance(val, union_arg) for union_arg in union_args) for val in value)
6064
else:
6165
return all(isinstance(val, args[0]) for val in value)
62-
return isinstance(value, expected)
66+
67+
try:
68+
return isinstance(value, expected)
69+
except TypeError:
70+
if expected_origin is not None:
71+
return isinstance(value, expected_origin)
72+
return False

0 commit comments

Comments
 (0)