|
1 | 1 | from dataclasses import dataclass |
| 2 | +import collections.abc |
2 | 3 | from typing import Any, List, Sequence, Union, get_args, get_origin |
3 | 4 |
|
4 | 5 | from weaviate.exceptions import WeaviateInvalidInputError |
@@ -47,16 +48,25 @@ def _is_valid(expected: Any, value: Any) -> bool: |
47 | 48 | if expected_origin is Union: |
48 | 49 | args = get_args(expected) |
49 | 50 | 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 |
50 | 53 | 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 |
52 | 55 | ): |
53 | | - if not isinstance(value, Sequence) and not isinstance(value, list): |
| 56 | + if not isinstance(value, (collections.abc.Sequence, list)): |
54 | 57 | return False |
55 | 58 | args = get_args(expected) |
56 | 59 | if len(args) == 1: |
57 | 60 | if get_origin(args[0]) is Union: |
58 | 61 | 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) |
60 | 64 | else: |
61 | 65 | 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